springboot 如何取消starter的自動注入
springboot 取消starter的自動注入
starer是spring boot中一個很重要的概念,starter相當(dāng)于一個模塊,它能將所需要的的依賴整合在一起并對模塊內(nèi)的bean自動裝配到spring IOC容器,使用者只需要在maven中依賴相應(yīng)的starter包并無需做過多的依賴即可進(jìn)行開發(fā)。
看例子
比如,我們導(dǎo)入了mybatis相關(guān)的依賴,但是我可能暫時沒用到數(shù)據(jù)庫,所以就沒有做數(shù)據(jù)庫相關(guān)的配置,這時候項目就會無法啟動
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中默認(rèn)的數(shù)據(jù)庫連接池是hikari,你沒有在application.properties里面進(jìn)行數(shù)據(jù)庫相關(guān)的配置的話,那么就會無法自動裝載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]
重點來了
如何取消相關(guān)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
配置文件如下
項目目錄
找了幾個類,該注解的也注解了。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
spring boot整合scurity做簡單的登錄校驗的實現(xiàn)
這篇文章主要介紹了spring boot整合scurity做簡單的登錄校驗的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04JVM調(diào)優(yōu)參數(shù)的設(shè)置
Java虛擬機(jī)的調(diào)優(yōu)是一個復(fù)雜而關(guān)鍵的任務(wù),可以通過多種參數(shù)來實現(xiàn),本文就來介紹一下JVM調(diào)優(yōu)參數(shù)的設(shè)置,具有一定的參考價值,感興趣的可以了解一下2024-03-03rocketmq的AclClientRPCHook權(quán)限控制使用技巧示例詳解
這篇文章主要為大家介紹了rocketmq的AclClientRPCHook使用技巧示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Java實現(xiàn)Twitter的分布式自增ID算法snowflake
這篇文章主要介紹了Java實現(xiàn)Twitter的分布式自增ID算法snowflake,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Apache Commons fileUpload實現(xiàn)文件上傳之一
這篇文章主要介紹了Apache Commons fileUpload實現(xiàn)文件上傳之一的相關(guān)資料,需要的朋友可以參考下2016-03-03