spring boot 自动配置解密之注解@EnableAutoConfiguration

spring boot 自动配置解密之注解@EnableAutoConfiguration

  
spring boot为了自动配置,增加了注解@EnableAutoConfiguration,spring boot 1.2版之前,
我们的启动类需要加注解:
 @Configuration
 @EnableAutoConfiguration
 @ComponentScan


spring boot 1.2版之后新增了注解@SpringBootApplication,上面的注解就可以用一个注解替换:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
		@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
注解 @EnableAutoConfiguration是spring boot自动配置的关键, 也是扩展spring boot的关键之处。
spring boot试图根据条件来注册我们需要的bean实例:
 
Enable auto-configuration of the Spring Application Context, attempting to guess and configure beans that 
you are likely to need. Auto-configuration classes are usually applied based on your classpath and what beans 
you have defined. For example, if you have tomcat-embedded.jar on your classpath you are likely to want a 
TomcatServletWebServerFactory (unless you have defined your own ServletWebServerFactory bean). 
When using SpringBootApplication, the auto-configuration of the context is automatically enabled and 
adding this annotation has therefore no additional effect.

Auto-configuration tries to be as intelligent as possible and will back-away as you define more of 
your own configuration. You can always manually exclude() any configuration that you never want to apply 
(use excludeName() if you don't have access to them). You can also exclude them via the 
spring.autoconfigure.exclude property. Auto-configuration is always applied after user-defined beans 
have been registered.
The package of the class that is annotated with @EnableAutoConfiguration, usually via 
@SpringBootApplication, has specific significance and is often used as a 'default'. For example, 
it will be used when scanning for @Entity classes. It is generally recommended that you place
@EnableAutoConfiguration (if you're not using @SpringBootApplication) in a root package so that
all sub-packages and classes can be searched.
Auto-configuration classes are regular Spring Configuration beans. They are located using the
SpringFactoriesLoader mechanism (keyed against this class). Generally auto-configuration beans are 
@Conditional beans (most often using @ConditionalOnClass and @ConditionalOnMissingBean annotations).


 注解@EnableAutoConfiguration之所以能自动自动配置bean,主要是其注解
@Import(AutoConfigurationImportSelector.class)中的类org.springframework.boot.autoconfigure
.AutoConfigurationImportSelector起主要作用,它主要寻找必要的一些配置类。
 AutoConfigurationImportSelector类有几个方法,但对于spring boot 能自动配置的关键方法是:



/**
	 * Return the auto-configuration class names that should be considered. By default
	 * this method will load candidates using {@link SpringFactoriesLoader} with
	 * {@link #getSpringFactoriesLoaderFactoryClass()}.
	 * @param metadata the source metadata
	 * @param attributes the {@link #getAttributes(AnnotationMetadata) annotation
	 * attributes}
	 * @return a list of candidate configurations
	 */
	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;
	}
该方法返回 spring boot约定需要自动配置的类。

代码:
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(
				getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader());
主要功能是从文件
public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";
获取 约定好的自动配置类。spring boot默认提供的自动配置类路径:  

spring-boot-autoconfigure-2.0.0.M7.jar!/META-INF/spring.factories
 
比如一个自动配置类:
@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,
		ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {

	public static final String DEFAULT_PREFIX = "";

	public static final String DEFAULT_SUFFIX = "";

	private static final String[] SERVLET_LOCATIONS = { "/" };

	@Bean
	@ConditionalOnMissingBean(HiddenHttpMethodFilter.class)
	public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {
		return new OrderedHiddenHttpMethodFilter();
	}

	......

}
这个自动配置类是不是要实例化及内部配置的bean是不是需要实例化,这又回到了
    
    有兴趣的可以查看spring boot相关源码。
  
    




猜你喜欢

转载自blog.csdn.net/doctor_who2004/article/details/79200995