springboot啟動的注意事項之不同包下有同樣名字的class類問題
springboot不同包下有同樣名字的class類
springboot 在啟動時候,常啟動不起來,檢查發(fā)現是不同包下面有同名的service和serviceImpl,按理說不同包下是可以有同名的類存在的,但是啟動就是啟動不了,報錯說
org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'roleServiceImpl' for bean class [com.example.service.RoleServiceImpl] conflicts with existing, non-compatible bean definition of same name and class [com.example.roleService.RoleServiceImpl]
意思是:
以Bean名字‘roleServiceImpl’注解的類[com.example.service.RoleServiceImpl]與存在的不相容的同名類[com.example.roleService.RoleServiceImpl]相沖突。
原來是在這兩個實現類上面都只用了@service這個注解,根據映射規(guī)則,這兩個service都映射為了roleServiceImpl,發(fā)生了沖突。
解決辦法
- 1.將其中一個實現類改為不同的名字;
- 2.將其中一個注解變更為一個name為非roleServiceImpl的注解@service(name="aaaa")。
再次啟動,OK。
springboot不同包下同名文件,啟動時報重名錯誤的解決
錯誤信息:
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.***.***.StarterApplication]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'discussController' for bean class [com.***.***.controller.wechat.DiscussController] conflicts with existing, non-compatible bean definition of same name and class [com.***.***.controller.web.DiscussController]
本人遇到這種問題,搞了半天,頭疼。
搜來的解決辦法
1:加:@Controller("rename") ,感覺太繁瑣。
2、重新定義Bean的命名策略,結果不起作用。
就請教了我司大牛,大牛一通操作,給出了完美解決方法,在這里感謝這位老師,也分享出來哈哈。
解決辦法
1、升級spring boot到2.2.7 升級spring到5.2.3以上
(我之前用的5.2.2,就差一個版本,就沒有FullyQualifiedAnnotationBeanNameGenerator)
2、StarterApplication中添加
@ComponentScan(value = {"com.**.**.spring", ……(此處可配置多個包)},
? ? ? ? nameGenerator = FullyQualifiedAnnotationBeanNameGenerator.class)完美!
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Springboot RabbitMQ 消息隊列使用示例詳解
本文通過示例代碼介紹了Springboot RabbitMQ 消息隊列使用,對大家的學習或工作具有一定的參考借鑒價值,感興趣的朋友跟隨小編一起看看吧2024-06-06
springboot執(zhí)行延時任務之DelayQueue實例
這篇文章主要介紹了springboot執(zhí)行延時任務之DelayQueue實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
Mac電腦如何通過?IntelliJ?IDEA?遠程連接?MySQL
本文詳解Mac通過IntelliJ?IDEA遠程連接MySQL的步驟,本文通過圖文并茂的形式給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧2025-08-08
Spring Cloud Gateway替代zuul作為API網關的方法
本文簡要介紹如何使用Spring Cloud Gateway 作為API 網關(不是使用zuul作為網關),結合實例代碼給大家詳細講解,感興趣的朋友跟隨小編一起看看吧2023-02-02

