First, the main method must have started spring boot notes @SpringBootApplication
@SpringBootApplication
public class SpringBootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}
1.1, @ SpringBootApplication comment
See @SpringBootApplication annotated source code:
@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 {
Thus @SpringBootApplication annotations are combinations annotations, annotation from a combination of three yuan:
@SpringBootConfiguration (actually @Configuration): showing a configuration of the current startup Classes, can be customized by @Bean bean annotations in this class, other dependencies.
@EnableAutoConfiguration : With @Import This annotation will automatically configure all the bean definitions in line with the conditions of load into Ioc container.
@ComponentScan : Spring annotation automated scanning, scan package defined range, Ioc loaded into the container.
1.1.1, @ ComponentScan comment
The annotation is equivalent xml configuration file <context: component-scan base- package = "com.test" />, the main path is used to define the scan, identifying the need to find a path from the scanning assembly in Ioc container class automatically fitted into the spring (spring that is scanned with a special annotation Java classes, these classes and add objects to the configuration application context bean).
Special notes are:
(1), @ Component General comments;
(2), @ Controller identifies the class defined as the Controller of Spring MVC;
(3), @ Repository database identifies the class defined;
(4), @ Service identifies the class is defined as a service;
(5) was used as a custom class @Component element annotation;
(6), custom scan configuration classes: class marked with @Configuration use @Bean and has marked annotations in the class.
1.1.2, @ EnableAutoConfiguration comment
First look at @EnableAutoConfiguration annotated source code:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
Class<?>[] exclude() default {};
String[] excludeName() default {};
}
Wherein the core is @Import ({AutoConfigurationImportSelector.class}), continue to look AutoConfigurationImportSelector.class source:
(. 1), loadMetadata loading configuration: configuration properties 438
In fact, with the class loader to load: META-INF / spring-autoconfigure-metadata.properties (spring-boot-autoconfigure-1.5.9.RELEASE-sources.jar) defined in the configuration file, return PropertiesAutoConfigurationMetadata (implements the interface AutoConfigurationMetadata , encapsulates attributes get set method)
(2), getCandidateConfigurations get a list of names supported by default automatic configuration class: 97 auto-configuration classes
The method of auto-configuration soul, SpringFactoriesLoader.loadFactoryNames automatically obtain configuration class key = EnableAutoConfiguration.class configuration from the META-INF / spring.factories (spring-boot-autoconfigure-1.5.9.RELEASE-sources.jar) file.
The actual acquisition of the automatic matching file spring.factories = #Auto Configure the later category.
(3)、filter过滤器 根据OnClassCondition注解把不满足条件的过滤掉