办法
<aop:aspectj-autoproxy/>
改为
<aop:aspectj-autoproxy proxy-target-class="true"/>
问题
2020-05-25 15:16:36,561 [main] ERROR (TestContextManager:234) - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@57db2b13] to prepare test instance [xxxRerankTest@30b34287]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xxxxRerankTest': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'DealRerankStrategy' is expected to be of type 'xxxRerankStrategyImpl.DealRerankStrategy' but was actually of type 'com.sun.proxy.$Proxy31'
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:321) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]
...
问题原因
Aspect的实现原理是基于接口的动态代理技术,而不是基于实现类的动态代理技术
<aop:aspectj-autoproxy />
有一个proxy-target-class
属性,默认为false,表示使用jdk动态代理织入增强,当配为<aop:aspectj-autoproxy poxy-target-class="true"/>
时,表示使用CGLib动态代理技术织入增强。不过即使proxy-target-class
设置为false,如果目标类没有声明接口,则spring将自动使用CGLib动态代理。
网友评论