SpringBoot自动配置学习
快速构建springboot项目网址:https://start.spring.io/
@SpringBootApplication注解
在springboot项目中的每一个启动类上都会有SpringBootApplication这样一个重要的组合注解。

- @Target(ElementType.TYPE),@Retention(RetentionPolicy.RUNTIME),@Documented三个注解都为元注解。分别表示可作用在哪些地方,注解保留多久,生成文档时有@三方面。
- @SpringBootConfiguration注解表示加载配置configuration
- @ComponentScan 扫描注解
@SpringBootApplication注解中的@EnableAutoConfiguration注解
springboot实现自动配置Bean,将Bean配置到ioc容器中,该注解功不可没。

-
从上可以看出@EnableAutoConfiguration又是一个组合注解,其中@AutoConfigurationPackage注解为自动配置包,其下也是使用@import注解导入一个配置。

-
最后回到@import注解导入自动配置Bean,其中重写selectImports方法,调用getAutoConfigurationEntry方法中的getCandidateConfigurations方法去实现满足条件的Bean的自动注入ioc容器


最后总结
- 最重要的就是@EnableAutoConfiguration注解中的底层getCandidateConfigurations方法。去加载META-INF/spring.factories配置信息去实现自动装配。

浙公网安备 33010602011771号