Spring Boot 出現Consider defining a bean of type `com.xxx.xxx` in your configuration 錯誤解決

所用框架:

SSM+Spring Boot +Eureka +OpenFeign +Maven

完整報錯:

Consider defining a bean of type 'com.service.UserService' in your configuration.

上面這個報錯的意思是沒有找到UserService這個接口的實現類,需要定義一個UserService的實現類

原因分析和解決:

1、已經使用@Service註解定義了UserService的實現類,但是Spring沒有掃描到。

解決方法:

(1)將當前模塊的Dao類、Service類、Entity類、Controller類放在和XxxApplication啓動類同一目錄下或者子目錄下。

(2)在xxxApplication啓動類加上 @ComponentScan("com") 註解。

2、UserService啓動裝配時變量名默認爲實現類的名字userServiceImpl,實現類是UserServiceImpl類,而我們使用@Autowired註解時變量名爲userService。

解決辦法:

將實現類的@Service改爲@Service("userService")

3、使用了OpenFeign來調用其他模塊的接口,但是OpenFeign相關的jar包沒有完成導入。

解決辦法請參考以下博客

Feign 出現Failed to introspect Class [org.springframework.cloud.openfeign.FeignClientFactoryBean問題解決

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章