Consider injecting the bean as one of its interfaces or forcing the use of CGLib-based proxies by se

Posted 微wx笑

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Consider injecting the bean as one of its interfaces or forcing the use of CGLib-based proxies by se相关的知识,希望对你有一定的参考价值。

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.

问题描述

Spring Boot 项目,启用事务支持时出现以下错误:

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.

2018-12-28 09:19:09.467 ERROR 1105 --- [          main] o.s.b.d.LoggingFailureAnalysisReporter  : 

***************************

APPLICATION FAILED TO START

***************************

Description:

The bean 'departmentDao' could not be injected as a 'com.wanyu.fams.dao.DepartmentDao' 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.

相关代码

/**
 * Hello world!
 * @SpringBootApplication注释是相当于使用 @Configuration,@EnableAutoConfiguration,和@ComponentScan 会去拿起在所有的配置类和属于所述类的包。
 */
@SpringBootApplication
@ServletComponentScan // 不使用此注解导致项目访问后出现死循环,估计和Filter有关:at
						// com.wanyu.frame.base.AuthFilter.doFilter(AuthFilter.java:71)
						// ~[classes/:na]
// @Configuration
// @EnableAutoConfiguration
// @EnableWebMvc // 启用后导致 /loginback 等都返回 404
@EnableTransactionManagement  // 启用事务处理
// @MapperScan("com.wanyu.fams.dao")
// @ComponentScan
// @EnableAspectJAutoProxy  不需要此注解,带有@Aspect注解的类就会被织入
// @MapperScan(basePackages = "com.wanyu.fams.mapping")
@EnableAsync
public class FamsApplication extends SpringBootServletInitializer 

也就是添加了一个注解:@EnableTransactionManagement  // 启用事务处理,启动的时候就报错了。

解决方法

在 application.properties 或者 application.yml 配置文件中添加配置:

以下为 application.properties 中的配置方法:

#解决问题
#Description:
#The bean 'departmentDao' could not be injected as a 'com.wanyu.fams.dao.DepartmentDao' 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.
spring.aop.proxy-target-class=true

 application.yml 写法有点不同:

spring:
  aop:
    proxy-target-class: true

 

失败的尝试

文章指出:在springboot启动类中添加如下代码,最终解决问题,我尝试失败,错误依旧!

@Bean
    @ConditionalOnMissingBean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() 
        DefaultAdvisorAutoProxyCreator daap = new DefaultAdvisorAutoProxyCreator();
        daap.setProxyTargetClass(true);
        return daap;
    

相关参考:

springboot CGLib 方式实现AOP代理问题

还有另外一种可能是:启动类位置不在同级包下

 

以上是关于Consider injecting the bean as one of its interfaces or forcing the use of CGLib-based proxies by se的主要内容,如果未能解决你的问题,请参考以下文章

consider increasing the maximum size of the cache.

consider increasing the maximum size of the cache

Tomcat 警告:consider increasing the maximum size of the cache

消除: warning C4996: 'sprintf': This function or variable may be unsafe. Consider 的方法

pip install报错,“Consider using the `--user` option or check the permissions”

Python提示:Consider using the `--user` option or check the permissions.