cglib动态代理

JDK实现动态代理需要实现类通过接口定义业务方法,对于没有接口的类,如何实现动态代理呢,这就需要CGLib了。CGLib采用了非常底层的字节码技术,其原理是通过字节码技术为一个类创建子类,并在子类中采用方法拦截的技术拦截所有父类方法的调用,顺势织入横切逻辑,所以用final修饰的方法将不能实现代理。同样我们采用jdk动态案例分析说明




1、创建实体类

  1. package www.csdn.spring.cglib.proxy;  
  2.   
  3. //真实主题角色  
  4.   
  5. public class SayGoodByeImpl {  
  6.   
  7.   
  8. public void say(String content) {  
  9.   
  10. System.out.println("say:" + content);  
  11.   
  12.   
  13. }  
  14.   
  15. }  

2、测试说明:

  1. package www.csdn.spring.cglib.proxy;  
  2.   
  3.   
  4. import org.junit.Test;  
  5.   
  6.   
  7. public class TestSay {  
  8.   
  9.   
  10. @Test  
  11.   
  12. public void say(){  
  13.   
  14.   
  15. //真实主题角色  
  16.   
  17. SayGoodByeImpl sayGoodByeImpl = new SayGoodByeImpl();  
  18.   
  19.   
  20. //自己和女朋友说  
  21.   
  22. sayGoodByeImpl.say("咱们分手吧!");  
  23.   
  24.   
  25. }  
  26.   
  27. }  

 

3、采用cglib代理实现

  1. package www.csdn.spring.cglib.proxy;  
  2.   
  3.   
  4. import java.lang.reflect.Method;  
  5.   
  6.   
  7. import net.sf.cglib.proxy.Enhancer;  
  8.   
  9. import net.sf.cglib.proxy.MethodInterceptor;  
  10.   
  11. import net.sf.cglib.proxy.MethodProxy;  
  12.   
  13.   
  14. public class CglibProxy implements MethodInterceptor {  
  15.   
  16.   
  17. // 目标对象  
  18.   
  19. private Object target;  
  20.   
  21.   
  22. // 创建代理对象  
  23.   
  24. public Object createProxyInstance(Object target) {  
  25.   
  26. this.target = target;  
  27.   
  28. Enhancer enhancer = new Enhancer(); // 用于生成代理对象的  
  29.   
  30. enhancer.setSuperclass(this.target.getClass());// 用于设置代理对象的父类  
  31.   
  32. enhancer.setCallback(this); // 设置回调  
  33.   
  34. return enhancer.create(); // 创建代理对象  
  35.   
  36. };  
  37.   
  38.   
  39. /** 
  40.  
  41.  * proxy:目标对象代理的实例 method:目标对象调用父类方法的method实例 args:调用父类方法传递参数 
  42.  
  43.  * methodProxy:代理的方法去调用目标方法 
  44.  
  45.  */  
  46.   
  47. @Override  
  48.   
  49. public Object intercept(Object proxy, Method method, Object[] args,  
  50.   
  51. MethodProxy methodProxy) throws Throwable {  
  52.   
  53.   
  54. Object returnValue = null;  
  55.   
  56.   
  57. // 目标方法执行之前的操作  
  58.   
  59. beforeMethod();  
  60.   
  61. // 执行目标方法  
  62.   
  63. returnValue = methodProxy.invoke(target, args);  
  64.   
  65. // 目标方法执行之后的操作  
  66.   
  67. afterMethod();  
  68.   
  69.   
  70. return returnValue;  
  71.   
  72. }  
  73.   
  74.   
  75. public void beforeMethod() {  
  76.   
  77. System.out.println("---------------------目标方法之前执行");  
  78.   
  79. }  
  80.   
  81.   
  82. public void afterMethod() {  
  83.   
  84. System.out.println("---------------------目标方法之后执行");  
  85.   
  86. }  
  87.   
  88.   
  89. }  

4、测试说明:

  1. package www.csdn.spring.cglib.proxy;  
  2.   
  3.   
  4. import org.junit.Test;  
  5.   
  6.   
  7. public class TestSay {  
  8.   
  9.   
  10. @Test  
  11.   
  12. public void say(){  
  13.   
  14.   
  15. //真实主题角色  
  16.   
  17. SayGoodByeImpl sayGoodByeImpl = new SayGoodByeImpl();  
  18.   
  19.   
  20. //代理角色  
  21.   
  22. CglibProxy cglibProxy = new CglibProxy();  
  23.   
  24. //创建了一个 目标类的子类的对象    
  25.   
  26. SayGoodByeImpl proxy = (SayGoodByeImpl) cglibProxy.createProxyInstance(sayGoodByeImpl);  
  27.   
  28.   
  29. //代理说  
  30.   
  31. proxy.say("你和他分手吧!");  
  32.   
  33.   
  34. }  
  35.   


CGLIB是一个强大的开源代码生成库,它可以在运行时扩展Java类和实现接口。CGLIB动态代理CGLIB库的一个重要特性,它通过生成目标类的子类来实现代理。 在CGLIB动态代理中,我们可以使用`MethodInterceptor`接口来定义代理逻辑。`MethodInterceptor`接口有一个`intercept`方法,该方法在目标方法被调用时被触发。在`intercept`方法中,我们可以编写自定义的逻辑来增强目标方法的功能。 下面是一个使用CGLIB动态代理的示例代码: ```java import net.sf.cglib.proxy.Enhancer; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; import java.lang.reflect.Method; public class CglibProxyExample implements MethodInterceptor { public Object createProxy(Object target) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(target.getClass()); enhancer.setCallback(this); return enhancer.create(); } @Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { // 在目标方法执行前的逻辑 System.out.println("Before method: " + method.getName()); // 调用目标方法 Object result = proxy.invokeSuper(obj, args); // 在目标方法执行后的逻辑 System.out.println("After method: " + method.getName()); return result; } } ``` 在上述示例中,我们首先创建了一个`CglibProxyExample`类,实现了`MethodInterceptor`接口。然后,我们通过`Enhancer`类创建了一个代理对象,并设置了目标类和代理逻辑。在`intercept`方法中,我们可以在目标方法执行前后添加自定义的逻辑。 使用CGLIB动态代理时,我们可以通过调用`createProxy`方法来创建代理对象。例如: ```java SomeClass target = new SomeClass(); CglibProxyExample proxyExample = new CglibProxyExample(); SomeClass proxy = (SomeClass) proxyExample.createProxy(target); ``` 这样,我们就可以通过`proxy`对象来调用目标类的方法,并在方法执行前后添加自定义的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值