有时候想学一个知识点,又想只花刷一个段子的时间,那就关注我:
springboot的自动配置:通过@springbootApplication复合注解中的@EnableAutoConfiguration注解
通过@EnableAutoConfigration中的@import导入的AutoConfigurationImportSelector类
再调用该类中的selectImports方法,在该方法中,通过getAutoConfigurationEntry方法调用getCandidateConfigurations方法,然后在getCandidateConfigurations中通过SpringFactoriesLoader.loadFactoryNames()扫描所有具有META-INF/spring.factories的jar包(spring-boot-autoconfigure-x.x.x.x.jar里就有一个这样的spring.factories文件)
在spring.factories文件中是用key-value形式进行一个配置的,key为EnableAutoConfiguration类的全路径,value为各个配置类的全路径,每个用逗号隔开
在这些个写类中,通过@ConditionalOnxxxx判断该配置类是否生效,在通过@EnableConfigurationProperties绑定xxxxproperties.class。
然后再xxxproperties.java类中通过@ConfigrurationProperties注解与application.yml进行绑定