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中已經集成了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM