JDK动态代理与cglib代理

错误:如下
The bean 'resSourceServiceImpl' could not be injected as a 'com.hy.service.impl.ResSourceServiceImpl' because it is a JDK dynamic proxy that implements:
    com.hy.service.ResSourceService
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.
这个resSourceServiceImpl类不能被注入com.hy.service.impl.ResSourceServiceImpl'因为它是一个JDK动态代理实现:, 考虑注入bean作为它的接口之一,或者通过在@EnableAsync或@EnableCaching上设置proxyTargetClass=true来强制使用基于cglib的代理。
这个类好好的,是因为我加了一个@Transactional事务注解,加了事务注解就会使用AOP,因此就会使用cglib代理。那为什么这个类会是JDK动态代理呢。经过项目排查。发现在使用这个实现类时注入时使用了
@Autowired
private ResSourceServiceImpl resSourceServiceImpl;
这样就会使用JDK动态代理。改为:
@Autowired
private ResSourceService resSourceServiceImpl;
就会使用cglib动态代理了。

猜你喜欢

转载自blog.csdn.net/weixin_43075758/article/details/120016028