在springboot程序的入口有@SpringBootApplication注解。
在该注解的内部包含三个重要注解:
- @SpringBootConfiguration
- @EnableAutoConfiguration
- @ComponentScan
其中@EnableAutoConfiguration通过导入
@Import({AutoConfigurationImportSelector.class})
该类,使用selectImports()方法,使用SpringFactoriesLoader.loadFactoryNames()扫描有META-INF/spring.factories的jar包
在Spring.factories中有很多的配置,其中有的会生效,有的不会呢?
这些自动配置类在某些条件下才会生效。
@ConditionalOnBean:当容器里有指定的bean的条件下。
@ConditionalOnMissingBean:当容器里不存在指定bean的条件下。
@ConditionalOnClass:当类路径下有指定类的条件下。
@ConditionalOnMissingClass:当类路径下不存在指定类的条件下。
@ConditionalOnProperty:指定的属性是否有指定的值,比如@ConditionalOnProperties(prefix=”xxx.xxx”, value=”enable”, matchIfMissing=true),代表当xxx.xxx为enable时条件的布尔值为true,如果没有设置的情况下也为true。