Spring Boot Secret and the actual combat source analysis-Working principle analysis

Source: Internet
Author: User

Article Directory

    1. 1. enableautoconfiguration help us do what
    2. 2.  configuration parameter class –freemarkerproperties
    3. 3.  automatic configuration class –freemarkerautoconfiguration 4.  extended read
      1. 3.1.  core annotations
      2. 3.2.  inject Bean

Combined with the "Spring Boot Disclosure and actual combat source analysis-out-of-the-box, hidden Mystery," a article, we will be in-depth understanding of how Spring boot works.

In the "Spring boot secret and actual combat source analysis-out of the box, hidden mystery" article, we understand that spring boot provides a lot of out-of-the-box dependent modules, developers simply add dependencies in Maven's pom file, Spring boot will be The required Spring beans are automatically created and injected into the context.

In this article, we take Freemarker's automatic configuration as an example, focusing on the working principle and loading process. Because Freemarker relatively simple, Spring Boot source only three classes, so as a relatively good understanding of the case.

Enableautoconfiguration helped us do something.

Do you remember @EnableAutoConfiguration annotations?

Let's go back to the next.

  1. @RestController
  2. @EnableAutoConfiguration
  3. @ComponentScan(basepackages = { "com.lianggzone.springboot" })
  4. Public class webmain {
  5. public static void main(String[] args) throws Exception {
  6. springapplication. Run(webmain. Class, args);
  7. }
  8. }

Well, now we analyze the source of the @EnableAutoConfiguration.

The key to this is that the Enableautoconfigurationimportselector class that @Import annotation imports is most critical in the Getcandidateconfigurations method Springfactoriesloader.loadfactorynames Scan the spring.factories file.

Now, we are looking at the Springfactoriesloader source code.

Suddenly, did you find the Spring.factories file is quite important? Yes, Spring Boot determines what is configured automatically by scanning the contents of this file. Take Freemarker as an example, let's see how it's configured.

Therefore, Spring Boot scans the enableautoconfiguration parameter in the Spring.factories file for automatic configuration and loading.

configuration parameter Class –freemarkerproperties

Here the configuration parameters can be set directly through the application.properties. We found that its prefix must be spring.freemarker.

automatic Configuration Class –freemarkerautoconfigurationCore Annotations

In the "Spring Boot secret and real-time source analysis-out of the box, the hidden mystery" article, there is a reference to @ConditionalOnClass parameters corresponding to the class in the Classpath directory exists, will parse the corresponding configuration class, otherwise do not resolve the annotation decorated configuration class.

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.

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

Inject Bean

This source code very good understanding, I mainly want to speak 2 annotations.

The first annotation is the @ConditionalOnMissingBean (name = "Freemarkerviewresolver"), which specifies the processing if the container does not have a bean specified.

The second annotation is that, @ConditionalOnProperty, whether the specified property has a specified value for processing, in other words, if the application.properties is not configured, the default is true, that is, the condition conforms.

Extended Reading
    • Spring Boot uncover and combat your own implementation of a simple automatic configuration module

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-Working principle analysis
    • Article Link: http://blog.720ui.com/2017/springboot_source_autoconfigure_run/

Spring Boot Secret and the actual combat source analysis-Working principle analysis

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.