SpringBoot自動配置實現(xiàn)流程詳細分析
第一種
給容器中的組件加上
@ConfigurationProperties注解即可
測試:
@Component @ConfigurationProperties(prefix = "mycar") public class Car { private String brand; private Integer price; private Integer seatNum; public Integer getSeatNum() { return seatNum; } public void setSeatNum(Integer seatNum) { this.seatNum = seatNum; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public Integer getPrice() { return price; } public void setPrice(Integer price) { this.price = price; } @Override public String toString() { return "Car{" + "brand='" + brand + '\'' + ", price=" + price + ", seatNum=" + seatNum + '}'; } public Car() { } }
在application.properties中屬性:
mycar.seatNum = 4
mycar.brand = BMW
mycar.price = 100000
即可給之后new 的Car 對象自動配置。
運行:
public class MainApplication { public static void main(String[] args) { //返回springboot中的ioc容器 ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args); Car car = run.getBean("car", Car.class); System.out.println(car); } }
控制臺結果:
第二種
第一種的情況下是自己寫的類作為組件,實現(xiàn)自動裝配的過程;
但有時候使用第三方類的時候無法將其設置為自己的組件,所以就需要用
@EnableConfigurationProperties + @ConfigurationProperties
將Car類刪除@Component注解,此時Car類已經(jīng)不是組件了:
11 usages @ConfigurationProperties(prefix = "mycar " ) public class Car { 3 usages private String brand ; 3 usages private Integer price ; 3 usages
此時,假設Car是第三方提供的類:
對于第三方的類 想要其作為組件就需要@Bean注解,就和之前的SSM項目中配置的bean
標簽一樣:
SSM中的配置文件中:
<bean id="car" class="xxx.xxx.xxx.Car"> <property name="brand" value=""/> <property name="price" value=" "/> <property name="seatNum" value=" "/> </bean>
就等同于SpringBoot中配置類下的:
@Bean public Car car(){ Car car = new Car(); return car; }
其中屬性的賦值就需要在Car類上增加
@ConfigurationProperties(prefix = "mycar")注解
最后在該配置類上使用
@EnableConfigurationProperties(Car.class)注解開啟即可
@Configuration(proxyBeanMethods = false) @EnableConfigurationProperties(Car.class) public class CarAutoConfiguration { @Bean public Car car(){ Car car = new Car(); return car; } }
控制臺顯示結果一樣:
到此這篇關于SpringBoot自動配置實現(xiàn)流程詳細分析的文章就介紹到這了,更多相關SpringBoot自動配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
解決MultipartFile.transferTo(dest) 報FileNotFoundExcep的問題
這篇文章主要介紹了解決MultipartFile.transferTo(dest) 報FileNotFoundExcep的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07SpringBoot的配置文件application.yml及加載順序詳解
這篇文章主要介紹了SpringBoot的配置文件application.yml及加載順序,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07logback的AsyncAppender高效日志處理方式源碼解析
這篇文章主要為大家介紹了logback的AsyncAppender高效日志處理方式源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10一文詳細springboot實現(xiàn)MySQL數(shù)據(jù)庫的整合步驟
Spring Boot可以很方便地與MySQL數(shù)據(jù)庫進行整合,下面這篇文章主要給大家介紹了關于springboot實現(xiàn)MySQL數(shù)據(jù)庫的整合步驟,文中通過圖文以及代碼介紹的非常詳細,需要的朋友可以參考下2024-03-03JavaFX程序初次運行創(chuàng)建數(shù)據(jù)庫并執(zhí)行建表SQL詳解
這篇文章主要介紹了JavaFX程序初次運行創(chuàng)建數(shù)據(jù)庫并執(zhí)行建表SQL詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08詳解Java編程規(guī)約(命名風格、常量定義、代碼格式)
這篇文章主要介紹了詳解Java編程規(guī)約(命名風格、常量定義、代碼格式),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-10-10