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:
- The first is the
@EnableConfigurationProperties
commentimport
of theEnableConfigurationPropertiesImportSelector
post processor; EnableConfigurationPropertiesImportSelector
ED postprocessorSpring
vessel is registeredConfigurationPropertiesBeanRegistrar
andConfigurationPropertiesBindingPostProcessorRegistrar
twobean
;- Wherein
ConfigurationPropertiesBeanRegistrar
theSpring
registration of the vesselXxxProperties
typebean
;ConfigurationPropertiesBindingPostProcessorRegistrar
theSpring
registration of the vesselConfigurationBeanFactoryMetadata
andConfigurationPropertiesBindingPostProcessor
two post-processor; ConfigurationBeanFactoryMetadata
Post processor initializationbean
factory
when the@Bean
annotation metadata stored for use in subsequent binding properties associated logic in external configuration;ConfigurationPropertiesBindingPostProcessor
Postprocessor external configuration attribute value is bound to theXxxProperties
Logical property entrusted toConfigurationPropertiesBinder
the object, thenConfigurationPropertiesBinder
the object and the logical property binding eventually entrusted toBinder
the object to complete.
Visible, it is important that the above step 5 .
2 Introduction
As we all know, SpringBoot built a variety of Starter
start-reliance, we are very easy to use, greatly reducing our development work. With the Starter
start-dependent, we do not have to consider what this project needs the library, this library groupId
and artifactId
what 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-web
the 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 tospring-web
andspring-webmvc
dependence; 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 Starter
is how to build?
3 Maven dependent optional label transfer
In the analysis of various SpringBoot built Starter
before construction principle, let us first know Maven under the optional
label, since the label play a crucial role.
Maven the optional
label indicates optional delivery means dependent i.e. not following directly give chestnut be described.
For example there A
, B
and C
three libraries, C
dependent B
, B
dependent A
. Here's a look at three library pom.xml
files:
// 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
, B
and a C
library of pom.xml
shows, B
library dependencies A
library, then C
the library in turn depends on the B
library, then please think about, Maven package to build C
the library, A
the library has not been introduced to?
The answer is definitely no , because the B
import library A
using the library relies <optional>true</optional>
, is about Maven's optional
label is set to a value true
, then C
the library re-introduced B
when the library dependencies, A
libraries will not be introduced to the C
library.
At the same time there is a transitive dependencies with Maven related exclusions
tag, 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 Starter
in 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:
<center>图1</center>
We all know, SpringBoot of Starter
the construction principle essence is automatically configured, and therefore can be seen from Figure 1 internal SpringBoot source projects with Starter
its automatic configuration has four related modules: spring-boot-starters
, spring-boot-actuator-autoconfigure
, spring-boot-autoconfigure
and 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-starters
the module automatically configured with the back of three related modules xxx-autoconfigure
relationship module of what is it?
At this point we take a look spring-boot-starters
inside the module What is the structure?
<center>图2</center>
2 can be seen from FIG spring-boot-starters
module contains various built SpringBoot starter
: spring-boot-starter-xxx
. Due to a variety of built-SpringBoot starter
too, with our usual spring-boot-starter-web
start to explore rely good.
We first look at spring-boot-starter-web
the internal structure of the module:
<center>图3</center>
You can see spring-boot-starter-web
inside the module only .flattened-pom.xml
and pom.xml
documents, without any code ! A bit beyond what we expected. We all know when to use SpringBoot introduction of web features spring-boot-starter-web
start to rely on, and now the spring-boot-starter-web
module there is no single line of code, then spring-boot-starter-web
exactly how to build it? It will now shown in Figure 1 spring-boot-autoconfigure
on the automatic configuration module?
At this point we need to look at spring-boot-starter-web
the module pom.xml
contents of the file:
<center>图4</center>
It can be seen in Figure 4, spring-boot-starter-web
the module dependency of spring-boot-starter
, spring-boot-starter-tomcat
, spring-web
and spring-webmvc
other modules, not actually dependent spring-boot-autoconfigure
automatic configuration module!
Since the spring-boot-starter-web
module to do with spring-boot-autoconfigure
auto-configuration module related, so the spring-boot-starter-web
module must be indirectly dependent on the spring-boot-autoconfigure
auto-configuration module.
Figure 4 marked marked "focus" of the spring-boot-starter
module is that most spring-boot-starter-xxx
module base on which module is the core Starter
, including automatic configuration, logging and YAML
support. At this point we have to pay attention at spring-boot-starter
the pom.xml
file, maybe it depends on the the spring-boot-autoconfigure
auto-configuration module.
<center>图5</center>
Can be seen from Figure 5, in front of us suspect that is not wrong, it is spring-boot-starter
module dependent on the spring-boot-autoconfigure
auto-configuration module! Thus, the here we can conclude: the spring-boot-starter-web
module is no code, but by spring-boot-starter
module indirectly dependent on the spring-boot-autoconfigure
auto-configuration module, in order to achieve its start-dependent function.
At this time, we look at spring-boot-autoconfigure
the internal structure of the automatic configuration of the module package:
<center>图6</center>
FIG 6 by the red box, we can know the spring-boot-starter-web
start of the auto-configuration dependent function originally by the spring-boot-autoconfigure
module web
class that implements the packet.
Here to spring-boot-starter-web
start 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 optional
label some effects.
To Get to the point, let's think about a problem: Usually we develop web
Why the project was introduced spring-boot-starter-web
after the start dependence spring-boot-autoconfigure
module web
associated with automatic configuration class will play automatically work?
We should know that an automated configuration class work is often due to classpath
the existence of a class, here to DispatcherServletAutoConfiguration
this class automatically configured as an entry point to Get the point better.
Look under DispatcherServletAutoConfiguration
conditions capable of automatic configuration is valid?
<center>图7</center>
It is shown in FIG. 7, DispatcherServletAutoConfiguration
one of the conditions that can be automatically configured @ConditionalOnClass(DispatcherServlet.class)
, that only classpath
exists in DispatcherServlet.class
the class, then DispatcherServletAutoConfiguration
the automatic configuration logic to work.
And DispatcherServlet
this class is spring-webmvc
the dependency library, as shown below:
<center>图8</center>
At this point we look at spring-boot-autoconfigure
the module pom.xml
file to introduce spring-webmvc
the situation-dependent:
<center>图9</center>
Shown in FIG. 9, spring-boot-autoconfigure
the module introduced spring-webmvc
when this dependency optional
is set true
, the original is optional dependent. I.e., spring-webmvc
this will depend library is introduced into spring-boot-autoconfigure
the module, instead of being introduced into indirectly dependent spring-boot-autoconfigure
module spring-boot-starter-web
the start dependence.
At this point, let us look at spring-boot-starter-web
the pom.xml
dependency of documentation:
<Center> FIG. 10 </ center>
As shown in FIG. 10, spring-boot-starter-web
the initial dependency explicitly introduces spring-webmvc
the dependencies, i.e. the introduction of spring-webmvc
no time optional
label, and because DispatcherServlet
is in this class spring-webmvc
, so that the dependencies of classpath
the presence of DispatcherServlet
this class, DispatcherServletAutoConfiguration
this class will come into effect automatically configured. Of course, web
related to other auto-configuration class is to take effect this principle.
At this point, we also understand spring-boot-autoconfigure
why the module should introduced spring-webmvc
this dependence as optional dependencies, and its purpose is to the spring-boot-starter-web
energy dependence explicitly introduced starting spring-webmvc
this dependence (this play a decisive role), so we have developed web projects as long as the introduction of spring-boot-starter-web
start dependence, then automatically configure web-related classes will take effect, so out of the box this is the spring-boot-starter-web
construction principle of the start of a dependent.
Mentioned above spring-boot-starter-actuator
, spring-boot-starter-test
and other built- spring-boot-starter-xxx
in started construction principle relies too, but spring-boot-starter-actuator
relies spring-boot-actuator-autoconfigure
, spring-boot-starter-test
relies spring-boot-test-autoconfigure
module fills not elaborate.
Thoughts :
spring-boot-actuator-autoconfigure
Thepom.xml
document was introduced more than 20 optional dependency, and why thespring-boot-starter-actuator
start rely only introducedmicrometer-core
this dependency it?
5 SpringBoot mimic a custom package structure Starter
SpringBoot previous analysis of the various built- Starter
building principles, theory with practice, so if we can practice hands-on look at the custom Starter
so much the better.
The following provides a custom Starter
of a simple Demo
, this Demo
fully mimic SpringBoot
the built-in Starter
internal structure of the package to write, learn more about SpringBoot built for various Starter
construction principle helpful.
Here is the Demo
the 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 Starter
construction principles of analysis to this end, will now summarize the key points:
spring-boot-starter-xxx
Starting no code-dependent, but directly or indirectly dependent on thexxx-autoconfigure
module, thexxx-autoconfigure
module takes thespring-boot-starter-xxx
realization of automatic configuration of the start-dependent;xxx-autoconfigure
Automatic configuration module introduces some optional dependencies, these dependencies are not transmitted to the optionalspring-boot-starter-xxx
start dependency, which is constructed starting dependent key ;spring-boot-starter-xxx
Start dependency explicitly introduces some dependence on the optional auto-configuration function;- 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
bean
and 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.