Auto configuration of spring boot

Keywords: Programming Spring Redis ElasticSearch SpringBoot

Spring boot auto configuration is mainly completed by @ EnableAutoConfiguration, @ Conditional, @ EnableConfigurationProperties or @ ConfigurationProperties.

@EnableAutoConfiguration enables automatic configuration. Its main function is to call loadFactoryNames() in the spring core package to load the written automatic configuration in the autoconfig package.

@Conditional condition annotation determines whether to load and automatically configure the class by judging whether there is a corresponding configured jar package in the class path.

@The function of EnableConfigurationProperties is to provide specific configuration parameters for automatic configuration. Just write them in, and you can write them into POJO properties of configuration class through mapping.


@The Enable * annotation is not a newly invented annotation of spring boot. Spring 3 framework introduces these annotations and replaces XML configuration files with these annotations. For example:
@Enable transaction management annotation, which can declare transaction management
@EnableWebMvc annotation, which enables Spring MVC
@Enable scheduling annotation, which initializes a scheduler.

These annotations are actually simple configurations, imported through the @ Import annotation. #

Enter @ SpringBootApplication of startup class, and find @ EnableAutoConfiguration in it,

@The EnableAutoConfigurationImportSelector is imported through @ Import in EnableAutoConfiguration,

Enter his parent class AutoConfigurationImportSelector

Find the selectImports() method, which calls the getCandidateConfigurations() method, which in turn calls the loadFactoryNames() method in the Spring Core package. The purpose of this method is to query the JAR file contained in the META-INF/spring.factories file.

When the spring.factories file is found, spring factories loader queries the properties named by the configuration file.

Jar file in spring.factories of org.springframework.boot.autoconfigure

The contents of spring.factories are as follows (the intercepted part). In this file, you can see a list of Spring Boot auto configuration

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.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,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\,\ 

Let's look at the details of automatic configuration of redis, RedisAutoConfiguration: #


This class implements a simple Spring configuration and declares the typical Bean required by Redis. Like many other classes, it heavily relies on the Spring Boot annotation:
1) @ ConditionOnClass activate a configuration, which will be configured only when the class exists in the class path
2) @ EnableConfigurationProperties automatically maps a property set from POJO to the Spring Boot configuration file (the default is the file).
3) @ ConditionalOnMissingBean enables a Bean definition, but it is valid only if the Bean has not been defined before.
You can also use the @ autoconfigurebeefore annotation and the @ AutoConfigureAfter annotation to define the loading order of these configuration classes.

Focus on @ Conditional annotation, new features of Spring 4 framework #

This comment enables some configurations only when certain conditions are met. SrpingBoot's AutoConfig uses a lot of @ Conditional, which dynamically injects beans according to the running environment. Here we introduce the use and principle of @ Conditional, and customize @ Conditional to customize the function.

  • @Conditional is a function of the spring framework. SpringBoot defines
  • @A series of annotations such as ConditionalOnClass, @ ConditionalOnProperty are used to achieve richer content.

The meaning of several @ condition * annotations #


A Bean is instantiated only when an object exists in the current context


If a class is on the class path, a bean will be instantiated. The class corresponding to the parameter of the annotation must exist. Otherwise, the configuration class modified by the annotation will not be resolved


When the expression is true, a Bean will be instantiated


Only when an object does not exist in the current context will a bean be instantiated. This annotation means that if there is a bean of the class it modifies, you do not need to create the bean. You can pass in parameters to the annotation, such as @ ConditionOnMissingBean(name = "example"). This means that if the bean with name "example" exists, the code block modified by the annotation will not execute


When a class path does not exist, a Bean will be instantiated


Only when it is not a web application

2.Properties series notes #

@ConfigurationProperties(prefix = "may")

Add this annotation to the class requiring injection configuration. Prefix means configuration starting with this prefix. The following is an example

    @ConfigurationProperties(prefix = "may")  
    public class User { private String name; private String gender; //Omit setter and getter methods } 

Configuration in application.yml

      name: youjie
 gender: man

If you do not use the initial application.yml configuration class of the system, but use your own, such as youjie.yml, you can configure as follows

    @ConfigurationProperties(prefix = "may",locations = "classpath:youjie.yml")  
    public class User2 { private String name; private String gender; //Omit setter and getter methods } 

Obsolete: since spring boot version 1.5.2 has removed the location attribute, the above method is obsolete in the latest version.

After spring boot version 1.5.2, use this method

//@PropertySource can only load. properties file. You need to change the above yml file to. properties file
@ConfigurationProperties(prefix="may") public class User2 { private String name; private String gender; //Omit setter and getter methods } 


Finally, add @ EnableConfigurationProperties to the spring Boot entry class

    public class DemoApplication { public static void main(String[] args) {, args); } } 

In fact, @ EnableConfigurationProperties({User.class,User2.class}) can be omitted here

summary #

Spring boot's automatic configuration benefits from the powerful support of spring framework, which has many tools and annotations to automatically assemble beans. SpringBoot directly writes configuration classes for common components in the market through a package. When our program relies on the jar package of these components, we start the spring boot application, and the automatic loading starts.

We can also define our own auto assembly components. After dependency, Spring can directly load the starter we defined. The author will code and interpret in the following articles.


Change from:

Author: who distorts time and space


Copyright: Knowledge sharing Attribution - non-commercial use - sharing in the same way 4.0 International Knowledge sharing license agreement.

Note: please indicate the source of reprint^_^^_^

Posted by StateGuy on Fri, 20 Mar 2020 11:02:12 -0700