日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

spring boot原理_SpringBoot-02-原理初探之主启动类

發布時間:2025/3/15 javascript 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring boot原理_SpringBoot-02-原理初探之主启动类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2. 原理初探

2.1 pom.xml

父依賴

主要依賴一個父項目,主要管理項目的資源過濾和插件

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --> </parent>

點進去,發現還有一個父依賴

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.3.2.RELEASE</version> </parent>

這里才是真正管理SpringBoot應用里所有依賴版本的地方,SpringBoot的版本控制中心;

以后導依賴默認不需要寫版本;但是如果導入的包沒有在依賴中管理就需要手動配置版本

啟動器 spring-boot-starter<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId> </dependency>

spring-boot-starter-web:幫我們導入web模塊正常運行所依賴的組件。

需要使用什么功能,就只需要找到對應的啟動器即可!

2.2 主啟動類

默認的主啟動類//SpringBootApplication:標注這個類是一個SpringBoot的應用 @SpringBootApplication public class Springboot01HelloApplication {public static void main(String[] args) {//將SpringBoot應用啟動SpringApplication.run(Springboot01HelloApplication.class, args);}}

注解:

@SpringBootConfiguration:springboot的配置@Configuration:spting配置類@Component:說明這也是spring的組件@EnableAutoConfiguration:自動配置@AutoConfigurationPackage:自動配置包@Import(AutoConfigurationPackages.Registrar.class):自動配置‘包注冊’@Import(AutoConfigurationImportSelector.class):自動配置導入//獲取所有的配置 List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);

獲取候選的配置:

protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),getBeanClassLoader());Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you "+ "are using a custom packaging, make sure that file is correct.");return configurations; }@SpringBootApplication

作用:標注在某個類上說明這個類是SpringBoot的主配置類,SpringBoot就運行在這個類的main方法上來啟動SpringBoot應用。

@SpringBootConfiguration @EnableAutoConfiguration @ComponentScan (excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) public @interface SpringBootApplication {... }@ComponentScan

它對應XML配置中的元素。

作用:自動掃描并加載所有符合條件的組件或bean,將這個bean定義加載到IOC容器中。

@SpringBootConfiguration

作用:標注在類上,表示這是一個springboot的配置類

繼續點進去:

@Configuration public @interface SpringBootConfiguration {... }@Component public @interface Configuration {... }

這里出現的注解及解釋: @Configuration :說明這是一個配置類,配置類就是對應Spring的xml配置文件;

@Component:說明啟動類本身也是Spring中的一個組件,負責啟動應用。

@EnableAutoConfiguration

作用:開啟自動配置功能,即以前我們需要自己配置的東西,現在SpringBoot自動幫我們配置。

點進該注解:

@AutoConfigurationPackage @Import(AutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration {... }

這里出現的注解:

@AutoConfigurationPackage:自動配置包

Registrar.class的作用:將主啟動類所在包及所有子包里的所有組件掃描到Spring容器中。

@Import(Registrar.class) public @interface AutoConfigurationPackage {... }

@import:表示給容器導入一個組件

@Import(AutoConfigurationImportSelector.class):給容器導入組件;

AutoConfigurationImportSelector:自動配置導入選擇器,那么它到底導入了哪些組件的選擇器呢?

  • 這個類中有這樣一個方法
  • protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),getBeanClassLoader());Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you "+ "are using a custom packaging, make sure that file is correct.");return configurations; }

    2. 這個方法又調用了SpringFactoriesLoader類的靜態方法:

    public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {String factoryTypeName = factoryType.getName();return loadSpringFactories(classLoader).getOrDefault(factoryTypeName, Collections.emptyList()); }

    3. 繼續點擊查看loadSpringFactories方法

    private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {MultiValueMap<String, String> result = cache.get(classLoader);if (result != null) {return result;}try {Enumeration<URL> urls = (classLoader != null ?classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));result = new LinkedMultiValueMap<>();while (urls.hasMoreElements()) {URL url = urls.nextElement();UrlResource resource = new UrlResource(url);Properties properties = PropertiesLoaderUtils.loadProperties(resource);for (Map.Entry<?, ?> entry : properties.entrySet()) {String factoryTypeName = ((String) entry.getKey()).trim();for (String factoryImplementationName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {result.add(factoryTypeName, factoryImplementationName.trim());}}}cache.put(classLoader, result);return result;}catch (IOException ex) {throw new IllegalArgumentException("Unable to load factories from location [" +FACTORIES_RESOURCE_LOCATION + "]", ex);} }

    4. 發現一個多次出現的文件:spring.factories,全局搜索它

    spring.factories

    我們根據源頭打開spring.factories,看到很多自動配置的文件;這就是自動配置根源的所在!

    以WebMvcAutoConfiguration為例,打開:

    可以看到一個個都是JavaConfig配置類,并且都注入了一些bean。

    所以,自動配置真正實現是從classpath中搜尋所有的META-INF/spring.factories配置文件,并將對應的 org.springframework.boot.autoconfigure包下的配置項,通過反射實例化為對應標注了@Configuration的JavaConfig形式的IOC容器配置類,然后將這些匯總成為一個實例并加載到IOC容器。

    結論
  • SpringBoot在啟動的時候從類路徑META-INF/spring.factories中獲取EnableAutoConfiguration指定的值;
  • 將這些值作為自動配置類導入容器,自動配置類就生效,幫我們自動配置工作;
  • 整個J2EE的整體解決方案和自動配置都在spring-autoconfigure的jar包中;
  • 它會給容器中導入非常多的自動配置類(xxxAutoConfiguration),就是給容器中導入這個場景需要的所有組件,并配置好;
  • 有了自動配置類,免去了我們手動編寫配置注入功能組件等工作。
  • 總結

    以上是生活随笔為你收集整理的spring boot原理_SpringBoot-02-原理初探之主启动类的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。