springboot 如何取消starter的自動注入
springboot 取消starter的自動注入
starer是spring boot中一個很重要的概念,starter相當于一個模塊,它能將所需要的的依賴整合在一起并對模塊內的bean自動裝配到spring IOC容器,使用者只需要在maven中依賴相應的starter包并無需做過多的依賴即可進行開發(fā)。
看例子
比如,我們導入了mybatis相關的依賴,但是我可能暫時沒用到數(shù)據(jù)庫,所以就沒有做數(shù)據(jù)庫相關的配置,這時候項目就會無法啟動
2020-03-08 22:13:10.396 WARN 10692 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource
[org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate
[com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception;
nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
因為springboot中默認的數(shù)據(jù)庫連接池是hikari,你沒有在application.properties里面進行數(shù)據(jù)庫相關的配置的話,那么就會無法自動裝載dataSource
Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]
重點來了
如何取消相關starer的自動注入?
我們還是以數(shù)據(jù)庫的這個為例子:
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
那么,就需要在啟動類加上如上配置,取消DataSourceAutoConfiguration的自動注入
springbootApplication是一個組合注解,其實里面真正實現(xiàn)自動注入功能的,是這個EnableAutoConfiguration注解
SpringBoot 自動注入問題
Description:
Field service in com.test.controller.UserController required a bean of type 'com.test.service.UserService' that could not be found.
Action:
Consider defining a bean of type 'com.test.service.UserService' in your configuration.
項目啟動的時候出現(xiàn)出現(xiàn)問題
run
controller
service
dao
配置文件如下
項目目錄
找了幾個類,該注解的也注解了。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
spring boot整合scurity做簡單的登錄校驗的實現(xiàn)
這篇文章主要介紹了spring boot整合scurity做簡單的登錄校驗的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04rocketmq的AclClientRPCHook權限控制使用技巧示例詳解
這篇文章主要為大家介紹了rocketmq的AclClientRPCHook使用技巧示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08Java實現(xiàn)Twitter的分布式自增ID算法snowflake
這篇文章主要介紹了Java實現(xiàn)Twitter的分布式自增ID算法snowflake,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08Apache Commons fileUpload實現(xiàn)文件上傳之一
這篇文章主要介紹了Apache Commons fileUpload實現(xiàn)文件上傳之一的相關資料,需要的朋友可以參考下2016-03-03