动态代理的实现3-实现aop功能的封装和配置

来源:互联网 发布:淘宝互刷收藏软件 编辑:程序博客网 时间:2024/05/16 02:41

[java] view plaincopy
  1. //一个通告或建议的接口  
  2. public interface Advice {  
  3.     void afterMethod(Method method);  
  4.     void beforeMethod(Method method);  
  5. }  
[java] view plaincopy
  1. public class MyAdvice implements Advice{  
  2.     private long start;  
  3.     @Override  
  4.     public void afterMethod(Method method) {  
  5.         System.out.println("运行时间计算结束!!");  
  6.         System.out.println("共耗时:"+(System.currentTimeMillis()-start));  
  7.         System.out.println("------------------------");  
  8.     }  
  9.   
  10.     @Override  
  11.     public void beforeMethod(Method method) {  
  12.         System.out.println("开始计算运行时间:");  
  13.         start=System.currentTimeMillis();  
  14.         System.out.println("所执行的方法:"+method.getName());  
  15.     }  
  16. }  
[java] view plaincopy
  1. Properties文件的配置  
  2. #xxx=java.util.ArrayList  
  3. xxx=aopframework.ProxyFactoryBean  
  4. xxx.advice=aopframework.MyAdvice  
  5. xxx.target=java.util.ArrayList  
[java] view plaincopy
  1. /** 
  2.  * 负责创建目标类或代理类的实例对象,并通过配置文件实现切换 
  3.  * 
  4.  */  
  5. public class BeanFactory {  
  6.     private Properties prop;  
  7.     public BeanFactory(InputStream is){  
  8.         prop=new Properties();  
  9.         try {prop.load(is);  
  10.         } catch (IOException e) {  
  11.             e.printStackTrace();  
  12.         }  
  13.     }  
  14.     public Object getBean(String propkey){  
  15.         String className=prop.getProperty(propkey);  
  16.         Object bean=null;  
  17.         try {  
  18.             bean=Class.forName(className).newInstance();  
  19.             if(bean instanceof ProxyFactoryBean){  
  20.                 String adviceName=prop.getProperty(propkey+".advice");  
  21.                 String targetName=prop.getProperty(propkey+".target");  
  22.                 try {  
  23.                     Advice advice = (Advice) Class.forName(adviceName).newInstance();  
  24.                     Object target = Class.forName(targetName).newInstance();  
  25.                     ProxyFactoryBean beanproxy=(ProxyFactoryBean)bean;  
  26.                     beanproxy.setAdvice(advice);  
  27.                     beanproxy.setTarget(target);  
  28.                     bean=beanproxy.getProxy();  
  29.                 } catch (Exception e) {  
  30.                     e.printStackTrace();  
  31.                 }  
  32.             }  
  33.         } catch (Exception e) {  
  34.             e.printStackTrace();  
  35.         }  
  36.         return bean;  
  37.     }  
  38. }  
[java] view plaincopy
  1. /** 
  2.  * 充当封装生成动态代理的工厂 
  3.  */  
  4. public class ProxyFactoryBean {  
  5.     private Advice advice;  
  6.     private Object target;  
  7.     public Object getProxy() {  
  8.         Object coll=(Object) Proxy.newProxyInstance(  
  9.                 target.getClass().getClassLoader(),   
  10.                 target.getClass().getInterfaces(),   
  11.                 new InvocationHandler(){  
  12.                     @Override  
  13.                     public Object invoke(Object proxy, Method method,  
  14.                             Object[] args) throws Throwable {  
  15.                         advice.beforeMethod(method);  
  16.                         Object obj=method.invoke(target, args);  
  17.                         advice.afterMethod(method);  
  18.                         return obj;  
  19.                     }  
  20.                 });  
  21.         return coll;  
  22.     }  
  23.     public Advice getAdvice() {  
  24.         return advice;  
  25.     }  
  26.     public void setAdvice(Advice advice) {  
  27.         this.advice = advice;  
  28.     }  
  29.     public Object getTarget() {  
  30.         return target;  
  31.     }  
  32.     public void setTarget(Object target) {  
  33.         this.target = target;  
  34.     }  
  35. }  
[java] view plaincopy
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         InputStream is=Test.class.getClassLoader().getResourceAsStream("config.properties");  
  4.         BeanFactory factory=new BeanFactory(is);  
  5.         Collection coll=(Collection) factory.getBean("xxx");  
  6.         coll.add("aaa");  
  7.         coll.add("222");  
  8.         System.out.println(coll.size());  
  9.     }  
  10.     /** 
  11.      * 运行结果 
  12.                开始计算运行时间: 
  13.         所执行的方法:add 
  14.         运行时间计算结束!! 
  15.         共耗时:0 
  16.         ------------------------ 
  17.         开始计算运行时间: 
  18.         所执行的方法:add 
  19.         运行时间计算结束!! 
  20.         共耗时:0 
  21.         ------------------------ 
  22.         开始计算运行时间: 
  23.         所执行的方法:size 
  24.         运行时间计算结束!! 
  25.         共耗时:0 
  26.         ------------------------ 
  27.         2 
  28.      */  
  29. }  
原创粉丝点击