原创spring aop精简版

来源:互联网 发布:知乎评论 编辑:程序博客网 时间:2024/05/17 11:35

//javabean 必须要有一个不带参数的构造器


config.properties

#xxx=java.util.ArrayList
xxx=cn.itcast.day3.aopframework.ProxyFactoryBean
xxx.advice=cn.itcast.day3.MyAdvice
xxx.target=java.util.ArrayList


BeanFactory.java

public class BeanFactory {
    Properties props = new Properties();
    public BeanFactory(InputStream ips){
        try {
            props.load(ips);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public Object getBean(String name){
        String className = props.getProperty(name);
        Object bean = null;
        try {
            Class clazz = Class.forName(className);
            bean = clazz.newInstance();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if(bean instanceof ProxyFactoryBean){
            Object proxy = null;
            ProxyFactoryBean proxyFactoryBean = (ProxyFactoryBean)bean;
            try {
                Advice advice = (Advice)Class.forName(props.getProperty(name + ".advice")).newInstance();
                Object target = Class.forName(props.getProperty(name + ".target")).newInstance();
                proxyFactoryBean.setAdvice(advice);
                proxyFactoryBean.setTarget(target);
                proxy = proxyFactoryBean.getProxy();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return proxy;
        }
        return bean;
    }
}


Advice.java

public interface Advice {
    void beforeMethod(Method method);
    void afterMethod(Method method);
}

MyAdvice.java

public class MyAdvice implements Advice {
    long beginTime = 0;
    public void afterMethod(Method method) {
        // TODO Auto-generated method stub
        System.out.println("毕业上班啦!");        
        long endTime = System.currentTimeMillis();
        System.out.println(method.getName() + " running time of " + (endTime - beginTime));

    }

    public void beforeMethod(Method method) {
        // TODO Auto-generated method stub
        System.out.println("来学习啦!");
        beginTime = System.currentTimeMillis();
    }

}


ProxyFactoryBean.java

public class ProxyFactoryBean {

    private Advice advice;
    private Object target;
    
    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;
    }

    public Object getProxy() {
        // TODO Auto-generated method stub
        Object proxy3 = Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                /*new Class[]{Collection.class},*/
                target.getClass().getInterfaces(),
                new InvocationHandler(){
                
                    public Object invoke(Object proxy, Method method, Object[] args)
                            throws Throwable {

                        /*long beginTime = System.currentTimeMillis();
                        Object retVal = method.invoke(target, args);
                        long endTime = System.currentTimeMillis();
                        System.out.println(method.getName() + " running time of " + (endTime - beginTime));
                        return retVal;*/
                        

                        advice.beforeMethod(method);
                        Object retVal = method.invoke(target, args);
                        advice.afterMethod(method);
                        return retVal;                        
                        
                    }
                }
                );
        return proxy3;
    }

}


AopFrameworkTest.java

public class AopFrameworkTest {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        InputStream ips = AopFrameworkTest.class.getResourceAsStream("config.properties");
        Object bean = new BeanFactory(ips).getBean("xxx");
        System.out.println(bean.getClass().getName());
        ((Collection)bean).clear();
    }

}