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 application.properties, and you can write them into POJO properties of configuration class through mapping.
@EnableAutoConfiguration#
@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.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,\ org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.couchbase.CouchbaseRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration,\ org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.ldap.LdapDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\ org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration,\ org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration,\
Let's look at the details of automatic configuration of redis, RedisAutoConfiguration: #
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 application.properties 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 #
@ConditionalOnBean
A Bean is instantiated only when an object exists in the current context
@ConditionalOnClass
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
@ConditionalOnExpression
When the expression is true, a Bean will be instantiated
@ConditionalOnMissingBean
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
@ConditionalOnMissingClass
When a class path does not exist, a Bean will be instantiated
@ConditionalOnNotWebApplication
Only when it is not a web application
2.Properties series notes #
@EnableConfigurationProperties
@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
may 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.
@PropertySource
After spring boot version 1.5.2, use this method
@Component //@PropertySource can only load. properties file. You need to change the above yml file to. properties file @PropertySource("classpath:may.properties") @ConfigurationProperties(prefix="may") public class User2 { private String name; private String gender; //Omit setter and getter methods }
@EnableConfigurationProperties
Finally, add @ EnableConfigurationProperties to the spring Boot entry class
@SpringBootApplication @EnableConfigurationProperties({User.class,User2.class}) public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, 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: https://www.cnblogs.com/leihuazhe/p/7743479.html
Author: who distorts time and space
Source: https://www.cnblogs.com/itplay/p/10744616.html
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^_^^_^