Spring Boot automatic configuration principle

Source: Internet
Author: User

1). When spring boot starts, the main configuration class is loaded, and the automatic configuration function is turned on @enableautoconfiguration

2). A series of components are imported through the @enableautoconfiguration Autoconfigurationimportselector to the container via list<string> configurations = Getcandidateconfigurations (annotationmetadata,attributes); Get the candidate configuration, next through Springfactoriesloader.loadfactorynames ( Scan all the jar package Classpath meta-inf/ Spring.factories, wraps the scanned file contents into the Properties object, obtains the value corresponding to the Enableautoconfiguration.class class name from the properties, and then adds them to the container.

Add the values of all enableautoconfiguration configured in the meta-inf/spring.factories under the classpath to the container

This is part of the code in the file:

# Auto Configureorg.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, wait. Class for some columns

Each of these xxxxautoconfiguration classes is a component in a container that is added to the container and used to automatically configure

3). Each automatic configuration class @enableautoconfiguration for automatic configuration function

@Target (Elementtype.type) @Retention (retentionpolicy.runtime) @ Documented@inherited@autoconfigurationpackage@import (autoconfigurationimportselector.class) public @interface enableautoconfiguration {String Enabled_override_property = "Spring.boot.enableautoconfiguration";/** * Exclude Specific auto-configuration classes such that they would never be applied. * @return The classes to exclude */class<?>[] exclude () Default {};/** * exclude specific Auto-configuration Class N Ames such that they would never be * applied. * @return The class names to exclude * @since 1.3.0 */string[] excludename () default {};}

@autoconfigurationpackage: Automatic configuration Package

@Import(AutoConfigurationPackages.Registrar.class)

Spring's underlying annotation @import, importing a component to a container ()

  AutoConfigurationPackages.Registrar.class: Is thepackage that contains all of the packages under the packet containing the class with the @ springbootconfiguration dimension scanned into the spring container.

@Import(autoconfigurationimportselector.class): Import components;

Return all components that need to be imported in the form of a full-class name: These components are added to the container

 @Overridepublic string[] Selectimports (Annotationmetadata annotationmetadata {if (!isenabled (Annotationmetadata)) {return no_imports;} Autoconfigurationmetadata Autoconfigurationmetadata = Autoconfigurationmetadataloader.loadmetadata ( This.beanclassloader); Annotationattributes attributes = GetAttributes (annotationmetadata); list<string>   configurations  =   Getcandidateconfigurations   (annotationmetadata,attributes); configurations = RemoveDuplicates ( configurations); Set<string> exclusions = getexclusions (annotationmetadata, attributes); Checkexcludedclasses (configurations, exclusions); Configurations.removeall (exclusions); configurations = Filter (configurations, Autoconfigurationmetadata); fireautoconfigurationimportevents (configurations, exclusions); return Stringutils.tostringarray (configurations);}  

configurations: The array information stored inside is the full class name that needs to be automatically configured by spring. (xxx.autoconfiguration).

4). Explain the principle of automatic configuration with httpencodingautoconfiguration as an example

@Configuration//This is a configuration class that can also add components to a container//enable the Configurationproperties function of the specified class//bind the corresponding value in the configuration file with Httpencodingproperties. and add httpencodingproperties to IOC container @enableconfigurationproperties (httpencodingproperties.class)// Spring bottom @conditional annotations, according to different conditions. If the specified condition is met, the entire configuration class will take effect//determine if the current app is web App @conditionalonwebapplication (type = ConditionalOnWebApplication.Type.SERVLET)// Judging the current project there is no garbled solution in this class CHARACTERENCODINGFILTER:MVC filter @conditionalonclass (characterencodingfilter.class)// Determine if there is a configuration in the configuration file spring.http.encoding.enabled, if not exist is also established//will be matchifmissing = True to determine the default effect @conditionalonproperty ( prefix = "Spring.http.encoding", value = "Enabled", matchifmissing = true) public class Httpencodingautoconfiguration {pri Vate final httpencodingproperties properties;public httpencodingautoconfiguration (httpencodingproperties properties {this.properties = properties;} @Bean @conditionalonmissingbeanpublic 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;}

Determines whether the current configuration class is in effect, judging by the current different conditions?

Once the configuration class is in effect: This configuration class will add various components to the container, and the properties of the component are derived from the corresponding Xxxproperties class, each of which originates from the configuration file.

5). The properties that can be configured in all the profiles are encapsulated in the Xxxproperties class, what is available in the configuration file, what is here,

Gets the specified value from the configuration file and binds the Bean's properties
@ConfigurationProperties (prefix = "spring.http.encoding") public class Httpencodingproperties {
。。。
}

1.spring boot boot Loads a large number of automatic configuration classes

2. We look at the features we need spring boot default write-ready automatic configuration class has the properties

3. We look at the configuration of those components in this auto-configuration class, without writing

4. When adding components to an automatic configuration class in a container, some properties are obtained from the Prperties class and we can specify his value in the configuration file

Xxxautoconfiguration Automatic configuration Classes Add components to the container and there is a corresponding xxxproperties to encapsulate the related properties in the configuration file

Spring Boot automatic configuration principle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.