SpringBoot的主启动类:
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
来看看@SpringBootApplication的构成:
@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 {
...
}
其中,最重要的三个注解为@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan.接下来逐个分析这三个注解
1.1.@SpringBootConfiguration进入@SpringBootConfiguration:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
@AliasFor(
annotation = Configuration.class
)
boolean proxyBeanMethods() default true;
}
由@Comfiguration可见,代表当前是一个配置类
1.2.@ComponentScan表示指定扫描哪些Spring注解
1.3.@EnableAutoConfiguration进入@EnableAutoConfiguration:
@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 {};
}
此处需要重点关注两个注解@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
先来看看@AutoConfigurationPackage
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(AutoConfigurationPackages.Registrar.class)//给容器中导入一个组件
public @interface AutoConfigurationPackage {
String[] basePackages() default {};
Class>[] basePackageClasses() default {};
}
此注解中核心注解为@Import(AutoConfigurationPackages.Registrar.class)
- 利用Register给容器中导入一系列组件
- 将指定的包下所有的组件导入进来
- 指定的包默认为MainApplication 所在包下,此处也就解释了在默认情况下,为何只有主启动类MainApplication所在包下的类能否被扫描注册进入spring容器
进入AutoConfigurationImportSelector中
- 利用getAutoConfigurationEntry(annotationMetadata);给容器中批量导入一些组件
- 调用List configurations = getCandidateConfigurations(annotationMetadata, attributes)获取到所有需要导入到容器中的配置类
- 利用工厂加载 Map
loadSpringFactories(@Nullable ClassLoader classLoader);得到所有的组件 - 从META-INF/spring.factories位置来加载一个文件
默认扫描我们当前系统里面所有META-INF/spring.factories位置的文件,文件中写死了SpringBoot启动时需要往容器中加载的所有配置类,共127个。
文件里面写死了spring-boot一启动就要给容器中加载的所有配置类 spring-boot-autoconfigure-2.3.4.RELEASE.jar/META-INF/spring.factories # Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration= org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration, org.springframework.boot.autoconfigure.aop.AopAutoConfiguration, org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration, org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration, org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,
虽然127个配置类(如aop。redis等)所有自动配置在启动的时候需要全部加载,但是根据配置类上条件规则(@Conditional),按照实际要求按需装配。
如以下示例:
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(prefix = "spring.aop",name ="auto",havingValue = "true",matchIfMissing = true
)
public class AopAutoConfiguration {
public AopAutoConfiguration() {
}
...
}
2.自动配置流程
以DispatcherServletAutoConfiguration的内部类DispatcherServletConfiguration为例子:
@Bean
@ConditionalOnBean(MultipartResolver.class) //容器中有这个类型组件
@ConditionalOnMissingBean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME) //容器中没有这个名字 multipartResolver 的组件
public MultipartResolver multipartResolver(MultipartResolver resolver) {
//给@Bean标注的方法传入了对象参数,这个参数的值就会从容器中找。
//SpringMVC multipartResolver。防止有些用户配置的文件上传解析器不符合规范
// Detect if the user has created a MultipartResolver but named it incorrectly
return resolver;
}
给容器中加入了文件上传解析器;
SpringBoot默认会在在底层配好所有的组件,但是如果用户自己配置了,会以用户为优先:
//我们在application.propertis中可以修改启动端口
server.port=8090
//ServerProperties中默认8080
@ConfigurationProperties(prefix = "server",ingoreUnknownField = true)
public class ServerProperties{
private Integer port;
...
}
流程如下:
- SpringBoot先加载所有的自动配置xxxAutoConfiguration
- 每个配置类按照条件进行生效,默认会绑定配置文件中指定的值(xxxProperties)
- 生效的配置类就会给容器中装配很多组件
- 有了这些组件,就有了相应的功能
- 定制化配置:
1.用户自己使用@Bean替换底层组件
2.用户修改application.properties中的值进而修改默认配置
xxxxxAutoConfiguration —> 组件 —> xxxxProperties里面拿值 ----> application.properties



