使用springboot开发web应用是很方便,只需要引入相对应的GAV就可以使用对应的功能,springboot默认会帮我们配置好一些常用配置。那么springboot是怎么做到的呢?这篇文章将一步步跟踪源码,查看springboot到底是如何帮我们做自动化配置。
springboot核心注解
@SpringBootApplication
可以看到使用@import导入一个开启自动配置的选择器
@import的作用,官方源码
* @author Chris Beams* @since 3.0* @see Configuration* @see ImportSelector* @see ImportResource*/ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Import {/*** The @{@link Configuration}, {@link ImportSelector} and/or* {@link ImportBeanDefinitionRegistrar} classes to import.*/Class<?>[] value(); }
导入@Configuration注解的配置类
导入ImportSelector的实现类
导入ImportBeanDefinitionRegistrar的实现类
接着看导入的这个选择器(@EnableAutoConfigurationImportSelector.class)
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;}
这里方法调用了两个核心方法
1、 getSpringFactoriesLoaderFactoryClass(),我们发现返回的是EnableAutoConfiguration.class
2、loadFactoryNames这个方法
public static List<String> loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader) {String factoryClassName = factoryClass.getName();try {Enumeration<URL> urls = (classLoader != null ? classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));List<String> result = new ArrayList<String>();while (urls.hasMoreElements()) {URL url = urls.nextElement();Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url));String propertyValue = properties.getProperty(factoryClassName);for (String factoryName : StringUtils.commaDelimitedListToStringArray(propertyValue)) {result.add(factoryName.trim());}}return result;}catch (IOException ex) {throw new IllegalArgumentException("Unable to load factories from location [" +FACTORIES_RESOURCE_LOCATION + "]", ex);}}
先获取factoryClass(EnableAutoConfiguration)的className(org.springframework.boot.autoconfigure.EnableAutoConfiguration),
将这个className当做Property的key值,来获取Value。springboot默认会全局扫描FACTORIES_RESOURCE_LOCATION
public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";
springboot将会加载(org.springframework.boot.autoconfigure.EnableAutoConfiguration)所对应的所有自动配置到spring IOC容器中
自动配置如何生效
以(org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration)为例。
我们看到当前自动配置类核心在这几个注解。
@Configuration @ConditionalOnClass({ Servlet.class, StandardServletMultipartResolver.class,MultipartConfigElement.class }) @ConditionalOnProperty(prefix = "spring.http.multipart", name = "enabled", matchIfMissing = true) @EnableConfigurationProperties(MultipartProperties.class)
@Configuration spring注解配置类
@ConditionalOnClass 意思是存在某个类,当前配置生效
@ConditionalOnProperty 意思是否存在开启的spring.http.multipart 的配置,这里默认开启,对应的是springboot主配置文件(application)文件中配置项目
@EnableConfigurationProperties 意思是将MultipartProperties类加入spring容器,等价于在MultipartProperties类上加 @Component注解
我们去看一下MultipartProperties类是干什么了?
@ConfigurationProperties(prefix = "spring.http.multipart", ignoreUnknownFields = false) public class MultipartProperties {
@ConfigurationProperties 读取springboot主配置文件(application.prperties)的配置
所以最后发现只要满足@Conditionalxxxx条件 ,当前自动配置类即可生效。
综上,如果我们想知道引入的某个GAV可以配置哪些属性,主需要找到对应的xxxAutoConfiguration 查看对应的
@EnableConfigurationProperties(xxxx.class)引入的class即可。