Analysis of Spring Boot principle

We learned that Spring Boot provides a lot of out-of-the-box dependency modules. As long as developers add related dependencies in the Maven pom file, Spring Boot will automatically create and inject the required Spring Bean into the context for this application.

In this article, we take FreeMarker's automatic configuration as an example, focusing on explaining the working principle and loading process. Because FreeMarker is relatively simple, there are only three classes in the Spring Boot source code, so it is relatively easy to understand as a case.

Analysis of Spring Boot principle
What does EnableAutoConfiguration help us do?
Do you remember @EnableAutoConfiguration annotation?

Let's review it first.

@RestController
@EnableAutoConfiguration
@ComponentScan(basePackages = { "com.lianggzone.springboot" })
public class WebMain {
        public static void main(String[] args) throws Exception {
                SpringApplication.run(WebMain.class, args);
        }
}

So, now we analyze the source code of @EnableAutoConfiguration.
Analysis of Spring Boot principle

Here, the key is that the import autoimportImportSelector class imported by @Import annotation is the most important thing is to scan the spring.factories file through SpringFactoriesLoader.loadFactoryNames in the getCandidateConfigurations method.
Analysis of Spring Boot principle

Now, we are looking at the SpringFactoriesLoader source code.
Analysis of Spring Boot principle

Suddenly, did you find that the spring.factories file is quite important? That's right, Spring Boot scans the contents of this file to determine which automatic configurations are available. Taking FreeMarker as an example, let's see how it is configured.
Analysis of Spring Boot principle

Therefore, Spring Boot scans the Spring.factories file in the EnableAutoConfiguration parameter for automatic configuration and loads it.

Configuration parameter class-FreeMarkerProperties
configuration parameters here can be set directly in application.properties. We found that its prefix must be spring.freemarker.
Analysis of Spring Boot principle

Automatic configuration class-The
core annotation of FreeMarkerAutoConfiguration
Analysis of Spring Boot principle
is mentioned in "Spring Boot Demystifying and Practical Source Code Analysis-Out of the Box, Built-in Mystery", it is mentioned that the corresponding class in the @ConditionalOnClass parameter will only be resolved when it exists in the classpath directory Configuration class, otherwise the configuration class modified by the annotation is not parsed.

Spring Boot provides a lot of automatic configuration classes, such as RedisAutoConfiguration, MongoRepositoriesAutoConfiguration, and ElasticsearchAutoConfiguration. These automatic configuration classes will determine whether the class you need exists in the classpath. If it exists, it will automatically configure the related configuration, otherwise it will not Automatic configuration, therefore, after the developer adds related dependencies in the pom file of Maven, these dependencies will download many jar packages to the classpath. With these libs, automatic configuration will be triggered, so we can easily use the Of the module's function.

In addition, there is a main annotation @EnableConfigurationProperties, which is mainly used to load the configuration parameter class we mentioned above.

Injecting
Analysis of Spring Boot principle
the source code of Bean is very easy to understand, I mainly want to talk about 2 notes.

The first annotation is @ConditionalOnMissingBean (name = "freeMarkerViewResolver"), which specifies processing when the container does not specify a Bean.

The second comment is, @ConditionalOnProperty, whether the specified property has the specified value. In other words, if there is no configuration in application.properties, the default is true, which means the condition is met.

Guess you like

Origin blog.51cto.com/14765930/2486704