zoukankan      html  css  js  c++  java
  • 代码篇之AOP框架

    AopFrameworkTest类

    public class AopFrameworkTest {
        public static void main(String[] args) throws Exception {
            InputStream ips = AopFrameworkTest.class.getResourceAsStream("config.properties");
            Object bean = new BeanFactory(ips).getBean("xxx");
            System.out.println(bean.getClass().getName());
            ((Collection)bean).clear();
        }
    }

    ProxyFactoryBean类

    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() {
            Object proxy3 = Proxy.newProxyInstance(
                    target.getClass().getClassLoader(),
                    target.getClass().getInterfaces(),
                    new InvocationHandler(){
                    
                        public Object invoke(Object proxy, Method method, Object[] args)
                                throws Throwable {
                            advice.beforeMethod(method);
                            Object retVal = method.invoke(target, args);
                            advice.afterMethod(method);
                            return retVal;                        
                        }
                    }
                    );
            return proxy3;
        }
    
    }

    BeanFactory类

    public class BeanFactory {
        Properties props = new Properties();
        public BeanFactory(InputStream ips){
            try {
                props.load(ips);
            } catch (IOException e) {
                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) {
                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) {
                    e.printStackTrace();
                }
                return proxy;
            }
            return bean;
        }
    }

    config.properties文件

    #xxx=java.util.ArrayList
    xxx=cn.itcast.day3.aopframework.ProxyFactoryBean
    xxx.advice=cn.itcast.day3.MyAdvice
    xxx.target=java.util.ArrayList
  • 相关阅读:
    《应用Yii1.1和PHP5进行敏捷Web开发》学习笔记(转)
    YII 小模块功能
    Netbeans代码配色主题大搜集
    opensuse 启动巨慢 解决方法 90s多
    opensuse 安装 网易云音乐 rpm netease music
    linux qq rpm deb opensuse
    openSUSE 安装 alien
    第一行代码 Android 第2版
    Android Studio AVD 虚拟机 联网 失败
    docker error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.29/containers/json: open //./pipe/docker_engine: The system cannot find the file specified. In the default daemon configuratio
  • 原文地址:https://www.cnblogs.com/atomicbomb/p/6624473.html
Copyright © 2011-2022 走看看