黑马程序员——类似于spring的AOP框架

BeanFactory.java的代码

package com.itAOP;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/*
 * 整体的工作原理是这样的:
 * 工厂类BeanFactory在创建目标类或代理的对象的时候,先由getBean接收一个字符串,
 * 然后到配置文件中查找器对应的字符串并产生器对象,如果这个对象是ProxyFactoryBean类的
 * 实例对象,那么ProxyFactoryBean调用getProxy产生并返回代理对象;如果不是,直接返回
 * 这个目标对象。在配置文件中可以切换该字符串的内容
 * 
 * BeanFactory是一个Bean的工厂类,专门用于产生Bean,在它里面调用了一个特殊的类是ProxyFactoryBean
 * ProxyFactoryBean也是一个Bean,但他同时也是一个Proxy的一个工厂,专门用来产生代理的
 * 
 */

//BeanFactory就是负责创建目标类或代理类的对象,并通过配置文件实现切换
public class BeanFactory {
    //这种配置文件是Propeties格式的
    Properties properties = new Properties();
    //实例化BeanFactory的时候,需要给他传入一个配置文件,直接就传入一个流了
    public BeanFactory(InputStream inputStream){
        try {
            //将文件加载进来
            properties.load(inputStream);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    //创建出对应name的对象或代理
    public Object getBean(String name){
        Object bean = null;
        try {
            //这就是为什么Bean当中必须有一个无参的构造方法了,newInsatance()
            bean = Class.forName(properties.getProperty(name)).newInstance();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //判断这个对象是否是ProxyFactory的实例对象
        if(bean instanceof ProxyFactoryBean){
            Object proxy = null;
            ProxyFactoryBean proxyFactoryBean = (ProxyFactoryBean)bean;
            try {
                Class clazzAdvice = Class.forName(properties.getProperty(name+".advice"));
                proxyFactoryBean.setAdvice((Advice)clazzAdvice.newInstance());
                Class clazzTarget = Class.forName(properties.getProperty(name+".target"));
                proxyFactoryBean.setTarget((Object)clazzTarget.newInstance());
                //产生代理对象
                proxy = proxyFactoryBean.getProxy();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InstantiationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            return proxy;
            
        }
        return bean;
        
        
        
    }


}

 

ProxyFactoryBean.java的代码

package com.itAOP;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyFactoryBean {
    private Advice advice = null;
    private Object target = null;
    
    
    
    public Advice getAdvice() {
        return advice;
    }



    public void setAdvice(Advice advice) {
        this.advice = advice;
    }



    public Object getTarget() {
        return target;
    }



    public void setTarget(Object target) {
        this.target = target;
    }



    //方法里主要用到了两个参数,target和advice,这里定义为成员变量
    public Object getProxy() {
        // TODO Auto-generated method stub
        Object objProxy = (Object)Proxy.newProxyInstance(
                target.getClass().getClassLoader(), 
                target.getClass().getInterfaces(), 
                new InvocationHandler(){
                    //这里定义一个目标
                    

                    @Override
                    public Object invoke(Object proxy, Method method,
                            Object[] args) throws Throwable {
                        // TODO Auto-generated method stub
                        advice.methodBefore(method);
                        //在目标上执行代理的那个方法
                        Object obj = method.invoke(target, args);
                        advice.methodAfter(method);
                        return obj;
                    }
                    
                }
        );
        return objProxy;
    }
    

}

 

测试类BeanFactory.java

package com.itAOP;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class BeanFactoryTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        //在Class类里可以通过getResourceAsStream()来获取InputStream
        InputStream inputStream = BeanFactoryTest.class.getResourceAsStream("config.properties");
        BeanFactory bean = new BeanFactory(inputStream);
        Object obj = bean.getBean("nihao");
        
        System.out.println(obj.getClass().getName());

     obj.add("123"); } }

配置文件config.properties

nihao=java.util.ArrayList
#nihao=com.itAOP.ProxyFactoryBean
nihao.advice=com.itAOP.MyAdvice
nihao.target=java.util.ArrayList

Advice接口

package com.itAOP;

import java.lang.reflect.Method;

public interface Advice {
    public void methodBefore(Method method);
    public void methodAfter(Method method);

}


实现Advice接口的子类MyAdvice.java

package com.itAOP;

import java.lang.reflect.Method;

public class MyAdvice implements Advice {

    private long beginTime = 0;
    @Override
    public void methodBefore(Method method) {
        // TODO Auto-generated method stub
        //我们可以再执行目标的前后干一些其他的事情,比如下面
        //System.currentTimeMillis()返回的当前时间的毫秒
        System.out.println("我来了");
        beginTime = System.currentTimeMillis();
    }

    @Override
    public void methodAfter(Method method) {
        // TODO Auto-generated method stub
        long endTime = System.currentTimeMillis();
        System.out.println(method.getName()+"......"+(endTime-beginTime));
        System.out.println("我走了");
    }

}

黑马程序员——类似于spring的AOP框架,布布扣,bubuko.com

黑马程序员——类似于spring的AOP框架

上一篇:Spring定时器


下一篇:Android-AndroidStudio莫名其妙的错误-finished with non-zero exit value 1