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