SpringBoot自动装配
- spring支持两种bean的配置方式:基于xml文件和JavaConfig
主启动类上的注解@SpringBootApplication
 @SpringBootApplication里有三个重要注解

@SpringBootConfiguration
- 作用: 声明定义Bean,嵌套了@Component组件
-
- @SpringBootConfiguration源码是@Configuration:表示该类为主配置类,可用来装配bean
-
- @Configuration的源码是@Component:说明Spring的配置类也是Spring的一个组件。
 - 它是JavaConfig形式的基于Spring IOC容器的配置类使用的一种注解。SpringBoot本质上就是一个Spring应用,通过这个注解来加载IOC容器的配置。所以在启动类里面标注了@Configuration,意味着它也是一个IOC容器的配置类
@ComponentScan
- 作用:扫描主配置类包的所有包下的类,相当于xml配置文件中的context:component-scan。eg:pojo中的User类
@EnableAutoConfiguration(重点!!!)
@EnableAutoConfiguration里有两个重要注解

- @AutoConfigurationPackage:自动配置包
-
- 作用:给Spring容器中导入一个Registrar注册器组件

@AutoConfigurationPackage和@ComponentScan一样,也是将主配置类所在的包及其子包里面的组件扫描到IOC容器中,但是区别是 @AutoConfigurationPackage扫描@Enitity、@MapperScan等第三方依赖的注解 @ComponentScan只扫描@Controller/@Service/@Component/@Repository这些常见注解。所以这两个注解扫描的对象是不一样的。
- @Import(AutoConfigurationImportSelector.class)——核心注解
-
- 作用:通过import导入第三方提供的bean的配置类:AutoConfigurationImportSelector:给容器中导入组件
-
- 该类中有selectImports()方法,调用了getAutoConfigurationEntry()
 其下又调用了getCandidateConfigurations()  getCandidateConfigurations()中SpringFactoriesLoader.loadFactoryNames()方法,传入参数EnableAutoConfiguration.class -
- 作用:扫描所有jar包类路径下的META-INF/spring.factories文件,将扫描到的这些文件包装成properties对象,从properties中获取到EnableAutoConfiguration.class类名对应的值,将这些值添加到容器中,用这些类做自动配置功能
 在spring-boot-autoconfigure-.jar包中找到spring.factories文件  其中spring.factories文件是一组组的key=value的形式 -
- key=接口 value=接口实现类(多个,逗号隔开)
 以HttpEncodingAutoConfiguration为例解释: 
@Configuration(proxyBeanMethods = false)————表示该类为配置类 @EnableConfigurationProperties(ServerProperties.class)————将配置文件中设置的值与properties中的属性绑定,将组件添加到IOC容器 ————@Conditionalxxx:若满足不同的条件,则配置类中的配置生效 @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) @ConditionalOnClass(CharacterEncodingFilter.class) @ConditionalOnProperty(prefix = “server.servlet.encoding”, value = “enabled”, matchIfMissing = true) 
其中ServerProperties.class中的属性值,可与xx.yaml配置文件绑定并修改 精髓:根据当前配置类的条件判断配置类是否生效,若生效,则添加各种组件,会从properties类中获取属性,可以在配置文件xxx.yaml中设置属性的值
- Properties类:封装配置文件的相关属性。
- AutoConfiguration类:自动配置类,添加到IOC容器中。
SpringFactoriesLoader
- 作用:从classpath/META-INF/spring.factories文件中,根据key来加载对应的类到spring IOC容器中。
自动装配过程
- 通过各种注解实现了类与类之间的依赖关系,容器在启动的时候SpringApplication.run(),调用EnableAutoConfigurationImportSelector.class的selectImports方法
- selectImports方法最终会调用SpringFactoriesLoader.loadFactoryNames方法来获取一个全面的常用BeanConfiguration列表
- loadFactoryNames方法会读取spring.factories,获取所有的Spring相关的Bean的全限定名ClassName
- selectImports方法继续调用filter(configurations, autoConfigurationMetadata);这个时候会根据这些BeanConfiguration里面的条件,来一一筛选,最关键的是@ConditionalOnClass,这个条件注解会去classpath下查找,jar包里面是否有这个条件依赖类,所以必须有了相应的jar包,才有这些依赖类,才会生成IOC环境需要的一些默认配置Bean
- 最后把符合条件的BeanConfiguration注入默认的EnableConfigurationPropertie类里面的属性值,并且注入到IOC环境当中
总结
SpringBoot启动的时候通过@EnableAutoConfiguration注解找到META-INF/spring.factories文件中的所有自动配置类,并对其加载,这些自动配置类都是以AutoConfiguration结尾来命名的。它实际上就是一个JavaConfig形式的IOC容器配置类,通过以Properties结尾命名的类中取得在全局配置文件中配置的属性,如server.port。
|