1.@SpringBootApplication注解的原理
2.里面含有@EnableAutoConfiguration(启用自动配置)-->@Import(EnableAutoConfigurationImportSelector.class)
3.EnableAutoConfigurationImportSelector(启用自动配置导入选择器)继承父类AutoConfigurationImportSelector(自动配置导入选择器)
4.父类里有个方法selectImports()(选择导入)返回值是configurations(配置)
5.获取configurations的方法List<String> configurations = getCandidateConfigurations(获取候选配置)(annotationMetadata,attributes);
6.这getCandidateConfigurations()方法里有一个List<String> configurations = SpringFactoriesLoader.loadFactoryNames( getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader());
7.这个loadFactoryNames(装货工厂名称)()方法里有一个获取urls的方法,getResources()
Enumeration<URL> urls = (classLoader != null ? classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
8.这个属性FACTORIES_RESOURCE_LOCATION(工厂\资源\位置)的值为:META-INF/spring.factories
即这个自动配置加载的url都是来自META-INF/spring.factories里EnableAutoConfiguration配置下的所有值

9.然后就是遍历获取对应的配置名。然后把他们添加在容器中
10.这就是自动配置的原理。
本文详细解析了Spring Boot的@SpringBootApplication注解背后的自动配置机制。通过@EnableAutoConfiguration,Spring Boot加载META-INF/spring.factories文件中指定的配置,实现应用的自动化配置。在启动过程中,自动配置选择器获取候选配置并加载到容器中,简化了传统Spring的配置过程。
715

被折叠的 条评论
为什么被折叠?



