Spring Boot Secrets and Actual Source Code Analysis - Out of the Box, Hidden Mysteries

foreword

Spring Boot provides a lot of "out-of-the-box" dependency modules, so how does Spring Boot cleverly implement out-of-the-box and automatic configuration?

Out of the box, hidden secrets

Spring Boot provides many "out-of-the-box" dependency modules, all named after spring-boot-starter-xx. For example, the aforementioned spring-boot-starter-redis, spring-boot-starter-data-mongodb, spring-boot-starter-data-elasticsearch, etc.

Out of the box, Spring Boot is a great design and brings great convenience to developers. As long as the developer adds the relevant dependencies in the Maven pom file, Spring Boot will automatically create and inject the required Spring Beans into the context for the application.

So, how does Spring Boot cleverly do it out of the box and automatically configure it?

Now, let's analyze the implementation principle of Spring Boot in depth through the source code.

The core of automatic injection lies in the class library spring-boot-autoconfigure.jar. Before analyzing, let's look at a few files.

@Configuration
@ConditionalOnClass({ JedisConnection.class, RedisOperations.class, Jedis.class })
@EnableConfigurationProperties
public class RedisAutoConfiguration {}
@Configuration
@ConditionalOnClass({ Mongo.class, MongoRepository.class })
@ConditionalOnMissingBean({ MongoRepositoryFactoryBean.class,
    MongoRepositoryConfigurationExtension.class })
@ConditionalOnProperty(prefix = "spring.data.mongodb.repositories", 
    name = "enabled", havingValue = "true", matchIfMissing = true)
@Import(MongoRepositoriesAutoConfigureRegistrar.class)
@AutoConfigureAfter(MongoDataAutoConfiguration.class)
public class MongoRepositoriesAutoConfiguration {}
@Configuration
@ConditionalOnClass({ Client.class, TransportClientFactoryBean.class,
    NodeClientFactoryBean.class })
@EnableConfigurationProperties(ElasticsearchProperties.class)
public class ElasticsearchAutoConfiguration implements DisposableBean {}

The above three source codes correspond to Redis, MongoDB, and ElasticSearch respectively. By comparison, we will find that they all have one characteristic, and they all have the @ConditionalOnClass annotation. This annotation is the crux of the problem.

What does @ConditionalOnClass do? Let's first understand the code below.

write picture description here

The method in the source code is mainly to query and match the corresponding class in the parameters of @ConditionalOnClass.

So, what is the purpose of the query? The purpose of the query is to parse the corresponding configuration class only when the corresponding class in the @ConditionalOnClass parameter exists in the classpath directory, otherwise the configuration class modified by the annotation will not be parsed.

Therefore, the out-of-the-box implementation principle of Spring Boot is very simple and can be summarized in one sentence.

Spring Boot provides many automatic configuration classes, such as RedisAutoConfiguration , MongoRepositoriesAutoConfiguration , ElasticsearchAutoConfiguration , these automatic configuration classes will determine whether the class you need exists in the classpath, and if it exists, it will automatically configure the relevant configuration, otherwise it will not Automatic configuration. Therefore, after developers add relevant dependencies to Maven's pom file, these dependencies will download many jar packages to the classpath. With these libs, automatic configuration will be triggered. Therefore, we can easily use for function of the module.

Summarize

How does Spring Boot cleverly do it out of the box and automatically configure it? In fact, Spring Boot provides many automatically configured classes. These automatically configured classes will determine whether the class you need exists in the classpath. If there is, it will automatically configure the relevant configuration, otherwise it will not be automatically configured. Therefore, development After the user adds related dependencies in Maven's pom file, these dependencies will download many jar packages to the classpath, and with these libs, automatic configuration will be triggered.

Reprint address: http://blog.720ui.com/2016/springboot_source_autoconfigure/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326270370&siteId=291194637