Recently, the company's original framework for the micro-service upgrade, using the Springcould framework, Springboot is the basis of springcloud, with the continuous understanding of springboot, more and more to find it strong, here simply summarize what you learned;
The interesting thing about Springboot is that his startup class, which can be run with a simple main method, removes the annoying XML configuration in spring, it can be packaged directly into a Jar/war package, and the Jar/war package can be run directly, No additional configuration required;
There is a very important note in the Startup class: @SpringBootApplication; in fact, it is a conforming annotation, look at the source code can see:
@Target ({elementtype.type})
@Retention (retentionpolicy.runtime)
@Documented
@Inherited
@ Springbootconfiguration
@EnableAutoConfiguration
@ComponentScan (
excludefilters = { @Filter (
type = Filtertype.custom,
classes = {Typeexcludefilter.class}
), @Filter (
type = Filtertype.custom,
classes = {Autoconfigurationexcludefilter.class}
)}
) public
@interface springbootapplication {
Where @springboootconfiguration is also a consistent annotation
@Target ({elementtype.type})
@Retention (retentionpolicy.runtime)
@Documented
@Configuration
Public @interface springbootconfiguration {
}
Many of these annotations are tagged with meta information: But in fact, the most important thing for Springboot is a "three-body" structure: three annotations.
@Configuration
@EnableAutoConfiguration
@ComponentScan
So the start-class annotations are written in the following form and are the same as the original use of @springbootapplication
@ComponentScan
@Configuration
@EnableAutoConfiguration public
class DemoApplication {
public static void Main (string[] args) {
springapplication.run (demoapplication.class, args);
}
}
Here @configuration is not unfamiliar to us, it is the Javaconfig form of the spring IOC container configuration class use that @confinguration; since Springboot application is a spring application, It is also natural to load the configuration of an IOC container, while the Springboot community recommends the use of the Javaconfig form; So it's clear that the startup class here is labeled @configuration and is actually a configuration class for the IOC container. This is something in spring, nothing special;
@EnableAutoConfiguration: Also a composite annotation, Springboot introduces automatic assembly
There are various definitions in spring that start with enable: @EnableScheduling, @EnableMBeanExpot, @EnableAutoConfiguration is based on these annotations, The simple generalization is that with the support of @import, the phone and registering a variety of specific scenario-related bean definitions:
For example, @enablescheduling is loaded into the IOC container by @import The bean associated with the spring dispatch framework;
The @EnableMBeanExport is to load JMX-related beans into the IOC container through @import, and most importantly in the various composite annotations that make up the @enableautoconfiguration @import ({ Enableautoconfigurationimportselector.class}) with the Enableautoconfigurationimportselector class, @ Enableautoconfiguration can help springboot load all eligible @conguration configurations into the current Springboot and create an IOC container;
Automatic assembly @EnableAutoConfiguration annotations can be summarized as follows: Search all meta-inf/from class path The spring.factories configuration file that instantiates the org.springframework.boot.autoconfigure.EnableAutoConfiguration corresponding configuration item as a callout by reflection The configuration and Javaconfig form of the IOC container Config class, then summarized as one and loaded into the IOC container;
# Propertysource Loaders org.springframework.boot.env.propertysourceloader=\
Org.springframework.boot.env.propertiespropertysourceloader,\
Org.springframework.boot.env.YamlPropertySourceLoader # Run Listeners
Org.springframework.boot.springapplicationrunlistener=\
Org.springframework.boot.context.event.EventPublishingRunListener # Application Context initializers
Org.springframework.context.applicationcontextinitializer=\
Org.springframework.boot.context.configurationwarningsapplicationcontextinitializer,\
Org.springframework.boot.context.contextidapplicationcontextinitializer,\
Org.springframework.boot.context.config.DelegatingApplicationContextInitializer # application Listeners
Org.springframework.context.applicationlistener=\
Org.springframework.boot.builder.parentcontextcloserapplicationlistener,\
Org.springframework.boot.cloudfoundry.vcapapplicationlistener,\
Org.springframework.boot.context.fileencodingapplicationlistener,\ Org.springframework.boot.context.config.AnsiOutputAppliCationlistener,\ org.springframework.boot.context.config.configfileapplicationlistener,\
Org.springframework.boot.context.config.delegatingapplicationlistener,\
Org.springframework.boot.liquibase.liquibaseservicelocatorapplicationlistener,\
Org.springframework.boot.logging.classpathloggingapplicationlistener,\ Org.springframework.boot.logging.LoggingApplicationListener
The above is the content from the Meta-inf/spring.factories configuration file in the Autoconfigura dependency package from Springboot, which can be a good illustration of the problem: according to the @ The complete class of enableautoconfiguration is key, and a set of corresponding @configuration classes are found;
Finally, @conponeentscan this note:
There is no difference between this annotation and the use in spring, which is to automatically scan and load the qualifying component or bean definition and eventually load the beans into the IOC container, so that in the Springboot project, the code is placed in the same sibling or subordinate directory as the startup class;
Of course the load bean is defined in spring's IOC container, we can manually register it, not necessarily through batch automatic scanning, so this annotation in the startup class can also be removed. (but no one usually does)