SpringBoot built-in Starter is how to build a variety of? --SpringBoot source (VI)

Note: The source code analysis corresponds SpringBoot version 2.1.0.RELEASE

1 Reviewing the Old

Benpian access external configuration property value is tied to how XxxProperties class properties? --SpringBoot source (V)

Reviewing the Old, let's briefly review the contents of the articles, the one we analyzed SpringBoot external configuration property value is tied to how XxxProperties class property -related source code, now an important step in the external property binding summary as follows:

  1. The first is the @EnableConfigurationPropertiescomment importof the EnableConfigurationPropertiesImportSelectorpost processor;
  2. EnableConfigurationPropertiesImportSelectorED postprocessor Springvessel is registered ConfigurationPropertiesBeanRegistrarand ConfigurationPropertiesBindingPostProcessorRegistrartwo bean;
  3. Wherein ConfigurationPropertiesBeanRegistrarthe Springregistration of the vessel XxxPropertiestype bean; ConfigurationPropertiesBindingPostProcessorRegistrarthe Springregistration of the vessel ConfigurationBeanFactoryMetadataand ConfigurationPropertiesBindingPostProcessortwo post-processor;
  4. ConfigurationBeanFactoryMetadataPost processor initialization bean factorywhen the @Beanannotation metadata stored for use in subsequent binding properties associated logic in external configuration;
  5. ConfigurationPropertiesBindingPostProcessorPostprocessor external configuration attribute value is bound to the XxxPropertiesLogical property entrusted to ConfigurationPropertiesBinderthe object, then ConfigurationPropertiesBinderthe object and the logical property binding eventually entrusted to Binderthe object to complete.

Visible, it is important that the above step 5 .

2 Introduction

As we all know, SpringBoot built a variety of Starterstart-reliance, we are very easy to use, greatly reducing our development work. With the Starterstart-dependent, we do not have to consider what this project needs the library, this library groupIdand artifactIdwhat is? Not to worry about the introduction of this version of the library will have no conflict with other dependencies.

For chestnut : Now we want to develop a web project, as long as the introduction of spring-boot-starter-webthe starting rely on it, without regard to the introduction of which depend on which version of the. As before we have to consider what dependencies introduced, such as the introduction to spring-weband spring-webmvcdependence; Also, consider which version of the introduction of these libraries will not conflict with other libraries.

So we do not analyze the source code SpringBoot automatically configured today, due to the relationship started with automatic configuration is dependent on the relationship go hand in hand, so Benpian first stand in the perspective maven project to build we usually use the macro analysis SpringBoot built-in variety Starteris how to build?

3 Maven dependent optional label transfer

In the analysis of various SpringBoot built Starterbefore construction principle, let us first know Maven under the optionallabel, since the label play a crucial role.
Maven the optionallabel indicates optional delivery means dependent i.e. not following directly give chestnut be described.

For example there A, Band Cthree libraries, Cdependent B, Bdependent A. Here's a look at three library pom.xmlfiles:

// A的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <groupId>com.ymbj</groupId>
        <artifactId>A</artifactId>
    <version>1.0-SNAPSHOT</version>

</project>

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <groupId>com.ymbj</groupId>
        <artifactId>B</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--注意是可选依赖-->
    <dependencies>
        <dependency>
            <groupId>com.ymbj</groupId>
            <artifactId>A</artifactId>
            <version>1.0-SNAPSHOT</version>
        <optional>true</optional>
        </dependency>
    </dependencies>

</project>

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <groupId>com.ymbj</groupId>
        <artifactId>C</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.ymbj</groupId>
            <artifactId>B</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

Three above A, Band a Clibrary of pom.xmlshows, Blibrary dependencies Alibrary, then Cthe library in turn depends on the Blibrary, then please think about, Maven package to build Cthe library, Athe library has not been introduced to?

The answer is definitely no , because the Bimport library Ausing the library relies &lt;optional&gt;true&lt;/optional&gt;, is about Maven's optionallabel is set to a value true, then Cthe library re-introduced Bwhen the library dependencies, Alibraries will not be introduced to the Clibrary.

At the same time there is a transitive dependencies with Maven related exclusionstag, this would represent a child a library dependencies excluded, not described in detail here.

4 SpringBoot built-in Starter is how to build a variety of?

We now explore various SpringBoot built Starterin the end is how to build it?

Remember how to analyze SpringBoot source module and structure? The relationship between the interior of the module SpringBoot analysis of this post? First look at the source code SpringBoot internal block diagram:

SpringBoot built-in Starter is how to build a variety of?  --SpringBoot source (VI)
<center>图1</center>

We all know, SpringBoot of Starterthe construction principle essence is automatically configured, and therefore can be seen from Figure 1 internal SpringBoot source projects with Starterits automatic configuration has four related modules: spring-boot-starters, spring-boot-actuator-autoconfigure, spring-boot-autoconfigureand spring-boot-test-autoconfigure. Look at the role of each module how to analyze SpringBoot source module and structure? The article, not repeat them here.

So, spring-boot-startersthe module automatically configured with the back of three related modules xxx-autoconfigurerelationship module of what is it?

At this point we take a look spring-boot-startersinside the module What is the structure?

SpringBoot built-in Starter is how to build a variety of?  --SpringBoot source (VI)
<center>图2</center>

2 can be seen from FIG spring-boot-startersmodule contains various built SpringBoot starter: spring-boot-starter-xxx. Due to a variety of built-SpringBoot startertoo, with our usual spring-boot-starter-webstart to explore rely good.

We first look at spring-boot-starter-webthe internal structure of the module:

SpringBoot built-in Starter is how to build a variety of?  --SpringBoot source (VI)
<center>图3</center>

You can see spring-boot-starter-webinside the module only .flattened-pom.xmland pom.xmldocuments, without any code ! A bit beyond what we expected. We all know when to use SpringBoot introduction of web features spring-boot-starter-webstart to rely on, and now the spring-boot-starter-webmodule there is no single line of code, then spring-boot-starter-webexactly how to build it? It will now shown in Figure 1 spring-boot-autoconfigureon the automatic configuration module?

At this point we need to look at spring-boot-starter-webthe module pom.xmlcontents of the file:

SpringBoot built-in Starter is how to build a variety of?  --SpringBoot source (VI)
<center>图4</center>

It can be seen in Figure 4, spring-boot-starter-webthe module dependency of spring-boot-starter, spring-boot-starter-tomcat, spring-weband spring-webmvcother modules, not actually dependent spring-boot-autoconfigureautomatic configuration module!

Since the spring-boot-starter-webmodule to do with spring-boot-autoconfigureauto-configuration module related, so the spring-boot-starter-webmodule must be indirectly dependent on the spring-boot-autoconfigureauto-configuration module.

Figure 4 marked marked "focus" of the spring-boot-startermodule is that most spring-boot-starter-xxxmodule base on which module is the core Starter, including automatic configuration, logging and YAMLsupport. At this point we have to pay attention at spring-boot-starterthe pom.xmlfile, maybe it depends on the the spring-boot-autoconfigureauto-configuration module.

SpringBoot built-in Starter is how to build a variety of?  --SpringBoot source (VI)
<center>图5</center>

Can be seen from Figure 5, in front of us suspect that is not wrong, it is spring-boot-startermodule dependent on the spring-boot-autoconfigureauto-configuration module! Thus, the here we can conclude: the spring-boot-starter-webmodule is no code, but by spring-boot-startermodule indirectly dependent on the spring-boot-autoconfigureauto-configuration module, in order to achieve its start-dependent function.

At this time, we look at spring-boot-autoconfigurethe internal structure of the automatic configuration of the module package:

SpringBoot built-in Starter is how to build a variety of?  --SpringBoot source (VI)
<center>图6</center>

FIG 6 by the red box, we can know the spring-boot-starter-webstart of the auto-configuration dependent function originally by the spring-boot-autoconfiguremodule webclass that implements the packet.

Here to spring-boot-starter-webstart construction of the basic principles we rely on clear, but there is a particularly important key point we have not Get to. The key point with Maven's optionallabel some effects.

To Get to the point, let's think about a problem: Usually we develop webWhy the project was introduced spring-boot-starter-webafter the start dependence spring-boot-autoconfiguremodule webassociated with automatic configuration class will play automatically work?

We should know that an automated configuration class work is often due to classpaththe existence of a class, here to DispatcherServletAutoConfigurationthis class automatically configured as an entry point to Get the point better.
Look under DispatcherServletAutoConfigurationconditions capable of automatic configuration is valid?

SpringBoot built-in Starter is how to build a variety of?  --SpringBoot source (VI)
<center>图7</center>

It is shown in FIG. 7, DispatcherServletAutoConfigurationone of the conditions that can be automatically configured @ConditionalOnClass(DispatcherServlet.class), that only classpathexists in DispatcherServlet.classthe class, then DispatcherServletAutoConfigurationthe automatic configuration logic to work.

And DispatcherServletthis class is spring-webmvcthe dependency library, as shown below:

SpringBoot built-in Starter is how to build a variety of?  --SpringBoot source (VI)
<center>图8</center>

At this point we look at spring-boot-autoconfigurethe module pom.xmlfile to introduce spring-webmvcthe situation-dependent:

SpringBoot built-in Starter is how to build a variety of?  --SpringBoot source (VI)
<center>图9</center>

Shown in FIG. 9, spring-boot-autoconfigurethe module introduced spring-webmvcwhen this dependency optionalis set true, the original is optional dependent. I.e., spring-webmvcthis will depend library is introduced into spring-boot-autoconfigurethe module, instead of being introduced into indirectly dependent spring-boot-autoconfiguremodule spring-boot-starter-webthe start dependence.

At this point, let us look at spring-boot-starter-webthe pom.xmldependency of documentation:
SpringBoot built-in Starter is how to build a variety of?  --SpringBoot source (VI)
<Center> FIG. 10 </ center>

As shown in FIG. 10, spring-boot-starter-webthe initial dependency explicitly introduces spring-webmvcthe dependencies, i.e. the introduction of spring-webmvcno time optionallabel, and because DispatcherServletis in this class spring-webmvc, so that the dependencies of classpaththe presence of DispatcherServletthis class, DispatcherServletAutoConfigurationthis class will come into effect automatically configured. Of course, webrelated to other auto-configuration class is to take effect this principle.

At this point, we also understand spring-boot-autoconfigurewhy the module should introduced spring-webmvcthis dependence as optional dependencies, and its purpose is to the spring-boot-starter-webenergy dependence explicitly introduced starting spring-webmvcthis dependence (this play a decisive role), so we have developed web projects as long as the introduction of spring-boot-starter-webstart dependence, then automatically configure web-related classes will take effect, so out of the box this is the spring-boot-starter-webconstruction principle of the start of a dependent.

Mentioned above spring-boot-starter-actuator, spring-boot-starter-testand other built- spring-boot-starter-xxxin started construction principle relies too, but spring-boot-starter-actuatorrelies spring-boot-actuator-autoconfigure, spring-boot-starter-testrelies spring-boot-test-autoconfiguremodule fills not elaborate.

Thoughts : spring-boot-actuator-autoconfigureThe pom.xmldocument was introduced more than 20 optional dependency, and why the spring-boot-starter-actuatorstart rely only introduced micrometer-corethis dependency it?

5 SpringBoot mimic a custom package structure Starter

SpringBoot previous analysis of the various built- Starterbuilding principles, theory with practice, so if we can practice hands-on look at the custom Starterso much the better.

The following provides a custom Starterof a simple Demo, this Demofully mimic SpringBootthe built-in Starterinternal structure of the package to write, learn more about SpringBoot built for various Starterconstruction principle helpful.

Here is the Demothe github address and recommend it to small partners who are interested.
Springboot mimic the internal structure of a custom Starter . In addition, how to customize a Starter, Mybatis can refer to the spring-boot-starter what is written.

6 Summary

Well, SpringBoot built in various Starterconstruction principles of analysis to this end, will now summarize the key points:

  1. spring-boot-starter-xxxStarting no code-dependent, but directly or indirectly dependent on the xxx-autoconfiguremodule, the xxx-autoconfiguremodule takes the spring-boot-starter-xxxrealization of automatic configuration of the start-dependent;
  2. xxx-autoconfigureAutomatic configuration module introduces some optional dependencies, these dependencies are not transmitted to the optional spring-boot-starter-xxxstart dependency, which is constructed starting dependent key ;
  3. spring-boot-starter-xxxStart dependency explicitly introduces some dependence on the optional auto-configuration function;
  4. After the above-prepared 3-step, we started the project as long as the introduction of a dependent, you can use out of the box, without having to manually create some beanand so on.

The original is not easy, help point a praise chant!

As the author is limited, if there is an error in the text Please also indicate, thank you.

Reference:
. 1, Maven dependent thorough understanding transitive


[Source] welcome attention to the notes public number, along with the exchange of learning.
SpringBoot built-in Starter is how to build a variety of?  --SpringBoot source (VI)

Guess you like

Origin blog.51cto.com/14747176/2478625