SpringBoot in-depth analysis of the underlying automatic assembly principle

SpringBoot in-depth analysis of the underlying automatic assembly principle

In most cases, Spring Boot allows automatic assembly of Beans through configuration files. That's because it relies on the spring-boot-starter-web package, so it will introduce the spring-boot-starters package, and then the spring-boot-autoconfigure package. This package will provide the function of automatic configuration, there are many kinds of automatic configuration

​ a) Load the main configuration class when SpringBoot starts, use @EnableAutoConfigurationThe automatic configuration function is turned on

​ b) Use EnableAutoConfigurationImportSelector to import some components into the container, and place the classpath under META-INF/spring.factoriesEnableAutoConfiguration all values added to the configuration inside the vessel (hereinafter, partially automated configuration class code), each such xxxAutoConfigurationcomponent is a container class, are added to the vessel, they are used to do auto-configuration.

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
org.springframework.boot.autoconfigure.cloud.CloudAutoConfiguration,\
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,\
org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration,\
org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration,\

​ c) HttpEncodingAutoConfiguration (Http encoding automatic configuration) as an example to explain the principle of automatic configuration, the following code:

/*
注解作用:
	@Configuration:加在某个类上表示这个类属于配置类(和Spring的bean配置管理的XML文件功能一样)
	
	@EnableConfigurationProperties:表示HttpEncodingProperties类可以通过配置文件进行装配(可在application.properties文件中配置它)
	
	@ConditionalOnWebApplication:判断当前应用是否是web应用,如果是,当前配置类生效
	
	@ConditionalOnClass:判断当前项目有没有这个类CharacterEncodingFilter(SpringMVC中进行乱码解决的过滤器),存在CharacterEncodingFilter这个类后,Spring IoC容器才去装配HttpEncodingAutoConfiguration这个类
	
	@ConditionalOnProperty:这是一个检测属性配置的注解,代码中的配置也就是当存在属性spring.http.encoding.*配置后,才会启动这个类作为配置文件(即使不存在,判断也是成立的)
	
	@ConditionalOnMissingBean:说明在Spring IoC容器不存在CharacterEncodingFilter类型的Bean的时候才会使用这个方法装配Bean
	*/
@Configuration
@EnableConfigurationProperties(HttpEncodingProperties.class)
@ConditionalOnWebApplication//判断当前应用是否是web应用,如果是,当前配置类生效
@ConditionalOnClass(CharacterEncodingFilter.class)
@ConditionalOnProperty(prefix = "spring.http.encoding", value = "enabled", matchIfMissing = true)
public class HttpEncodingAutoConfiguration {
    
    

	private final HttpEncodingProperties properties;

	public HttpEncodingAutoConfiguration(HttpEncodingProperties properties) {
    
    
		this.properties = properties;
	}

	@Bean//给容器中添加一个组件,这个组件的某些值需要从properties中获取
	@ConditionalOnMissingBean(CharacterEncodingFilter.class)
	public CharacterEncodingFilter characterEncodingFilter() {
    
    
		CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
		filter.setEncoding(this.properties.getCharset().name());
		filter.setForceRequestEncoding(this.properties.shouldForce(Type.REQUEST));
		filter.setForceResponseEncoding(this.properties.shouldForce(Type.RESPONSE));
		return filter;
	}

​ d) View the automatic configuration source code of RedisRepositoriesAutoConfiguration as follows:

/*
	注解作用:
		@Import:表示加载其他的类到当前的环境中来。
		@AutoConfigureAfter:表示在完成RedisAutoConfiguration的装配后才执行,因为有些类存在先后的逻辑关系。
	补充:@AutoConfigureBefore来定制在哪些类之前初始化,这样就可以定制Spring IoC容器装配Bean的先后顺序
	*/
@Configuration
@ConditionalOnClass({
    
     Jedis.class, EnableRedisRepositories.class })
@ConditionalOnProperty(prefix = "spring.data.redis.repositories", name = "enabled", havingValue = "true", matchIfMissing = true)
@ConditionalOnMissingBean(RedisRepositoryFactoryBean.class)
@Import(RedisRepositoriesAutoConfigureRegistrar.class)
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisRepositoriesAutoConfiguration {
    
    

}

​ Through the analysis of the above two examples, I have a preliminary understanding of Spring Boot's automatic generation mechanism. In order to better use SpringBoot development in the future, to determine what Spring Boot will automatically assemble, if you need to customize the modification, what needs to be done, so that Spring Boot better serves development practice

Guess you like

Origin blog.csdn.net/weixin_45496190/article/details/107288240