反射实现AOP动态代理

概念:

AOP是Aspect Oriented Programming的缩写,意思是面向切面编程

 

功能:

日志记录,性能统计,安全控制,事务处理,异常处理等

 

原理:

AOP通过反射机制实现动态代理,具体看下面举例吧。

 

举例:

在业务方法执行前后加日志记录

业务类接口IHello.java

Java代码   收藏代码
  1. package demo.aop;  
  2.   
  3. public interface IHello {  
  4.     /** 
  5.      * 业务处理A 
  6.      *  
  7.      * @param name 
  8.      */  
  9.     void sayHello(String name);  
  10.   
  11.     /** 
  12.      * 业务处理B 
  13.      *  
  14.      * @param name 
  15.      */  
  16.     void sayGoodBye(String name);  
  17. }  

 IHello.java的一个实现类

Java代码   收藏代码
  1. package demo.aop;  
  2.   
  3. public class Hello implements IHello{  
  4.   
  5.     public void sayHello(String name) {  
  6.         System.out.println("hello " + name);  
  7.     }  
  8.   
  9.     public void sayGoodBye(String name) {  
  10.         System.out.println(name + " goodbye!");  
  11.     }  
  12.   
  13. }  

 

日志记录相关的类:Logger类和Level枚举

Java代码   收藏代码
  1. package demo.aop;  
  2.   
  3. public enum Level {  
  4.     DEBUG, INFO  
  5. }  

 

Java代码   收藏代码
  1. package demo.aop;  
  2.   
  3. import java.util.Date;  
  4.   
  5. public class Logger {  
  6.     public static void logging(Level level, String context) {  
  7.         if (Level.DEBUG.equals(level)) {  
  8.             System.out.println("DEBUG\t" + new Date().toString() + "\t"  
  9.                     + context);  
  10.         } else if (Level.INFO.equals(level)) {  
  11.             System.out.println("INFO\t" + new Date().toString() + "\t"  
  12.                     + context);  
  13.         }  
  14.     }  
  15. }  

 

 下面我们为这个sayHello业务方法加上日志记录的业务,我们在不改变原代码的情况下实现该功能

思路1:写一个类实现IHello接口,并依赖Hello这个类

Java代码   收藏代码
  1. package demo.aop;  
  2.   
  3. /** 
  4.  * Hello代理类 
  5.  *  
  6.  * @author ydc 
  7.  * @date 6:55:42 PM Jul 31, 2013 
  8.  */  
  9. public class ProxyHello implements IHello {  
  10.     private IHello hello;  
  11.   
  12.     public ProxyHello(IHello hello) {  
  13.         this.hello = hello;  
  14.     }  
  15.   
  16.     public void sayHello(String name) {  
  17.         Logger.logging(Level.DEBUG, "sayHello method start");  
  18.         hello.sayHello(name);  
  19.         Logger.logging(Level.INFO, "sayHello method end");  
  20.     }  
  21.   
  22.     public void sayGoodBye(String name) {  
  23.         hello.sayGoodBye(name);  
  24.     }  
  25.   
  26. }  

 测试一下

Java代码   收藏代码
  1. package demo.aop;  
  2.   
  3. public class AopTest {  
  4.     public static void main(String[] args) {  
  5.         new AopTest().test1();  
  6.     }  
  7.   
  8.     public void test1() {  
  9.         // 无日志记录功能  
  10.         IHello hello1 = new Hello();  
  11.   
  12.         // 有日志记录功能  
  13.         IHello hello2 = new ProxyHello(new Hello());  
  14.   
  15.         hello1.sayHello("wallet white");  
  16.         System.out.println("------------------------------");  
  17.         hello2.sayHello("wallet white");  
  18.           
  19.     }  
  20. }  

 结果输出:

Java代码   收藏代码
  1. hello wallet white  
  2. ------------------------------  
  3. DEBUG   Wed Jul 31 21:46:38 CST 2013    sayHello method start  
  4. hello wallet white  
  5. INFO    Wed Jul 31 21:46:38 CST 2013    sayHello method end  

 

 缺点:如果像Hello这样的类很多,那么我们就要写很多个ProxyHello这样的类

 

思路2:在jdk1.3以后,jdk提供了一个API java.lang.reflect.InvocationHandler的类,这个类可以让我们在JVM调用某个类的方法时动态的为这些方法做些其他事

 

写一个代理类实现DynaProxyHello实现InvocationHandler接口

Java代码   收藏代码
  1. package demo.aop;  
  2.   
  3. import java.lang.reflect.InvocationHandler;  
  4. import java.lang.reflect.Method;  
  5. import java.lang.reflect.Proxy;  
  6.   
  7. /** 
  8.  * Hello动态代理类 代理对象与被代理对像解藕 
  9.  *  
  10.  * @author ydc 
  11.  * @date 6:56:27 PM Jul 31, 2013 
  12.  */  
  13. public class DynaProxyHello implements InvocationHandler {  
  14.     /** 
  15.      * 要处理的对象 
  16.      */  
  17.     private Object delegate;  
  18.   
  19.     /** 
  20.      * 动态生成方法被处理过后的对象(写法固定) 
  21.      *  
  22.      * @param delegate 
  23.      * @return Object 
  24.      */  
  25.     public Object bind(Object delegate) {  
  26.         this.delegate = delegate;  
  27.         return Proxy.newProxyInstance(  
  28.                 this.delegate.getClass().getClassLoader(), this.delegate  
  29.                         .getClass().getInterfaces(), this);  
  30.     }  
  31.   
  32.     /** 
  33.      * 要处理的对象中的每个方法会被此方法送去JVM调用,也就是说, 要处理的对象的方法只能通过此方法調用,此方法是动态的 
  34.      */  
  35.     public Object invoke(Object proxy, Method method, Object[] args)  
  36.             throws Throwable {  
  37.         Object result = null;  
  38.         try {  
  39.             // 执行原来的方法之前记录日志  
  40.             Logger.logging(Level.DEBUG, method.getName() + " method start");  
  41.   
  42.             // JVM通过这条语句执行原来的方法(反射机制)  
  43.             result = method.invoke(this.delegate, args);  
  44.   
  45.             // 执行原来的方法之后记录日志  
  46.             Logger.logging(Level.INFO, method.getName() + " method end");  
  47.         } catch (Exception e) {  
  48.             e.printStackTrace();  
  49.         }  
  50.   
  51.         // 返回方法返回值给调用者  
  52.         return result;  
  53.   
  54.     }  
  55. }  

 测试一下

Java代码   收藏代码
  1. package demo.aop;  
  2.   
  3. public class AopTest {  
  4.     public static void main(String[] args) {  
  5.         new AopTest().test2();  
  6.     }  
  7.       
  8.     public void test2() {  
  9.         IHello hello = (IHello) new DynaProxyHello().bind(new Hello());  
  10.         hello.sayHello("wallet white");  
  11.                 System.out.println("------------------");  
  12.         hello.sayGoodBye("wallet white");  
  13.     }  
  14. }  

 结果输出:

Java代码   收藏代码
  1. DEBUG   Wed Jul 31 21:57:49 CST 2013    sayHello method start  
  2. hello wallet white  
  3. INFO    Wed Jul 31 21:57:49 CST 2013    sayHello method end  
  4. ------------------  
  5. DEBUG   Wed Jul 31 21:57:49 CST 2013    sayGoodBye method start  
  6. wallet white goodbye!  
  7. INFO    Wed Jul 31 21:57:49 CST 2013    sayGoodBye method end  

 

由此可知,采用面向接口编程,对于任何对象的方法执行之前要加上记录日志的操作都是可以的。

代理对象(DynaProxyHello)自动去代理执行被代理对象(Hello)中的每一个方法,一个InvocationHandler接口就把我们的代理对象和被代理对象解藕了。

 

问题:要是为不同的业务方法前加的日志记录不同,就需要写多个DynaProxyHello类

 

加强版:把DynaProxyHello对象和日志操作对象(Logger)解藕

我们要在被代理对象的方法前面或者后面加上日志操作代码 ,那么我们可以抽象一个接口,该接口就只有两个方法

Java代码   收藏代码
  1. package demo.aop;  
  2.   
  3. import java.lang.reflect.Method;  
  4.   
  5. public interface IOperation {  
  6.     /** 
  7.      * 方法执行之前的操作 
  8.      *  
  9.      * @param method 
  10.      */  
  11.     void start(Method method);  
  12.   
  13.     /** 
  14.      * 方法执行之后的操作 
  15.      *  
  16.      * @param method 
  17.      */  
  18.     void end(Method method);  
  19. }  

 写一个实现上面接口的类,把他作为真正的操作者,

Java代码   收藏代码
  1. package demo.aop;  
  2.   
  3. import java.lang.reflect.Method;  
  4. /** 
  5.  * 该类将代理者与操作者解藕 
  6.  *  
  7.  * @author ydc 
  8.  * @date 7:22:24 PM Jul 31, 2013 
  9.  */  
  10. public class LoggerOperation implements IOperation {  
  11.   
  12.     public void end(Method method) {  
  13.         Logger.logging(Level.DEBUG, method.getName() + "method end");  
  14.     }  
  15.   
  16.     public void start(Method method) {  
  17.         Logger.logging(Level.INFO, method.getName() + "method start");  
  18.   
  19.     }  
  20.   
  21. }  

 新建一个代理对象DynaProxyHello2

Java代码   收藏代码
  1. package demo.aop;  
  2.   
  3. import java.lang.reflect.InvocationHandler;  
  4. import java.lang.reflect.Method;  
  5. import java.lang.reflect.Proxy;  
  6.   
  7. /** 
  8.  * Hello动态代理类 代理对象与被代理对像解藕 
  9.  *  
  10.  * @author ydc 
  11.  * @date 6:56:27 PM Jul 31, 2013 
  12.  */  
  13. public class DynaProxyHello2 implements InvocationHandler {  
  14.     /** 
  15.      * 操作者 
  16.      */  
  17.     private Object proxy;  
  18.   
  19.     /** 
  20.      * 要处理的对象 
  21.      */  
  22.     private Object delegate;  
  23.   
  24.     /** 
  25.      * 动态生成方法被处理过后的对象(写法固定) 
  26.      *  
  27.      * @param delegate 
  28.      * @param proxy 
  29.      * @return Object 
  30.      */  
  31.     public Object bind(Object delegate, Object proxy) {  
  32.         this.delegate = delegate;  
  33.         this.proxy = proxy;  
  34.         return Proxy.newProxyInstance(  
  35.                 this.delegate.getClass().getClassLoader(), this.delegate  
  36.                         .getClass().getInterfaces(), this);  
  37.     }  
  38.   
  39.     /** 
  40.      * 要处理的对象中的每个方法会被此方法送去JVM调用,也就是说, 要处理的对象的方法只能通过此方法調用,此方法是动态的 
  41.      */  
  42.     @SuppressWarnings("unchecked")  
  43.     public Object invoke(Object proxy, Method method, Object[] args)  
  44.             throws Throwable {  
  45.         Object result = null;  
  46.         try {  
  47.             // 反射得到操作者的实例  
  48.             Class clazz = this.proxy.getClass();  
  49.             // 反射得到操作者的start方法  
  50.             Method start = clazz.getDeclaredMethod("start",  
  51.                     new Class[]{Method.class});  
  52.             // 反射执行start方法  
  53.             start.invoke(this.proxy, new Object[]{method});  
  54.   
  55.             // JVM通过这条语句执行原来的方法(反射机制)  
  56.             result = method.invoke(this.delegate, args);  
  57.   
  58.             // 反射得到操作者的end方法  
  59.             Method end = clazz.getDeclaredMethod("end",  
  60.                     new Class[]{Method.class});  
  61.             // 反射执行end方法  
  62.             end.invoke(this.proxy, new Object[]{method});  
  63.         } catch (Exception e) {  
  64.             e.printStackTrace();  
  65.         }  
  66.   
  67.         // 返回方法返回值给调用者  
  68.         return result;  
  69.   
  70.     }  
  71. }  

 测试一下

Java代码   收藏代码
  1. package demo.aop;  
  2.   
  3. public class AopTest {  
  4.     public static void main(String[] args) {  
  5.         new AopTest().test3();  
  6.     }  
  7.     public void test3() {  
  8.         IHello hello = (IHello) new DynaProxyHello2().bind(new Hello(),  
  9.                 new LoggerOperation());  
  10.         hello.sayHello("wallet white");  
  11.         System.out.println("------------------");  
  12.         hello.sayGoodBye("wallet white");  
  13.     }  
  14. }  

 结果输出:

Java代码   收藏代码
  1. INFO    Wed Jul 31 22:12:08 CST 2013    sayHellomethod start  
  2. hello wallet white  
  3. DEBUG   Wed Jul 31 22:12:08 CST 2013    sayHellomethod end  
  4. ------------------  
  5. INFO    Wed Jul 31 22:12:08 CST 2013    sayGoodByemethod start  
  6. wallet white goodbye!  
  7. DEBUG   Wed Jul 31 22:12:08 CST 2013    sayGoodByemethod end  

 

本文主要参考了下面列出的参考文献的第2篇文章

文章最后留下一个问题:如果不想让所有方法都被日志记录,我们应该怎么去解藕?

也给了一个明确的思路:将需要日志记录的方法写在配置文件里

我在这里做法如下:

---------------------------------------------------------------------------------------------

在馆内添加一个配置文件aop.xml(demo.aop)

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="gbk"?>  
  2. <aop>  
  3.     <clazz name="demo.aop.Hello">  
  4.         <method name="sayHello" />  
  5.     </clazz>  
  6.     <clazz name="demo.aop.Hello2">  
  7.         <method name="sayHello" />  
  8.         <method name="sayGoodBye" />  
  9.     </clazz>  
  10. </aop>  

 在这里是完全匹配,由配置可以看出,对于类demo.aop.Hello内的sayHello方法和类demo.aop.Hello2内的sayHello,sayGoodBye方法添加日志记录功能

 

添加一个读取配置文件的工具类

Java代码   收藏代码
  1. package demo.aop;  
  2.   
  3. import java.io.File;  
  4. import java.util.ArrayList;  
  5. import java.util.HashMap;  
  6. import java.util.List;  
  7. import java.util.Map;  
  8.   
  9. import javax.xml.parsers.DocumentBuilder;  
  10. import javax.xml.parsers.DocumentBuilderFactory;  
  11.   
  12. import org.w3c.dom.Document;  
  13. import org.w3c.dom.Element;  
  14. import org.w3c.dom.Node;  
  15. import org.w3c.dom.NodeList;  
  16.   
  17. public class AopUtil {  
  18.   
  19.     private static Map<String, List<String>> aopMap = null;  
  20.   
  21.     public static Map<String, List<String>> initConfig() {  
  22.         if (aopMap != null) {  
  23.             return aopMap;  
  24.         }  
  25.         aopMap = new HashMap<String, List<String>>();  
  26.         String filePath = Class.class.getClass().getResource(  
  27.                 "/demo/aop/aop.xml").getPath();  
  28.         File f = new File(filePath);  
  29.   
  30.         Element element = null;  
  31.         DocumentBuilder db = null;  
  32.         DocumentBuilderFactory dbf = null;  
  33.         try {  
  34.             dbf = DocumentBuilderFactory.newInstance();  
  35.             db = dbf.newDocumentBuilder();  
  36.             Document dt = db.parse(f);  
  37.             element = dt.getDocumentElement(); // 根元素 aop  
  38.             NodeList clazzNodes = element.getChildNodes(); // 根元素下的子节点 clazz  
  39.             int cLen = clazzNodes.getLength();  
  40.   
  41.             // 遍历包名.类名 clazz  
  42.             for (int i = 0; i < cLen; i++) {  
  43.                 Node node1 = clazzNodes.item(i);  
  44.                 if ("clazz".equals(node1.getNodeName())) {  
  45.                     String clazzName = node1.getAttributes().getNamedItem(  
  46.                             "name").getNodeValue();  
  47.                     NodeList nodeDetail = node1.getChildNodes();  
  48.   
  49.                     List<String> methodList = new ArrayList<String>();  
  50.   
  51.                     // 遍历方法名  
  52.                     for (int j = 0; j < nodeDetail.getLength(); j++) {  
  53.                         Node detail = nodeDetail.item(j);  
  54.                         if ("method".equals(detail.getNodeName())) {  
  55.                             String methodName = detail.getAttributes()  
  56.                                     .getNamedItem("name").getNodeValue();  
  57.                             methodList.add(methodName);  
  58.                         }  
  59.                     }  
  60.                     aopMap.put(clazzName, methodList);  
  61.                 }  
  62.             }  
  63.         } catch (Exception e) {  
  64.             e.printStackTrace();  
  65.         }  
  66.   
  67. //      for (Map.Entry<String, List<String>> entry : aopMap.entrySet()) {  
  68. //          System.out.println(entry.getKey());  
  69. //          List<String> mList = entry.getValue();  
  70. //          if (mList != null)  
  71. //              for (String s : mList) {  
  72. //                  System.out.println(s);  
  73. //              }  
  74. //      }  
  75.   
  76.         return aopMap;  
  77.     }  
  78. }  

 

修改动态代理类

Java代码   收藏代码
  1. package demo.aop;  
  2.   
  3. import java.lang.reflect.InvocationHandler;  
  4. import java.lang.reflect.Method;  
  5. import java.lang.reflect.Proxy;  
  6. import java.util.List;  
  7. import java.util.Map;  
  8.   
  9. /** 
  10.  * Hello动态代理类 代理对象与被代理对像解藕 
  11.  *  
  12.  * @author ydc 
  13.  * @date 6:56:27 PM Jul 31, 2013 
  14.  */  
  15. public class DynaProxyHello3 implements InvocationHandler {  
  16.     /** 
  17.      * 操作者 
  18.      */  
  19.     private Object proxy;  
  20.   
  21.     /** 
  22.      * 要处理的对象 
  23.      */  
  24.     private Object delegate;  
  25.   
  26.     /** 
  27.      * 动态生成方法被处理过后的对象(写法固定) 
  28.      *  
  29.      * @param delegate 
  30.      * @param proxy 
  31.      * @return Object 
  32.      */  
  33.     public Object bind(Object delegate, Object proxy) {  
  34.         this.delegate = delegate;  
  35.         this.proxy = proxy;  
  36.         return Proxy.newProxyInstance(  
  37.                 this.delegate.getClass().getClassLoader(), this.delegate  
  38.                         .getClass().getInterfaces(), this);  
  39.     }  
  40.   
  41.     /** 
  42.      * 要处理的对象中的每个方法会被此方法送去JVM调用,也就是说, 要处理的对象的方法只能通过此方法調用,此方法是动态的 
  43.      */  
  44.     @SuppressWarnings("unchecked")  
  45.     public Object invoke(Object proxy, Method method, Object[] args)  
  46.             throws Throwable {  
  47.         Object result = null;  
  48.         try {  
  49.             // 初始化配置文件  
  50.             Map<String, List<String>> aopMap = AopUtil.initConfig();  
  51.   
  52.             // 要执行的方法所在包与类名  
  53.             String clazzName = this.delegate.getClass().getName();  
  54.             String methodName = method.getName();  
  55.   
  56.             // 是否调用日志记录的标志  
  57.             boolean isOpen = false;  
  58.   
  59.             for (Map.Entry<String, List<String>> entry : aopMap.entrySet()) {  
  60.                 // 匹配方法所在的包名与类名  
  61.                 if (clazzName.equals(entry.getKey())) {  
  62.                     List<String> mList = entry.getValue();  
  63.                     if (mList != null) {  
  64.                         for (String m : mList) {  
  65.                             if (methodName.equals(m)) {  
  66.                                 isOpen = true;  
  67.                                 break;  
  68.                             }  
  69.                         }  
  70.                     }  
  71.                 }  
  72.             }  
  73.   
  74.             // 反射得到操作者的实例  
  75.             Class clazz = this.proxy.getClass();  
  76.             if (isOpen) {  
  77.                 // 反射得到操作者的start方法  
  78.                 Method start = clazz.getDeclaredMethod("start",  
  79.                         new Class[]{Method.class});  
  80.                 // 反射执行start方法  
  81.                 start.invoke(this.proxy, new Object[]{method});  
  82.             }  
  83.   
  84.             // JVM通过这条语句执行原来的方法(反射机制)  
  85.             result = method.invoke(this.delegate, args);  
  86.   
  87.             if (isOpen) {  
  88.                 // 反射得到操作者的end方法  
  89.                 Method end = clazz.getDeclaredMethod("end",  
  90.                         new Class[]{Method.class});  
  91.                 // 反射执行end方法  
  92.                 end.invoke(this.proxy, new Object[]{method});  
  93.             }  
  94.         } catch (Exception e) {  
  95.             e.printStackTrace();  
  96.         }  
  97.   
  98.         // 返回方法返回值给调用者  
  99.         return result;  
  100.   
  101.     }  
  102. }  

 

这样,再对上述代码进行测试:

Java代码   收藏代码
  1. package demo.aop;  
  2.   
  3. public class AopTest {  
  4.     public static void main(String[] args) {  
  5.         new AopTest().test4();  
  6.     }  
  7.     public void test4() {  
  8.         IHello hello = (IHello) new DynaProxyHello3().bind(new Hello(),  
  9.                 new LoggerOperation());  
  10.   
  11.         hello.sayHello("wallet white");  
  12.         System.out.println("------------------");  
  13.         hello.sayGoodBye("wallet white");  
  14.     }  
  15. }  

 结果输出:

Java代码   收藏代码
  1. INFO    Thu Aug 01 09:35:03 CST 2013    sayHellomethod start  
  2. hello wallet white  
  3. DEBUG   Thu Aug 01 09:35:03 CST 2013    sayHellomethod end  
  4. ------------------  
  5. wallet white goodbye!  

 在这里我们看到 Hello类内的sayGoodBye方法没有日志记录功能。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值