Use springboot developing web applications is very convenient, only need to introduce corresponding GAV can use the corresponding function, springboot default configuration will help us better common configuration. So springboot is how to do it? This article will step through the source code to see springboot in the end is how to help us to do automated configuration.
springboot core notes
![13341631-2e71b828ec8e73de.png](https://upload-images.jianshu.io/upload_images/13341631-2e71b828ec8e73de.png)
![13341631-2aa04857b5004247.png](https://upload-images.jianshu.io/upload_images/13341631-2aa04857b5004247.png)
See @import introduced using a selector turn automatic configuration
@Import effect, the official source
* @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();
}
Introducing @Configuration annotation configuration class
introduced ImportSelector implementation class
introduced ImportBeanDefinitionRegistrar implementation class
Then look at the introduction of the selector (@ EnableAutoConfigurationImportSelector.class)
![13341631-a0c01c10b7bcd2f4.png](https://upload-images.jianshu.io/upload_images/13341631-a0c01c10b7bcd2f4.png)
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;
}
Here method called two core methods
1, getSpringFactoriesLoaderFactoryClass (), we find that the return is EnableAutoConfiguration.class
2, loadFactoryNames this method
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 this as a key value of the Property, to acquire Value. springboot default global scan FACTORIES_RESOURCE_LOCATION
![13341631-9468aa44ace2b63a.png](https://upload-images.jianshu.io/upload_images/13341631-9468aa44ace2b63a.png)
springboot will load (org.springframework.boot.autoconfigure.EnableAutoConfiguration) corresponding to all the autoconfiguration spring IOC container
How automatic configuration to take effect
以(org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration)为例。
![13341631-4e208eb842026bab.png](https://upload-images.jianshu.io/upload_images/13341631-4e208eb842026bab.png)
We see the current auto-configuration core classes in these notes.
@Configuration
@ConditionalOnClass({ Servlet.class, StandardServletMultipartResolver.class,
MultipartConfigElement.class })
@ConditionalOnProperty(prefix = "spring.http.multipart", name = "enabled", matchIfMissing = true)
@EnableConfigurationProperties(MultipartProperties.class)
@Configuration spring configuration class notes
@ConditionalOnClass mean the existence of a class, the current configuration to take effect
@ConditionalOnProperty meaning of spring.http.multipart whether to open the configuration exists, here enabled by default, corresponding to springboot main configuration file (application) file configuration items
@EnableConfigurationProperties means is added to the class MultipartProperties spring containers, equivalent to the added annotation on MultipartProperties class @Component
We take a look MultipartProperties the class is doing?
@ConfigurationProperties(prefix = "spring.http.multipart", ignoreUnknownFields = false)
public class MultipartProperties {
@ConfigurationProperties read configuration springboot main configuration file (application.prperties) of
So finally we found as long as @Conditionalxxxx conditions, the current class automatic configuration to take effect.
To sum up, if we want to know which attributes the introduction of a GAV can be configured, the primary need to find the corresponding view corresponding xxxAutoConfiguration
@EnableConfigurationProperties (xxxx.class) can be introduced in class.
Reproduced in: https: //www.jianshu.com/p/ad21f8e65c7a