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
  • 相关阅读:
    Attributes in C#
    asp.net C# 时间格式大全
    UVA 10518 How Many Calls?
    UVA 10303 How Many Trees?
    UVA 991 Safe Salutations
    UVA 10862 Connect the Cable Wires
    UVA 10417 Gift Exchanging
    UVA 10229 Modular Fibonacci
    UVA 10079 Pizza Cutting
    UVA 10334 Ray Through Glasses
  • 原文地址:https://www.cnblogs.com/atomicbomb/p/6624473.html
Copyright © 2011-2022 走看看