SpringBoot注入Service失败

Description:

The bean 'userService' could not be injected as a 'com.phy.hemanresoruce.service.UserService' 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.

根据上面描述,注入userServiceImpl这个Bean时失败,(失败的原因就是实现接口,而springboot的事务默认是使用jdk的动态代理,即基于接口))。在action层中注入的Bean是实现类,因此就会报错。

解决:将此注入Bean的方式改成了其接口

2.

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.

再看一下idea给的建议:可以考虑下将action中注入的Bean改成接口方式或者强迫事务使用CGLib代理方式(基于类,即设置proxyTargetClass=True在启动事务管理上@EnableTransactionManagement(proxyTargetClass=True)),这种以CGLib代理方式进行,按照之前写法,应该是需要引入相应的cglib库的jar包。而在springboot中已经集成了。

上一篇:关于如何在Listener中注入service和ServletContextListener源码分析


下一篇:spring boot测试类自动注入service或dao