Java SpringBoot自動(dòng)配置原理詳情
SpringBoot的底層注解
首先了解一些SpringBoot的底層注解,是如何完成相關(guān)的功能的
@Configuration 告訴SpringBoot被標(biāo)注的類(lèi)是一個(gè)配置類(lèi),以前Spring xxx.xml能配置的內(nèi)容,它都可以做,spring中的Bean組件默認(rèn)是單實(shí)例的
#############################Configuration使用示例######################################################
/**
* 1、配置類(lèi)里面使用@Bean標(biāo)注在方法上給容器注冊(cè)組件,默認(rèn)也是單實(shí)例的
* 2、配置類(lèi)本身也是組件
* 3、proxyBeanMethods:代理bean的方法
* Full(proxyBeanMethods = true)、【保證每個(gè)@Bean方法被調(diào)用多少次返回的組件都是單實(shí)例的】
* Lite(proxyBeanMethods = false)【每個(gè)@Bean方法被調(diào)用多少次返回的組件都是新創(chuàng)建的】
* 組件依賴(lài)必須使用Full模式默認(rèn)。其他默認(rèn)是否Lite模式
*
*
*
*/
@Configuration(proxyBeanMethods = false) //告訴SpringBoot這是一個(gè)配置類(lèi) == 配置文件
public class MyConfig {
/**
* Full:外部無(wú)論對(duì)配置類(lèi)中的這個(gè)組件注冊(cè)方法調(diào)用多少次獲取的都是之前注冊(cè)容器中的單實(shí)例對(duì)象
* @return
*/
@Bean //給容器中添加組件。以方法名作為組件的id。返回類(lèi)型就是組件類(lèi)型。返回的值,就是組件在容器中的實(shí)例
public User user01(){
User zhangsan = new User("zhangsan", 18);
//user組件依賴(lài)了Pet組件
zhangsan.setPet(tomcatPet());
return zhangsan;
}
@Bean("tom")
public Pet tomcatPet(){
return new Pet("tomcat");
}
}
################################@Configuration測(cè)試代碼如下########################################
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan("com.atguigu.boot")
public class MainApplication {
public static void main(String[] args) {
//1、返回我們IOC容器
ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
//2、查看容器里面的組件
String[] names = run.getBeanDefinitionNames();
for (String name : names) {
System.out.println(name);
}
//3、從容器中獲取組件
Pet tom01 = run.getBean("tom", Pet.class);
Pet tom02 = run.getBean("tom", Pet.class);
System.out.println("組件:"+(tom01 == tom02));
//4、com.atguigu.boot.config.MyConfig$$EnhancerBySpringCGLIB$$51f1e1ca@1654a892
MyConfig bean = run.getBean(MyConfig.class);
System.out.println(bean);
//如果@Configuration(proxyBeanMethods = true)代理對(duì)象調(diào)用方法。SpringBoot總會(huì)檢查這個(gè)組件是否在容器中有。
//保持組件單實(shí)例
User user = bean.user01();
User user1 = bean.user01();
System.out.println(user == user1);
User user01 = run.getBean("user01", User.class);
Pet tom = run.getBean("tom", Pet.class);
System.out.println("用戶(hù)的寵物:"+(user01.getPet() == tom));
}
}@Import注解詳解,將指定組件導(dǎo)入到容器中
* 4、@Import({User.class, DBHelper.class})
* 給容器中自動(dòng)創(chuàng)建出這兩個(gè)類(lèi)型的組件、默認(rèn)組件的名字就是全類(lèi)名
*
*
*
*/
@Import({User.class, DBHelper.class})
@Configuration(proxyBeanMethods = false) //告訴SpringBoot這是一個(gè)配置類(lèi) == 配置文件
public class MyConfig {
}@Conditional注解詳解,條件裝配,必須滿足Conditional指定的條件,才會(huì)繼續(xù)組件的注入

以上是所有Conditional的實(shí)現(xiàn)注解
@ConditionalOnBean(name = "tom") 容器中有tom組件,才會(huì)進(jìn)行組件的注入
=====================測(cè)試條件裝配==========================
@Configuration(proxyBeanMethods = false) //告訴SpringBoot這是一個(gè)配置類(lèi) == 配置文件
//@ConditionalOnBean(name = "tom")
@ConditionalOnMissingBean(name = "tom")
public class MyConfig {
/**
* Full:外部無(wú)論對(duì)配置類(lèi)中的這個(gè)組件注冊(cè)方法調(diào)用多少次獲取的都是之前注冊(cè)容器中的單實(shí)例對(duì)象
* @return
*/
@Bean //給容器中添加組件。以方法名作為組件的id。返回類(lèi)型就是組件類(lèi)型。返回的值,就是組件在容器中的實(shí)例
public User user01(){
User zhangsan = new User("zhangsan", 18);
//user組件依賴(lài)了Pet組件
zhangsan.setPet(tomcatPet());
return zhangsan;
}
@Bean("tom22")
public Pet tomcatPet(){
return new Pet("tomcat");
}
}
public static void main(String[] args) {
//1、返回我們IOC容器
ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
//2、查看容器里面的組件
String[] names = run.getBeanDefinitionNames();
for (String name : names) {
System.out.println(name);
}
boolean tom = run.containsBean("tom");
System.out.println("容器中Tom組件:"+tom);
boolean user01 = run.containsBean("user01");
System.out.println("容器中user01組件:"+user01);
boolean tom22 = run.containsBean("tom22");
System.out.println("容器中tom22組件:"+tom22);
}@ImportResource導(dǎo)入資源注解,一些老的項(xiàng)目還是會(huì)有xml配置的文件,該注解用于將這些xml文件導(dǎo)入進(jìn)來(lái)
======================beans.xml=========================
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="haha" class="com.atguigu.boot.bean.User">
<property name="name" value="zhangsan"></property>
<property name="age" value="18"></property>
</bean>
<bean id="hehe" class="com.atguigu.boot.bean.Pet">
<property name="name" value="tomcat"></property>
</bean>
</beans>@ImportResource("classpath:beans.xml")
public class MyConfig {}
======================測(cè)試=================
boolean haha = run.containsBean("haha");
boolean hehe = run.containsBean("hehe");
System.out.println("haha:"+haha);//true
System.out.println("hehe:"+hehe);//true配置綁定
使用java讀取到properties文件中的內(nèi)容,并且把它封裝到j(luò)avaBean中,以供隨時(shí)使用
public class getProperties {
public static void main(String[] args) throws FileNotFoundException, IOException {
Properties pps = new Properties();
pps.load(new FileInputStream("a.properties"));
Enumeration enum1 = pps.propertyNames();//得到配置文件的名字
while(enum1.hasMoreElements()) {
String strKey = (String) enum1.nextElement();
String strValue = pps.getProperty(strKey);
System.out.println(strKey + "=" + strValue);
//封裝到JavaBean。
}
}
}@ConfigurationProperties 將properties里的內(nèi)容綁定到對(duì)應(yīng)的屬性當(dāng)中
只有在容器中的組件,才會(huì)擁有SpringBoot提供的強(qiáng)大的功能
/**
* 只有在容器中的組件,才會(huì)擁有SpringBoot提供的強(qiáng)大功能
*/
@Component
@ConfigurationProperties(prefix = "mycar")
public class Car {
private String brand;
private Integer price;
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 +
'}';
}
}@EnableConfigurationProperties(Car.class)(開(kāi)啟car配置綁定功能,把這個(gè)car這個(gè)組件自動(dòng)注冊(cè)到容器中)
@ConfigurationProperties
自動(dòng)配置原理入門(mén)
核心注解@SpringBootApplication相當(dāng)于三個(gè)注解,@SpringBootConfiguration、
@EnableAutoConfiguration、@ComponentScan
- @SpringBootConfiguration:@Configuration。代表當(dāng)前是一個(gè)配置類(lèi)
- @ComponentScan:指定掃描范圍
- @EnableAutoConfiguration:核心注解,它也是一個(gè)核心注解,它里面包括@AutoConfigurationPackage和
- @Import(AutoConfigurationImportSelector.class)
- @AutoConfigurationPackage:內(nèi)部是一個(gè)Import注解,就是給容器中導(dǎo)入組件
@Import(AutoConfigurationPackages.Registrar.class) ?//給容器中導(dǎo)入一個(gè)組件
public @interface AutoConfigurationPackage {}
//利用Registrar給容器中導(dǎo)入一系列組件
//將指定的一個(gè)包下的所有組件導(dǎo)入進(jìn)來(lái),MainApplication 所在包下。@Import(AutoConfigurationImportSelector.class)
- 1、利用getAutoConfigurationEntry(annotationMetadata);給容器中批量導(dǎo)入一些組件
- 2、調(diào)用List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes)獲取到所有需要導(dǎo)入到容器中的配置類(lèi)
- 3、利用工廠加載 Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader);得到所有的組件
- 4、從META-INF/spring.factories位置來(lái)加載一個(gè)文件。
默認(rèn)掃描我們當(dāng)前系統(tǒng)里面所有META-INF/spring.factories位置的文件
spring-boot-autoconfigure-2.3.4.RELEASE.jar包里面也有META-INF/spring.factories
到此這篇關(guān)于Java SpringBoot自動(dòng)配置原理詳情的文章就介紹到這了,更多相關(guān)Java SpringBoot自動(dòng)配置 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Springboot自動(dòng)配置與@Configuration配置類(lèi)詳解
- SpringBoot自動(dòng)配置實(shí)現(xiàn)的詳細(xì)步驟
- SpringBoot自動(dòng)配置原理詳解
- SpringBoot自動(dòng)配置Quartz的實(shí)現(xiàn)步驟
- springboot自動(dòng)配置原理以及spring.factories文件的作用詳解
- springboot自動(dòng)配置原理解析
- 淺談springboot自動(dòng)配置原理
- SpringBoot自動(dòng)配置的實(shí)現(xiàn)原理
- Springboot的自動(dòng)配置是什么及注意事項(xiàng)
相關(guān)文章
在idea2023中使用SpringBoot整合Lombok全過(guò)程及詳細(xì)用法
Lombok項(xiàng)目是一個(gè)java庫(kù),它可以自動(dòng)插入到編輯器和構(gòu)建工具中,增強(qiáng)java的性能,本文詳細(xì)給大家介紹了在idea2023中使用SpringBoot整合Lombok全過(guò)程及詳細(xì)用法,需要的朋友可以參考下2023-09-09
Java listener簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java listener簡(jiǎn)介,可以用于統(tǒng)計(jì)用戶(hù)在線人數(shù)等,有興趣的可以了解一下2017-07-07
SpringCloud自定義loadbalancer實(shí)現(xiàn)標(biāo)簽路由的詳細(xì)方案
本文介紹了通過(guò)標(biāo)簽路由解決前端開(kāi)發(fā)環(huán)境接口調(diào)用慢的問(wèn)題,實(shí)現(xiàn)方案包括在本地服務(wù)注冊(cè)元數(shù)據(jù)、自定義負(fù)載均衡器、以及網(wǎng)關(guān)配置等步驟,通過(guò)環(huán)境變量設(shè)置標(biāo)簽,網(wǎng)關(guān)根據(jù)請(qǐng)求頭中的標(biāo)簽進(jìn)行路由,從而實(shí)現(xiàn)前后端互不干擾的開(kāi)發(fā)調(diào)試,感興趣的朋友一起看看吧2025-02-02
java序列化和serialVersionUID的使用方法實(shí)例
這篇文章主要介紹了java序列化和serialVersionUID的使用方法實(shí)例的相關(guān)資料,這里說(shuō)明很詳細(xì)的使用方法讓你徹底學(xué)會(huì),需要的朋友可以參考下2017-08-08
mybatis-plus 擴(kuò)展批量新增的實(shí)現(xiàn)
本文主要介紹了mybatis-plus 擴(kuò)展批量新增的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
MyBatis直接執(zhí)行SQL的工具SqlMapper
今天小編就為大家分享一篇關(guān)于MyBatis直接執(zhí)行SQL的工具SqlMapper,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
Java?輪詢(xún)鎖使用時(shí)遇到問(wèn)題解決方案
這篇文章主要介紹了Java?輪詢(xún)鎖使用時(shí)遇到問(wèn)題解決方案,當(dāng)我們遇到死鎖之后,除了可以手動(dòng)重啟程序解決之外,還可以考慮使用順序鎖和輪詢(xún)鎖,但是過(guò)程也會(huì)遇到一些問(wèn)題,接下來(lái)我們一起進(jìn)入下面文章了解解決方案,需要的小伙伴可以參考一下2022-05-05
解決idea中debug工具欄消失后如何顯示的問(wèn)題
這篇文章主要介紹了解決idea中debug工具欄消失后如何顯示的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02

