時間:2024-02-05 12:47作者:下載吧人氣:17
sprint boot2.0啟動的時候報錯!
A component required a bean of type ‘XXX’ that could not be found.
就是沒有掃描到我的repository包里的代碼
我先用@ComponentScan注解加上類所在的包名,沒有報錯,可以正常啟動
但是坑爹的是@RestController注解下的Controller層的代碼沒有掃描到
就是說http://127.0.0.1:8080可以正常訪問,但是Controller層配置的@RequestMapping都匹配不到
折騰了好久好久,比如@ComponentScan在加上Controller層的包名還是不行(導致repository包掃描不到),使用包名.*模糊匹配也不行,好坑爹,說好的比spring mvc配置要簡單的呢
最后我盯上了@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)注解
這個是因為最開始的時候項目啟動報錯沒有數據庫相關配置
然后我刪掉了注解里的exclude,開始加上一些數據庫配置,比如
spring.datasource.url=jdbc:mysql://xxx.x.x.x/favorites?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver
網友評論