Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-08-01 09:37:14.845 ERROR 12264 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'AdapterSchemeVersionService' could not be injected as a 'com.yihu.hos.rest.services.standard.adapter.AdapterSchemeVersionService' because it is a JDK dynamic proxy that implements:
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.
出现这种异常可能有两个原因:
1.使用spring-boot做事务管理时,出现异常:The bean 'xxx' could not be injected as a 'xx.xxxx' because it is a JDK dynamic proxy that implements:
搞了半天发现是因为代理的原因
解决方法:
① 使用 @Transactional
开启@Transactional 注解支持,两种方式,一种是通过xml的方式
<annotation-driven transaction-manager="txManager">
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
</annotation-driven>
② 另外一种是使用@EnableTransactionManagement,将该注解标注在@Configuration类上,等价于上面的<tx:annotation-driven/>
Spring推荐奖该注解标记在类(方法)而不是接口,将注解标记在接口上时,只有使用动态代理的时候才会生效,需要标记proxy-target-class="true"或者mode="aspectj",如下:
@RestController
@SpringBootApplication
@EnableCaching
/**
* 在 SpringBootApplication 上使用@ServletComponentScan 注解后,Servlet、Filter、Listener
* 可以直接通过 @WebServlet、@WebFilter、@WebListener 注解自动注册,无需其他代码。
* */
@ServletComponentScan
@EnableScheduling
@EnableTransactionManagement(proxyTargetClass = true)
@EnableSwagger2
public class App {
}
- CGLib 方式实现AOP代理问题
当我们需要使用CGLIB来实现AOP的时候,需要配置spring.aop.proxy-target-class=true,不然默认使用的是标准Java的实现,就会报错。
解决方法:在yml文件中加入
spring:
aop:
proxy-target-class: true
网友评论