码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot源码分析----(一)SpringBoot自动配置

时间:2020-03-23 22:11:40      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:自动   cat   strong   try   nbsp   原理   扫描   min   项目   

前言

springboot项目将模块化设计发挥到及至,需要什么模块,只需导入这个模块对应的stater即可,当然,用户根据业务需要自定义相关的stater,关于自定义stater在后续章节将一一解说,学习springboot,首要了解springboot的自动配置原理,我们从springboot项目的主启动类说起逐步解读springboot自动配置的奥秘。

springboot自动配置解读

@SpringBootApplication
public class SpringBootQuickApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootQuickApplication.class, args);
    }

}

 如上所示,@SpringBootApplication 注解标注在springboot应用的主启动类上,如下所示,@SpringBootApplication  其为复合注解,主要分析@SpringBootConfiguration  @EnableAutoConfiguration 这两个注解代表的含义

1.  @SpringBootConfiguration 

 

@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}
)}
)
public @interface SpringBootApplication {

 

 @SpringBootConfiguration  标识在某个类上表示这个类为springboot的配置类,@Configuration 为 spring 定义的配置类注解,@SpringBootConfiguration为springboot定义的配置类注解,深入@Configuration 可以看到其为 spring 容器中的一个组件 

提示:配置类是为了替换配置文件而生的,配置类也为容器中的一个组件  @Component

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {


@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {

 2. @EnableAutoConfiguration

springboot中为我们自动配置好了许多东西,@EnableAutoConfiguration 告诉 springboot 开启自动配置,这样自动配置才能生效
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {

 注解 @AutoConfigurationPackage    @Import({AutoConfigurationImportSelector.class})  给容器中导入组件

① 、@AutoConfigurationPackage 自动配置包

@Import({Registrar.class})
public @interface AutoConfigurationPackage {

 @Import 为spring的底层注解,给容器中导入一个组件

由注册器 Register 给springboot中注入组件,
static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports {
        Registrar() {
        }
    //注册 bean 定义     //将主配置类(@SpringBootApplication标注的类)所在的包及下面子包里面的所有组件扫描到 spring 容器当中 public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) { AutoConfigurationPackages.register(registry, (new AutoConfigurationPackages.PackageImport(metadata)).getPackageName()); }

总结: 可以看出,通过 @AutoConfigurationPackage 注解将自定义的组件扫描到 spring 容器当中

 ②、@Import({AutoConfigurationImportSelector.class}) 

  给容器中导组件?

  导入那些组件?

  AutoConfigurationImportSelector 导入哪些组件的选择器

  将所需要的组件以全类名的方式返回,这些组件将加入到spring容器当中

给容器当中导入非常多的配置类(XXXAutoConfiguration)   ,给容器中导入这个场景所需要的所有组件并配置好这些组件

 

 

 

 

技术图片

 

 

有了这些自动配置,免去了我们手动编写配置注入功能组件的工作
SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class, this.getBeanClassLoader());
SpringBoot在启动的时候从类路径下的 MATE-INF/spring.factories中获取 EnableAutoConfiguration 指定的值,将这些值作为自动配置类导入到容器当中,
自动配置类就生效,就可以帮助我们自动配置工作,以前我们自己需要配置的东西springboot帮我们做了

Javaee 整体解决方案和自动配置都在这个包里面 spring-boot-autoconfigure-2.2.5.RELEASE.jar

SpringBoot源码分析----(一)SpringBoot自动配置

标签:自动   cat   strong   try   nbsp   原理   扫描   min   项目   

原文地址:https://www.cnblogs.com/tombky/p/12555416.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!