@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
//启动spring应用
SpringApplication.run(HelloWorldMainApplication.class, args);
}
}
注解:@SpringBootApplication是一个组合注解
Ctrl+鼠标左键进入如下:
java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE})
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@java.lang.annotation.Documented
@java.lang.annotation.Inherited
@org.springframework.boot.SpringBootConfiguration
@org.springframework.boot.autoconfigure.EnableAutoConfiguration
@org.springframework.context.annotation.ComponentScan(excludeFilters = {@org.springframework.context.annotation.ComponentScan.Filter(type = org.springframework.context.annotation.FilterType.CUSTOM, classes = {org.springframework.boot.context.TypeExcludeFilter.class}), @org.springframework.context.annotation.ComponentScan.Filter(type = org.springframework.context.annotation.FilterType.CUSTOM, classes = {org.springframework.boot.autoconfigure.AutoConfigurationExcludeFilter.class})})
public @interface SpringBootApplication {
@org.springframework.core.annotation.AliasFor(annotation = org.springframework.boot.autoconfigure.EnableAutoConfiguration.class, attribute = "exclude")
java.lang.Class<?>[] exclude() default {};
@org.springframework.core.annotation.AliasFor(annotation = org.springframework.boot.autoconfigure.EnableAutoConfiguration.class, attribute = "excludeName")
java.lang.String[] excludeName() default {};
@org.springframework.core.annotation.AliasFor(annotation = org.springframework.context.annotation.ComponentScan.class, attribute = "basePackages")
java.lang.String[] scanBasePackages() default {};
@org.springframework.core.annotation.AliasFor(annotation = org.springframework.context.annotation.ComponentScan.class, attribute = "basePackageClasses")
java.lang.Class<?>[] scanBasePackageClasses() default {};
}
一:@SpringBootConfiguration
作用:标注在某个类上,表示这是SpringBoot的一个配置类。
Ctrl+鼠标左键进入如下:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}
@Configuration,配置类上的标注。
Ctrl+鼠标左键进入如下:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
String value() default "";
}
@Compent:标注一个类为Spring容器的Bean。(把普通POJO对象实例化到Spring容器中,相当于<bean id = " " class = " "/>)
二:@EnableAutoConfiguration
Ctrl+鼠标左键进入如下:
@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 {};
}
===============================================================================================
@AutoConfiguration:自动配置包
Ctrl+鼠标左键进入如下:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({Registrar.class})
public @interface AutoConfigurationPackage {
}
@Import({Registrar.class}):导入一个组件。
Registrar
Ctrl+鼠标左键进入如下:
static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports {
Registrar() {
}
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
AutoConfigurationPackages.register(registry,
(new AutoConfigurationPackages.PackageImport(metadata)).getPackageName());//拿到一个包名
}
public Set<Object> determineImports(AnnotationMetadata metadata) {
return Collections.singleton(new AutoConfigurationPackages.PackageImport(metadata));
}
}
metadata:注解标注的源信息。
断点得到信息:
标的是SpringbootApplication,标在了HelloWorldMainApplication上。
getPackageName:
综上所述:@AutoConfiguration的功能就是将主配置类下的所有组件扫描进Spring容器
这也解释了为什么Spring的启动类要放在最外层的包下了。
================================================================================================