Java SpringBoot自動配置原理詳情
SpringBoot的底層注解
首先了解一些SpringBoot的底層注解,是如何完成相關的功能的
@Configuration 告訴SpringBoot被標注的類是一個配置類,以前Spring xxx.xml能配置的內容,它都可以做,spring中的Bean組件默認是單實例的
#############################Configuration使用示例###################################################### /** * 1、配置類里面使用@Bean標注在方法上給容器注冊組件,默認也是單實例的 * 2、配置類本身也是組件 * 3、proxyBeanMethods:代理bean的方法 * Full(proxyBeanMethods = true)、【保證每個@Bean方法被調用多少次返回的組件都是單實例的】 * Lite(proxyBeanMethods = false)【每個@Bean方法被調用多少次返回的組件都是新創(chuàng)建的】 * 組件依賴必須使用Full模式默認。其他默認是否Lite模式 * * * */ @Configuration(proxyBeanMethods = false) //告訴SpringBoot這是一個配置類 == 配置文件 public class MyConfig { /** * Full:外部無論對配置類中的這個組件注冊方法調用多少次獲取的都是之前注冊容器中的單實例對象 * @return */ @Bean //給容器中添加組件。以方法名作為組件的id。返回類型就是組件類型。返回的值,就是組件在容器中的實例 public User user01(){ User zhangsan = new User("zhangsan", 18); //user組件依賴了Pet組件 zhangsan.setPet(tomcatPet()); return zhangsan; } @Bean("tom") public Pet tomcatPet(){ return new Pet("tomcat"); } } ################################@Configuration測試代碼如下######################################## @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)代理對象調用方法。SpringBoot總會檢查這個組件是否在容器中有。 //保持組件單實例 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("用戶的寵物:"+(user01.getPet() == tom)); } }
@Import注解詳解,將指定組件導入到容器中
* 4、@Import({User.class, DBHelper.class}) * 給容器中自動創(chuàng)建出這兩個類型的組件、默認組件的名字就是全類名 * * * */ @Import({User.class, DBHelper.class}) @Configuration(proxyBeanMethods = false) //告訴SpringBoot這是一個配置類 == 配置文件 public class MyConfig { }
@Conditional注解詳解,條件裝配,必須滿足Conditional指定的條件,才會繼續(xù)組件的注入
以上是所有Conditional的實現(xiàn)注解
@ConditionalOnBean(name = "tom") 容器中有tom組件,才會進行組件的注入
=====================測試條件裝配========================== @Configuration(proxyBeanMethods = false) //告訴SpringBoot這是一個配置類 == 配置文件 //@ConditionalOnBean(name = "tom") @ConditionalOnMissingBean(name = "tom") public class MyConfig { /** * Full:外部無論對配置類中的這個組件注冊方法調用多少次獲取的都是之前注冊容器中的單實例對象 * @return */ @Bean //給容器中添加組件。以方法名作為組件的id。返回類型就是組件類型。返回的值,就是組件在容器中的實例 public User user01(){ User zhangsan = new User("zhangsan", 18); //user組件依賴了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導入資源注解,一些老的項目還是會有xml配置的文件,該注解用于將這些xml文件導入進來
======================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 {} ======================測試================= boolean haha = run.containsBean("haha"); boolean hehe = run.containsBean("hehe"); System.out.println("haha:"+haha);//true System.out.println("hehe:"+hehe);//true
配置綁定
使用java讀取到properties文件中的內容,并且把它封裝到javaBean中,以供隨時使用
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里的內容綁定到對應的屬性當中
只有在容器中的組件,才會擁有SpringBoot提供的強大的功能
/** * 只有在容器中的組件,才會擁有SpringBoot提供的強大功能 */ @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)(開啟car配置綁定功能,把這個car這個組件自動注冊到容器中)
@ConfigurationProperties
自動配置原理入門
核心注解@SpringBootApplication相當于三個注解,@SpringBootConfiguration、
@EnableAutoConfiguration、@ComponentScan
- @SpringBootConfiguration:@Configuration。代表當前是一個配置類
- @ComponentScan:指定掃描范圍
- @EnableAutoConfiguration:核心注解,它也是一個核心注解,它里面包括@AutoConfigurationPackage和
- @Import(AutoConfigurationImportSelector.class)
- @AutoConfigurationPackage:內部是一個Import注解,就是給容器中導入組件
@Import(AutoConfigurationPackages.Registrar.class) ?//給容器中導入一個組件 public @interface AutoConfigurationPackage {} //利用Registrar給容器中導入一系列組件 //將指定的一個包下的所有組件導入進來,MainApplication 所在包下。
@Import(AutoConfigurationImportSelector.class)
- 1、利用getAutoConfigurationEntry(annotationMetadata);給容器中批量導入一些組件
- 2、調用List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes)獲取到所有需要導入到容器中的配置類
- 3、利用工廠加載 Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader);得到所有的組件
- 4、從META-INF/spring.factories位置來加載一個文件。
默認掃描我們當前系統(tǒng)里面所有META-INF/spring.factories位置的文件
spring-boot-autoconfigure-2.3.4.RELEASE.jar包里面也有META-INF/spring.factories
到此這篇關于Java SpringBoot自動配置原理詳情的文章就介紹到這了,更多相關Java SpringBoot自動配置 內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
在idea2023中使用SpringBoot整合Lombok全過程及詳細用法
Lombok項目是一個java庫,它可以自動插入到編輯器和構建工具中,增強java的性能,本文詳細給大家介紹了在idea2023中使用SpringBoot整合Lombok全過程及詳細用法,需要的朋友可以參考下2023-09-09Java listener簡介_動力節(jié)點Java學院整理
這篇文章主要介紹了Java listener簡介,可以用于統(tǒng)計用戶在線人數等,有興趣的可以了解一下2017-07-07SpringCloud自定義loadbalancer實現(xiàn)標簽路由的詳細方案
本文介紹了通過標簽路由解決前端開發(fā)環(huán)境接口調用慢的問題,實現(xiàn)方案包括在本地服務注冊元數據、自定義負載均衡器、以及網關配置等步驟,通過環(huán)境變量設置標簽,網關根據請求頭中的標簽進行路由,從而實現(xiàn)前后端互不干擾的開發(fā)調試,感興趣的朋友一起看看吧2025-02-02java序列化和serialVersionUID的使用方法實例
這篇文章主要介紹了java序列化和serialVersionUID的使用方法實例的相關資料,這里說明很詳細的使用方法讓你徹底學會,需要的朋友可以參考下2017-08-08MyBatis直接執(zhí)行SQL的工具SqlMapper
今天小編就為大家分享一篇關于MyBatis直接執(zhí)行SQL的工具SqlMapper,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12