Spring Boot Secret and actual combat source analysis-out of the box, hidden mystery

Source: Internet
Author: User

Article Directory

    1. 1. Out of the box, hidden in the mystery
    2. 2. Summary
    3. 3. Source Code

Spring boot provides a lot of "out-of-the-box" dependency modules, so how does spring boot skillfully do it out of the box and automatically configure it?

Out of the box, inside the hidden mystery

Spring Boot provides many "out-of-the-box" dependency modules, all named Spring-boot-starter-xx. For example, the previously mentioned Spring-boot-starter-redis, Spring-boot-starter-data-mongodb, Spring-boot-starter-data-elasticsearch, and so on.

Spring Boot's out-of-the-box is a great design that brings great convenience to developers. Once the developer has added dependencies to 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 skillfully do it out of the box and automatically configure it?

Now, we analyze the implementation principle of Spring Boot through the source code.

The core of auto-injection is to spring-boot-autoconfigure.jar this class library. Before analyzing, let's look at a few files.

  1. @Configuration
  2. @ConditionalOnClass({ jedisconnection. Class, redisoperations. Class, Jedis. Class })
  3. @EnableConfigurationProperties
  4. Public class redisautoconfiguration {}
  1. @Configuration
  2. @ConditionalOnClass({ Mongo. Class, mongorepository. Class })
  3. @ConditionalOnMissingBean({ mongorepositoryfactorybean. Class,
  4. mongorepositoryconfigurationextension. Class })
  5. @ConditionalOnProperty(prefix = "spring.data.mongodb.repositories",
  6. Name = "Enabled", havingvalue = "true", matchifmissing = true< c14>)
  7. @Import(mongorepositoriesautoconfigureregistrar. Class)
  8. @AutoConfigureAfter(mongodataautoconfiguration. Class)
  9. Public class mongorepositoriesautoconfiguration {}
  1. @Configuration
  2. @ConditionalOnClass({ Client. Class, transportclientfactorybean. Class,
  3. Nodeclientfactorybean. Class })
  4. @EnableConfigurationProperties(elasticsearchproperties. Class)
  5. Public class elasticsearchautoconfiguration implements Disposablebean {}

The above three sources correspond to Redis, MongoDB, ElasticSearch respectively. By contrast, we will find that they all have a feature, and there are @ConditionalOnClass annotations. This annotation is the key to the problem.

What is the role of @ConditionalOnClass? Let's start with a general understanding of the code below.

The main method in the source code is to query and match the corresponding classes in the @ConditionalOnClass parameters.

So what is the purpose of the query? The purpose of the query is to parse the corresponding configuration class if the corresponding class in the @ConditionalOnClass parameter exists in the Classpath directory, otherwise the configuration class of the callout adornment will not be resolved.

Therefore, Spring Boot's out-of-the-box implementation principle, it is very simple, in a word can be summed up.

Spring Boot internally provides many classes of automation configuration, such as Redisautoconfiguration, Mongorepositoriesautoconfiguration, Elasticsearchautoconfiguration, these automation-configured classes will determine if there is a class of their own in classpath, and if so, the relevant configuration will be automatically configured, otherwise it will not be automatically configured, so the developer in Maven's Pom When dependencies are added to the file, these dependencies will download many jar packages into the classpath, which will trigger an automated configuration, so we can easily use the functionality of the module.

Summary

How does Spring Boot skillfully do it out of the box and automatically configure it? In fact, Spring Boot internally provides a lot of automation-configured classes These automated configuration classes will determine if there is a class of their own in classpath, and if it does, it will automatically configure the relevant configuration, otherwise it will not be automatically configured, so the developer in Maven's Pom When dependencies are added to the file, these dependencies will download many jar packages into the classpath, which will trigger an automated configuration.

Source Code

Related example complete code: springboot-action

Finish



If you feel that my article is helpful to you, please feel free to make a reward.

    • Copyright NOTICE: This article was published by Liang in Liang Blog
    • Reprint statement: Free reprint-Non-commercial-non-derivative-maintain attribution (Creative Sharing 3.0 license), non-commercial reprint please indicate the author and source, commercial reprint please contact the author himself.
    • Article title: Spring Boot Secret and actual combat source analysis-out of the box to use, inside hidden mystery
    • Article Link: http://blog.720ui.com/2016/springboot_source_autoconfigure/

Spring Boot Secret and actual combat source analysis-out of the box, hidden mystery

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.