1.spring-boot启动的main入口
SpringBootApplication注解,点击注解,发现这个注解中的@EnableAutoConfiguration 这个注解表示开启自动配置,自动配置一些组件
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import({AutoConfigurationImportSelector.class}) public @interface EnableAutoConfiguration { String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration"; Class<?>[] exclude() default {}; String[] excludeName() default {}; }
这个注解中import这个注解里
AutoConfigurationImportSelector这个类就会扫描所有包,找到所有的配置类,点开这个类
里面最重要的就是这个selectImports方法,我们来分析一下:
public String[] selectImports(AnnotationMetadata annotationMetadata) { if (!this.isEnabled(annotationMetadata)) { return NO_IMPORTS; } else { AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader); AnnotationAttributes attributes = this.getAttributes(annotationMetadata); List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes); configurations = this.removeDuplicates(configurations); Set<String> exclusions = this.getExclusions(annotationMetadata, attributes); this.checkExcludedClasses(configurations, exclusions); configurations.removeAll(exclusions); configurations = this.filter(configurations, autoConfigurationMetadata); this.fireAutoConfigurationImportEvents(configurations, exclusions); return StringUtils.toStringArray(configurations); } }
AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader);//会去调用loadMetadata方法,传入的是一个beanClassLoader
查看loadMetadata发现其调用的是重载方法
public static AutoConfigurationMetadata loadMetadata(ClassLoader classLoader) { return loadMetadata(classLoader, "META-INF/spring-autoconfigure-metadata.properties"); } static AutoConfigurationMetadata loadMetadata(ClassLoader classLoader, String path) { try { Enumeration<URL> urls = classLoader != null ? classLoader.getResources(path) : ClassLoader.getSystemResources(path); Properties properties = new Properties(); while(urls.hasMoreElements()) { properties.putAll(PropertiesLoaderUtils.loadProperties(new UrlResource((URL)urls.nextElement()))); } return loadMetadata(properties); } catch (IOException var4) { throw new IllegalArgumentException("Unable to load @ConditionalOnClass location [" + path + "]", var4); } }
打断点后发现是把“”META-INF/spring-autoconfigure-metadata.properties“”这个文件变成一个properties返回
然后在org.springframework.boot下随便找一个配置类看一下具体实现,我这里找的是
HttpEncodingAutoConfiguration
这个类上面有个
@EnableConfigurationProperties({HttpEncodingProperties.class})
而点开这个类,里面就见到我熟悉的
ConfigurationProperties
说明我们要找的自动配置的类就在这里,
@ConfigurationProperties( prefix = "spring.http.encoding" )
这个类里面也是我们能够在springboot中能够使用的所有字段,验证一下