introduction

A reader is interviewing recently , Such a question was asked in the interview “ Look, you've used springboot, Tell me springboot How is the automatic configuration of ?” This should be a springboot One of the most common interview questions . Let's take this question and dissect it together springBoot The principle of automatic configuration .

SpringMvc and SpringBoot contrast

First of all, let's go back to building a springmvc Of hello-word Of web project (xml Configured ) Are we going to be in pom Import various dependencies in , Then there may be version conflicts among the dependencies, which need to be excluded . When you go through a lot of hardships to solve the dependence , And then you need to write web.xml、springmvc.xml Configuration files, etc . We just want to write individual hello-word It's just a project , It's really a lot of time spent on configuration files and jar Package dependency . Greatly affected the efficiency of our development , And increased web Difficulty of development . To simplify this complex configuration 、 And the conflicting dependencies of each version ,springBoot It came into being . We now pass idea Create a springboot The project was solved in minutes , You don't need to care about the configuration ( Zero configuration is basically realized ). Let you really realize out of the box .SpringBoot It saves you a lot of time to spend with your girlfriend , No, programmers don't have girlfriends ?( If not, you can new One of them ) It not only allows you to spend more time on your business logic development , And it's greatly reduced web The threshold of development . therefore SpringBoot It's better to understand people's clothes , Wrong, wrong, understanding , Know where the pain points of developers are .

SpringBoot Automatic configuration loading

since Springboot Even though it's so easy to use , But as a user , We're still curious about how it helps us out of the box .Spring Boot There is a global profile :application.properties or application.yml. In this global file, you can configure various parameters, such as you want to change the port server.port Or you want to adjust the level of the log, all can be configured . For more properties that can be configured, please refer to the official website .https://docs.spring.io/spring-boot/docs/2.3.0.RELEASE/reference/htmlsingle/#common-application-properties



So many attributes , How do these properties work in a project ?SpringBoot There is no configuration for the project , To configure ”(application.properties or application.yml With the exception of ), Both However, there is no breakthrough in configuration , Then we can only find the entry from the startup class . The startup class is a bare one main Method , There is only one note on the class SpringBootApplication

The note is Spring Boot Essential notes for the project . So the principle of automatic configuration must have something to do with this annotation ! Let's take a look at this annotation .

@SpringBootApplication annotation

@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 {

There's nothing to say about the top four notes , If you have basically implemented a custom annotation , We all know what difference means .

  • @SpringBootConfiguration Inherited from @Configuration, The two functions are the same , Note that the current class is a configuration class .
  • @ComponentScan It is mainly used to specify scan path on class or interface , Follow Xml Inside <context:component-scan base-package="" /> The configuration is the same .springboot If you don't write this scan path , The default is the path to start the class .
  • @EnableAutoConfiguration
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {

Let's focus on this note AutoConfigurationImportSelector This class getCandidateConfigurations

In this method SpringFactoriesLoader.loadFactoryNames() Scan all with META-INF/spring.factories Of jar package ( spring.factories We can understand that Spring Boot Their own SPI Mechanism ).

spring-boot-autoconfigure-x.x.x.x.jar There's one in there spring.factories file .spring.factories Files are made up of groups of Key = value In the form of , One of them key yes EnableAutoConfiguration The full class name of the class , And its value It's an example. AutoConfiguration List of class names at the end of , Yes redis、mq Wait for the class names to be separated by commas .



We're going back to getAutoConfigurationEntry This method will be executed when getCandidateConfigurations In this method, we can see that a total of 127 Autoconfig classes .



Do these classes have to be loaded in ?springboot Still not so stupid , It advocates loading on demand .

  • It removes duplicate classes
  • Filter it out. We configured it exclude The configuration below the annotated class will be filtered out RestTemplateAutoConfiguration This class

  • After the above treatment , The rest of the auto configuration classes, if they're going to work , It is necessary to meet certain conditions . If these conditions are satisfied spring boot It's done through conditional annotations .

@ConditionalOnBean: When there is a designation in the container Bean Under the condition of

@ConditionalOnClass: When there is a specified class in the classpath

@ConditionalOnExpression: be based on SpEL Expression for true It is only instantiated as a judgment condition

@ConditionalOnJava: be based on JVM Version as a condition of judgment

@ConditionalOnJndi: stay JNDI Find the specified location if it exists

@ConditionalOnMissingBean: When there is no designation in the container Bean Under the circumstances

@ConditionalOnMissingClass: When there is no specified class in the container

@ConditionalOnWebApplication: The current project Web Under project conditions

@ConditionalOnNotWebApplication: The current project is not Web Under project conditions

@ConditionalOnProperty: Whether the specified property has the specified value

@ConditionalOnResource: Whether the class path has the specified value

@ConditionalOnOnSingleCandidate: When specifying Bean There's only one in the container , Or more than one but specify the preferred one Bean

These notes are all combined @Conditional annotation , It's just that different combinations of conditions are used, and finally true To instantiate the class that needs to be instantiated , Otherwise, ignore filtering out . When we go back to the code, we can see that after the conditional judgment and filtering, we have left the auto configuration classes that meet the conditions, only 23 A the . The others are filtered because they don't satisfy the condition annotation .



If we want to know which autoconfiguration classes are filtered , What's the reason it's filtered , And which classes are loaded .spring boot It's all in the journal for us . It's still very sweet . We can adjust the level of our log to debug. Then we can see the following log



Here we intercept part of the log . There are four parts in total :

  • Positive matches@Conditional Condition is true , Configuration class is Spring Container loading .
  • Negative matches: @Conditional The condition is false , Configuration class is not Spring Container loading .
  • Exclusions: We've identified classes that don't need to be loaded . For example, start the class configuration above RestTemplateAutoConfiguration class
  • Unconditional classes: Autoconfig classes do not contain any class level conditions , in other words , Classes are always loaded automatically .

Automatic configuration takes effect

We use ServletWebServerFactoryAutoConfiguration For example, configure a class , Explain how the properties in the global configuration file work , such as :server.port=88, How it works ( Of course, if it is not configured, there will be default values , This default value comes from org.apache.catalina.startup.Tomcat).

// Marked as configuration class 
@Configuration(proxyBeanMethods = false)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
// If there is ServletRequest.class Will take effect
@ConditionalOnClass(ServletRequest.class)
@ConditionalOnWebApplication(type = Type.SERVLET)
// hold @ConfigurationProperties The annotated class injection is Spring Container of Bean.
@EnableConfigurationProperties(ServerProperties.class)
@Import({ ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class,
ServletWebServerFactoryConfiguration.EmbeddedTomcat.class,
ServletWebServerFactoryConfiguration.EmbeddedJetty.class,
ServletWebServerFactoryConfiguration.EmbeddedUndertow.class })
public class ServletWebServerFactoryAutoConfiguration {

We can find out EnableConfigurationProperties It's configured in the annotation ServerProperties.class

@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties { /**
* Server HTTP port.
*/
private Integer port;

There is an annotation on this class :@ConfigurationProperties, Its function is to bind properties from the configuration file to the corresponding bean On ( That is to put us application.properties Corresponding server.port Mapping to ServerProperties Class port attribute ) and @EnableConfigurationProperties This annotation is to bind the attribute of beanServerProperties) Injection into spring In the container ( amount to @Component The annotation is the same ).

All the properties that can be configured in the configuration file are in xxxxPropertites Class encapsulates , The configuration file can refer to the property class corresponding to a function .

By now, I should be able to answer the question at the beginning of the article , Interview should not need to answer so detailed, you can refer to the following answers :

Spring Boot When it starts, it will go through @EnableAutoConfiguration Notes found META-INF/spring.factories All autoconfiguration classes in the configuration file , And load it , And these auto configuration classes are based on AutoConfiguration It's named at the end , It's actually a JavaConfig Formal Spring Container configuration class , It can be achieved by Properties In the class named at the end, get the properties configured in the global configuration file, such as :server.port, and XxxxProperties Class is through @ConfigurationProperties The annotation is bound to the corresponding attribute in the global configuration file .

I found a picture on the Internet , Basically, the automatic assembly process has been explained clearly .

summary

  • SpringBoot Startup loads a large number of autoconfig classes ( adopt “SPI” The way ), Then it will reserve some required classes according to the conditional annotation .
  • We introduce a new component , You can have a look first springBoot Is there a default offer .
  • SpringBoot Basically achieved “ Zero configuration “, And out of the box .

end

Daily use SpringBoot I don't even know the principle of its automatic assembly ? More articles about

  1. SpringBoot: Principle of automatic assembly

    Preface Spring Translated into Chinese is “ spring ”, You bet , In a certain period of time , It gives Java Developers bring spring , But with the expansion of our project ,Spring There are more and more places to configure , Exaggeration point ,“ Configure for two hours ,Coding Five minutes ...

  2. SpringBoot Automation configuration II : Automatic configuration (AutoConfigure) principle 、EnableAutoConfiguration、condition

    Autoconfiguration is pretty good Spring Boot Highlight of , It's a perfect show CoC Convention over configuration : Spring Boot It can be configured automatically Spring Seed Projects (Spring MVC, Spring Security, ...

  3. Springboot Study : The principle of underlying dependency and auto configuration

    springboot Dependent parent project We're creating springboot When the project is , A parent project is set : This project can be click in , You can find that it depends on another parent project Click in again , Found no dependency on parent project Observe the progress of this project pom ...

  4. SpringBoot The principle of automatic assembly

    1.SpringApplication.run(AppConfig.class,args); There are some problems in the execution process refreshContext(context); this sentence . 2.refreshContext(co ...

  5. SpringBoot: Let me show you the principle of automatic assembly

    Preface Spring Translated into Chinese is “ spring ”, You bet , In a certain period of time , It gives Java Developers bring spring , But with the expansion of our project ,Spring There are more and more places to configure , Exaggeration point ,“ Configure for two hours ,Coding Five minutes ...

  6. springboot Principle of automatic assembly

    Recently started to learn spring Source code , I saw it when I read all kinds of articles springboot Realization principle of automatic assembly . Use your own words to summarize . First, open a basic springboot project , Click in @SpringBootApplica ...

  7. Step by step Spring Framework Assembly Mastery SpringBoot Automatic assembly

    Catalog Spring Framework Pattern annotation Spring Framework@Enable Module assembly Spring Framework Conditions for assembly SpringBoot Automatic assembly Summary of this chapter Spring ...

  8. SpringBoot Automatic assembly principle analysis

    This article contains :SpringBoot The principle of automatic configuration and how to customize SpringBootStar etc. We know , In the use of SpringBoot When , We just need to start one directly as follows Web Program : @SpringBoo ...

  9. Spring Boot From Spring Framework Assembly Mastery SpringBoot Automatic assembly

    Spring Framework Pattern annotation Pattern annotation is a kind of declaration used to play the role of “ Components ” Comments on the role . Such as Spring Framework Medium @Repository Label on any class , Used to play the warehousing role ...

  10. If you don't know how to control it yet springboot in bean Loading order of , Then you must read this article

    1. Why do I need to control the loading order springboot Follow the principle of convention over configuration , The problem of complicated configuration is solved to a great extent . On this basis , And provides spi Mechanism , use spring.factories It can complete the automatic assembly of a small component ...

Random recommendation

  1. EC note , The second part :9. Not in construction 、 Calling virtual function in destructor

    9. Not in construction . Calling virtual function in destructor 1. What happens when virtual functions are called in constructors and destructors? ? #; } What kind of output does the program produce ? You must think it will output : cls2 make cls2 delete ...

  2. Customize ANDROID in EDITTEXT Medium HINT The size of the text

    EditText editText = (EditText) rootView.findViewById(R.id.et); // Create a new text object to which attributes can be added SpannableString ss ...

  3. Connect oracle Reading data

    I didn't use it very much oracle, And it's in the local tax intranet. It's estimated that it's the reason of the firewall, and it's not connected in the virtual machine oracle, It took a lot of trouble to find the problem at the beginning , Now it's time to give up using direct database , Write it down for later use public class Load ...

  4. dedecms How to add thumbnails to columns

    Making website with dream weaving program , Sometimes because of functional requirements , We want to add the thumbnail function to the column page of the website , and dedecms It doesn't come with this function , Then we need to modify the program . Here is a way to add a thumbnail to a column , For your reference . The documents involved are as follows ( ...

  5. Java Multithreading Simple example ( Consumers and producers ) The relationship between

    PS:: What is the thread PHP It's a completely non-existent concept , Further study is needed : PS:: This example is based on the expansion of knowledge in books , Level of understanding 50% Or so! ! 1. Define the production and consumption environment package second; pub ...

  6. Configure green on other computers Jre+tomcat Running environment

    Other colleagues will use our web Program ( be based on tomcat Of web Program ). So the requirement is that no program needs to be installed on the other party's computer , Copy our bags , Run the batch directly . After some groping , The implementation is as follows : 1, Get ready jr ...

  7. Project use emoji Details of expression pack and expression parsing process

    Vegetable chicken , I didn't write well at the beginning , If you have any questions, you are welcome to discuss with each other . Without further ado . Three plug-ins are used Emoji Picker First step This emoji It's better for me to find the expression pack plug-in One of the ,input You can't put a picture in a box , effect ...

  8. original :Docker Application in cloud housekeeping No copy and paste

    Our company is now using it on a large scale Docker, At present, in addition to database applications , All other applications are in the Docker Running in the container , Here I am Docker Do some sharing in the company's application .. First of all, let me introduce the background of the company , The company belongs to small and medium-sized start-up companies , service ...

  9. Let more than one HTML page Use The same paragraph HTML Code

    Demand background A website has multiple pages : A web page , It can be divided into many parts , for instance , Here is a very simple web structure :     In general ,footer Are used to identify the relevant information of the website ( Keep on record . Contact information . Made by ), Every page is a phase ...

  10. Tour HDU - 3488 Minimum weight covering of directed rings Cost stream

    http://acm.hdu.edu.cn/showproblem.php?pid=3488 For a passive sink , Digraph with edge weight Let you find the smallest Hamiltonian circuit It can be used KM Algorithmic writing , But the cost stream is OK Ideas 1 ...