javascript
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:自動配置導入選擇器,那么它到底導入了哪些組件的選擇器呢?
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容器。
結論總結
以上是生活随笔為你收集整理的spring boot原理_SpringBoot-02-原理初探之主启动类的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: pythontry参数_python
- 下一篇: stm8s003程序跑飞_A股要大跌?跑
