@SpringBootApplication
He is a combination of notes:
@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 {
@AliasFor(annotation = EnableAutoConfiguration.class, attribute = "exclude")
Class<?>[] exclude() default {};
@AliasFor(annotation = EnableAutoConfiguration.class, attribute = "excludeName")
String[] excludeName() default {};
@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
String[] scanBasePackages() default {};
@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
Class<?>[] scanBasePackageClasses() default {};
}
@Inherited
The function of Inherited is to declare a custom annotation using this annotation. When using this custom annotation, if the annotation is above the class, the subclass will automatically inherit the annotation, otherwise, the subclass will not inherit the annotation. It's important to remember here that annotations declared with Inherited are valid only when used on classes, invalid for methods, attributes, etc.
Reference resources Detailed description of the use of meta-annotation Inherited in java annotations
@SpringBootConfiguration
Source code:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}
@ SpringBootConfiguration inherits from @Configuration, and both functions are identical. It annotates that the current class is a configuration class, and incorporates one or more instances of methods declared in the current class marked with @Bean annotations into the srping container, and the instance name is the method name.
@EnableAutoConfiguration
@SuppressWarnings("deprecation")
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
Class<?>[] exclude() default {};
String[] excludeName() default {};
}
@ Enable AutoConfiguration is the core annotation for spring boot to start automatic configuration. Enable AutoConfiguration import Selector is imported above, which inherits from AutoConfiguration import Selector. AutoConfiguration import Selector is the key class.
The EnableAutoConfiguration ImportSelector class uses the loadFactoryNamesof() method of the SpringFactories Loader class of the Spring Core package.
Spring Factories Loader queries JAR files contained in META-INF/spring.factories files.
When the spring.factories file is found, SpringFactories Loader queries the value of the attribute named by the configuration file, org.springframework.boot.autoconfigure.EnableAutoConfiguration.
Then a real bean load is performed in the refresh method during spring boot.
@ComponentScan
@ ComponentScan scans the classes labeled by @Component,@Controller,@Service,@Repository annotations under the current package and its subpackages and incorporates them into the spring container for management. This is why our startup class DemoApplication should be placed at the outermost level of the project.