就 Spring Boot 啟動的
SpringBootApplication進(jìn)行解釋。
@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}
)}
)
-
@SpringBootApplication組合了
@SpringBootConfiguration、@ComponentScan、@EnableAutoConfiguration,是 Spring Boot 項目的入口。-
@SpringBootConfiguration等同于
@Configuration-
@ConfigurationIOC 容器的配置標(biāo)簽,等同于 Spring 中
<bean></bean>的配置,@Configuration標(biāo)注的就是一個 JavaConfig 的配置類,注冊 bean 需要在方法上使用@Bean標(biāo)注
-
-
@EnableAutoConfiguration自動配置,通過
@Import收集和注冊特定場景的 bean 定義。-
@EnableAutoConfigurationImportSelector搜索服務(wù)條件的
@Configuration配置,并加載到 Spring Boot 中,創(chuàng)建并使用 IOC 容器。 SpringFactoriesLoader.java
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) { List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader()); Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct."); return configurations; }SpringFactoriesLoader會加載META-INF/spring.factories配置文件,從而加載相應(yīng)類到 IOC 容器中 -
-
@ComponentScan自動掃描符合條件的組件,如
@Component、@Repository等,也可以通過指定basePackages來更細(xì)粒度的定制掃描范圍。
graph TD A[收集條件和回調(diào)接口 ApplicationIntializer/ApplicationListener] --step1 started--> B[準(zhǔn)備 Environment] B -- step2 environmentPrepared--> C[初始化 ApplicationContext 設(shè)置 Enviornment] C -- step3 contextPrepared contextLoaded --> D[refresh context 啟動完成] -