The principle of springBoot and the use of configuration files

1. Preliminary understanding of springboot's automatic loading principle:

  When we create a springboot project and introduce a web starter, we can use the functions of springmvc, but we have not configured the springmvc.xml/web.xml file, how does it start?

2. How does springBoot manage dependencies:

Open pom.xml, we can see a parent project:
Insert picture description here
we click into this parent project and find that there is a parent project inside:
Insert picture description here
we enter this and find that it is all dependent coordinates and version numbers, etc.:
Insert picture description here
  this one It is the version control center of springboot (springboot version arbitration mechanism), which contains the dependencies that our normal environment needs. We can import dependencies in the future without writing versions, but if the dependencies we use are not in this, we need Normal import dependencies.
  When the dependency version imported by springboot does not meet our needs, we can change its default version:
Insert picture description here
Insert picture description here


Starter:
Insert picture description here
spring-boot-starter-web: help us import the components that the web module depends on for normal operation;
spring-boot-starter-test: help us import the components that the test module depends on for normal operation.
The general meaning of the starter:
  Springboot helps us extract all the environments required by the scene, and encapsulate them into one starter. When we need to use any scene, we only need to import the corresponding starter. Will import all the dependencies we need automatically. For example: we need to use redis, we only need to import the spring-boot-starter-redis this starter.



Automatic assembly

  When we create a brand-new project, it will automatically generate a class for us. There is a main method in it, and we can start the project:
Insert picture description here
we can see that there is an annotation on the class:
  @SpringBootApplication: This annotation means The class marked with this annotation is our startup class. Springboot will run the main method of this startup class to start our program.


Let's click in and see:
Insert picture description here
there are three new annotations provided by springboot:

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {
    
    @Filter(
    type = FilterType.CUSTOM,
    classes = {
    
    TypeExcludeFilter.class}
),
@Filter(
    type = FilterType.CUSTOM,
    classes = {
    
    AutoConfigurationExcludeFilter.class}
)}
)

@ComponentScan Annotation:
  Function: Automatically scan and load qualified components or beans, load this bean definition into the IOC container, and here is to exclude those classes and prevent them from being loaded into the ioc container, and the package scanning path is not specified
@ SpringBootConfiguration:
  Function: the configuration class of SpringBoot, and load the class into the ioc container, and mark it on a certain class, indicating that this is a configuration class of SpringBoot;


@EnableAutoConfiguration:
  The annotation to enable automatic assembly is a core annotation, indicating Tell springboot to enable the automatic configuration function.
We click into this annotation: we
Insert picture description here
found that there are two more annotations in this one:
@AutoConfigurationPackage: Automatically import the configuration package
. There is also an annotation in this annotation
Insert picture description here
@Import:spring bottom-level annotation, which imports the component
Registrar.class into the container, which means the main program class All the classes under the package and sub-packages are scanned into the spring container.
View the underlying code debugging:
Insert picture description here
This is why we need to build a sub-package under the package or package of the main program


After reading this, we will return to the previous level to view.

Insert picture description here
This annotation is to load this class into our spring container, and we can find a method in it:
Insert picture description here
this method calls another method:
Insert picture description here
this method calls the LoadSpringFactories method, click this method:
Insert picture description here

Here we can see a file, the directory where this file is located: under the
spring-boot-autoconfigure package, which is the automatic configuration package.
Insert picture description here
We open this file:
Insert picture description here

I found that there are many fully qualified class names configured, and found that his naming format is in the xxxAutoconfiguration format, that is, xxx is the environment name we need.
We found a configuration that we are familiar with:
Insert picture description here
click on this class:
Insert picture description here
this class is a configuration class with a lot of annotations on it, and these annotations are used to determine when to load this configuration class.


@ConditionalOnBean:当容器中有指定的Bean的条件下  
@ConditionalOnClass:当类路径下有指定的类的条件下  
@ConditionalOnExpression:基于SpEL表达式作为判断条件  
@ConditionalOnJava:基于JVM版本作为判断条件  
@ConditionalOnJndi:在JNDI存在的条件下查找指定的位置  
@ConditionalOnMissingBean:当容器中没有指定Bean的情况下  
@ConditionalOnMissingClass:当类路径下没有指定的类的条件下  
@ConditionalOnNotWebApplication:当前项目不是Web项目的条件下  
@ConditionalOnProperty:指定的属性是否有指定的值  
@ConditionalOnResource:类路径下是否有指定的资源  
@ConditionalOnSingleCandidate:当指定的Bean在容器中只有一个,或者在有多个Bean的情况下,用来指定首选的Bean

If the conditions are met, then this configuration class will be loaded into our spring container through reflection, which is automatic assembly.

Understand the summary by yourself:
  springboot will automatically help us to form all the files we need to configure into the form of configuration classes, and put the full class names of these configuration classes into the spring-boot-autoconfigure package Meta-info/spring In the .factories file, when we start, we will load this file, and load the configuration classes that meet the conditions into our spring container through reflection, so that our environment does not need to be configured again.
  Maybe this condition is related to the corresponding initiator.
  Reference from: Notes of Mad God Talking

extract:

SpringBoot obtains the value specified by EnableAutoConfiguration from META-INF/spring.factories under the classpath when it starts.

Import these values ​​into the container as the auto-configuration class, and the auto-configuration class will take effect, helping us with the auto-configuration work;

The entire J2EE overall solution and automatic configuration are in the jar package of springboot-autoconfigure;

It will import a lot of automatic configuration classes (xxxAutoConfiguration) into the container, that is, import all the components needed for this scenario into the container, and configure these components;

With the automatic configuration class, we can avoid the work of manually writing configuration injection functional components;

Summary:
• SpringBoot first loads all auto-configuration classes xxxxxAutoConfiguration
• Each auto-configuration class takes effect according to conditions, and by default it will bind the value specified by the configuration file. Take it inside xxxxProperties. xxxProperties is bound to the configuration file
• The effective configuration class will assemble many components in the
container • As long as these components are in the container, these functions are equivalent to these functions
• Customized configuration
• The user directly replaces the underlying components with @Bean
• The user can modify the value of the configuration file obtained by this component.
xxxxxAutoConfiguration —> Component —> Value in xxxxProperties----> application.properties

Guess you like

Origin blog.csdn.net/weixin_43431123/article/details/112704128