一直没去理解JDK代理和CGLib代理的区别,最近遇到了便记录下来。
背景
最近用到SpringBoot的异步注解(@Async),在一个接口有多个实现的情况下,其他地方使用子类来注入,如下:
@Resource
private TestServiceImpl testServiceImpl;
启动时报了下面的错误:
The bean 'billingInvoiceConverter' could not be injected as a 'com.testServiceImpl' because it is a JDK dynamic proxy that implements:
com.TestService
Action:
Consider injecting the bean as one of its interfaces or forcing the use of CGLib-based proxies by setting proxyTargetClass=true on @EnableAsync and/or @EnableCaching.
解决的办法这段话说的很清楚了,注入的时候应该由接口来接收,例如:
@Resource
private TestService testServiceImpl;
或者在开启异步的注解上添加强制使用CGLib,例如:
@EnableAsync(proxyTargetClass = true)
动态代理
首先JAVA常用的动态代理有JDK代理
和CGLib代理
两种。
- JDK代理
java动态代理是利用反射机制生成一个实现代理接口的匿名类,在调用具体方法前调用InvokeHandler来处理。
- CGLib代理
利用asm开源包,对代理对象类的class文件加载进来,通过修改其字节码生成子类来处理。
使用时有以下区别:
1、如果目标对象实现了接口,默认情况下会采用JDK的动态代理实现AOP
2、如果目标对象实现了接口,可以强制使用CGLIB实现AOP
3、如果目标对象没有实现了接口,必须采用CGLIB库,spring会自动在JDK动态代理和CGLIB之间转换
问题原因
点开@EnableAsync
注解的源码可以看到默认用的是JDK代理的方式,在注入TestServiceImpl
时会生成一个继承TestService
的代理类,假定为TestServiceProxy
。此时用TestServiceImpl
是无法接收TestServiceProxy
对象的,因为他俩同属于TestService
的子类。
如果将代理的方式改为CGLib,则在注入TestServiceImpl
时会生成一个继承TestServiceImpl
的代理类TestServiceProxy
。此时TestServiceProxy
属于TestServiceImpl
的子类,故能用TestServiceImpl
来接收TestServiceProxy
。
本文由 visionki 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: Apr 27, 2021 at 04:00 am