1. 主啟動類
@SpringBootApplication
public class Springboot01Application {
public static void main(String[] args) {
SpringApplication.run(Springboot01Application.class, args);
}
}
@SpringBootApplication 來標注一個主程序類 , 說明這是一個Spring Boot應用
2. @SpringBootApplication
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
............
}
@SpringBootConfiguration
作用:SpringBoot的配置類 ,標注在某個類上 , 表示這是一個SpringBoot的配置類
@EnableAutoConfiguration
作用:開啟自動配置功能,以前我們需要自己配置的東西,而現在SpringBoot可以自動幫我們配置 ;
告訴SpringBoot開啟自動配置功能,這樣自動配置才能生效;
@ComponentScan
這個注解在Spring中很重要 ,它對應XML配置中的元素。
作用:自動掃描并加載符合條件的組件或者bean , 將這個bean定義加載到IOC容器中
3. @SpringBootConfiguration
@Configuration
public @interface SpringBootConfiguration {
............
}
這里的 @Configuration,說明這是一個配置類 ,配置類就是對應Spring的xml 配置文件
3.1 @Configuration
@Component
public @interface Configuration {
............
}
這里的 @Component 這就說明,啟動類本身也是Spring中的一個組件而已,負責啟動應用!
4. @EnableAutoConfiguration
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
............
}
@AutoConfigurationPackage:自動配置包
@Import({AutoConfigurationImportSelector.class}) :給容器導入組件;
AutoConfigurationImportSelector:自動配置導入選擇器
4.1 @AutoConfigurationPackage
@Import(AutoConfigurationPackages.Registrar.class)
public @interface AutoConfigurationPackage {
............
}
@import :Spring底層注解@import, 給容器中導入一個組件 Registrar.class
作用:將主啟動類的所在包及包下面所有子包里面的所有組件掃描到Spring容器
4.2 @Import({AutoConfigurationImportSelector.class})
自動配置導入選擇器,那么它會導入哪些組件的選擇器呢?
方法:getCandidateConfigurations()
作用:獲得候選的配置
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;
}
這里的getSpringFactoriesLoaderFactoryClass()方法返回的就是我們最開始看的啟動自動導入配置文件的注解類:EnableAutoConfiguration
這個方法又調用了SpringFactoriesLoader類中的loadFactoryNames() 靜態方法。
4.2.1 loadFactoryNames()
public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {
String factoryTypeName = factoryType.getName();
return loadSpringFactories(classLoader).getOrDefault(factoryTypeName, Collections.emptyList());
}
這里它又調用了loadSpringFactories()方法
private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
//獲得classLoader, 我們返回可以看到這里得到的就是EnableAutoConfiguration標注的類本身
MultiValueMap<String, String> result = cache.get(classLoader);
if (result != null) {
return result;
}
try {
//去獲取一個資源 "META-INF/spring.factories"
Enumeration<URL> urls = (classLoader != null ?
classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
result = new LinkedMultiValueMap<>();
//將讀取到的資源遍歷,封裝成為一個Properties
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);
}
}
發現一個多次出現的文件: spring.factories,全局搜索它
image.png
WebMvcAutoConfiguration
我們在上面的自動配置類隨便找一個打開看看,比如 : WebMvcAutoConfiguration
image.png
可以看到這些一個個的都是JavaConfig配置類,而且都注入了一些Bean,可以找一些自己認識的類,看著熟悉一下!
所以,自動配置真正實現是從classpath中搜尋所有的META-INF/spring.factories配置文件,并將其中對應的 org.springframework.boot.autoconfigure包下的配置項,通過反射實例化為對應標注了 @Configuration的JavaConfig形式的IOC容器配置類, 然后將這些都匯總成為一個實例并加載到IOC容器中。
5. 結論:
1. SpringBoot在啟動的時候從類路徑下的META-INF/spring.factories中獲取 EnableAutoConfiguration指定的值
2. 將這些值作為自動配置類導入容器, 自動配置類就生效, 幫我們進行自動配置工作。
3. 整個J2EE的整體解決方案和自動配置都在springboot-autoconfigure的jar包中。
4. 它會給容器中導入非常多的自動配置類 (xxxAutoConfiguration, 就是給容器中導入這個場景需要的所有組件, 并配置好這些組件。
5. 有了自動配置類 , 免去了我們手動編寫配置注入功能組件等的工作。