java动态代理的两种方法

动态代理,有两种情况,第一种是有接口的情况下,你可以选择为jdk自带的动态代理的方式来编写程序,但你想要为一个实在的类编写动态代理的方式的话,这时候就必须选择一些开源的lib包,如cglib包,同时还需要asm包。 
cglib用于AOP,jdk中的proxy必须基于接口,cglib却没有这个限制。 

第一种通过jdk的动态代理(必须接口): 
主类(实现主要方法的类)接口: 

Java代码  收藏代码
  1. package bean;  
  2.   
  3. public interface TestInter {  
  4.     public void save();  
  5. }  
  6.   
  7. 具体类:  
  8.   
  9. package bean;  
  10.   
  11. public class TestClass implements TestInter{  
  12.     public void save(){  
  13.         System.out.println("调用TestClass.save()");  
  14.     }  
  15. }  


代理类: 

Java代码  收藏代码
  1. package bean;  
  2.   
  3. import java.lang.reflect.InvocationHandler;  
  4. import java.lang.reflect.Method;  
  5. import java.lang.reflect.Proxy;  
  6.   
  7. import org.apache.log4j.Logger;  
  8.   
  9. public class Test implements InvocationHandler {  
  10.   
  11.     private Object originalObject;  
  12.   
  13.     public Object bind(Object obj) {  
  14.     System.out.println("coming here...");  
  15.     this.originalObject = obj;  
  16.     return Proxy.newProxyInstance(  
  17.             obj.getClass().getClassLoader(),  
  18.             obj.getClass().getInterfaces(),this  
  19.         );  
  20.     }  
  21.   
  22.     /** 
  23.      * 反射? 
  24.      */  
  25.     public Object invoke(Object arg0, Method arg1, Object[] arg2) throws Throwable {  
  26.         Object result=null;  
  27.         if(arg1.getName().startsWith("save")){  
  28.                 System.out.println("start...");  
  29.                 result=arg1.invoke(this.originalObject,arg2);  
  30.                 System.out.println("end...");  
  31.         }  
  32.         return result;  
  33.     }  
  34. }  


测试类: 

Java代码  收藏代码
  1. package bean;  
  2. public class TestMain {  
  3.   
  4.     /** 
  5.      * @param args 
  6.      */  
  7.     public static void main(String[] args) {  
  8.         Test test=new Test();  
  9.         TestClass tc=new TestClass();     
  10.         try{  
  11.             ((TestInter)test.bind(tc)).save();  
  12.         }catch(Exception e){  
  13.             System.out.println(e.getMessage());  
  14.             e.printStackTrace();  
  15.               
  16.         }         
  17.   
  18.     }  
  19.   
  20. }  


运行结果: 

Java代码  收藏代码
  1. coming here...  
  2. start...  
  3. 调用TestClass.save()  
  4. end...  





第二种方法: 

主类(实现主要方法的类): 

Java代码  收藏代码
  1. package cglib;  
  2. public class TestClass {  
  3.     public void save(){  
  4.         System.out.println("调用TestClass.save()");  
  5.     }  
  6. }  



拦截器类(实现功能的地方): 

Java代码  收藏代码
  1. package cglib;  
  2.   
  3. import java.lang.reflect.Method;  
  4.   
  5. import net.sf.cglib.proxy.MethodInterceptor;  
  6. import net.sf.cglib.proxy.MethodProxy;  
  7. /** 
  8.  * 实现接口MethodInterceptor 
  9.  */  
  10. public class MyMethodInterceptor implements MethodInterceptor {  
  11.   
  12.       
  13.     /** 
  14.      * 拦截器,在这里实现需要的功能 
  15.      * 在这里仅仅是在执行之前打印了start 在执行之后打印了end 
  16.      */  
  17.     public Object intercept(Object arg0, Method arg1, Object[] arg2,  
  18.             MethodProxy arg3) throws Throwable {  
  19.            System.out.println("start...");  
  20.            Object result = arg3.invokeSuper(arg0,arg2);  
  21.            System.out.println("ending...");  
  22.            return result;   
  23.     }  
  24.   
  25. }  



创建代理的类: 

Java代码  收藏代码
  1. package cglib;  
  2.   
  3. import net.sf.cglib.proxy.Enhancer;  
  4.   
  5. public class TestProxy {  
  6.     /** 
  7.      * 创建代理类 
  8.      * @param targetClass 
  9.      * @return 
  10.      */  
  11.     public Object createProxy(Class targetClass){  
  12.         Enhancer enhancer = new Enhancer();  
  13.         //设定父类???  
  14.         enhancer.setSuperclass(targetClass);  
  15.         //这里貌似是进行回调,主要的操作被放进了MyMethodInterceptor类里  
  16.         enhancer.setCallback(new MyMethodInterceptor());  
  17.         return enhancer.create();  
  18.     }   
  19. }  



测试类 

Java代码  收藏代码
  1. package cglib;  
  2.   
  3. public class TestMain {  
  4.   
  5.     /** 
  6.      * 测试类 
  7.      * @param args 
  8.      */  
  9.     public static void main(String[] args) {  
  10.         TestClass tc=new TestClass();  
  11.         TestProxy tp=new TestProxy();  
  12.           
  13.         TestClass tcp=(TestClass)tp.createProxy(tc.getClass());  
  14.         tcp.save();  
  15.   
  16.     }  
  17.   
  18. }  



运行结果: 

Java代码  收藏代码
    1. start...  
    2. 调用TestClass.save()  
    3. ending...  
原文地址:https://www.cnblogs.com/baizhanshi/p/6394225.html