美文网首页
springboot的简单理解

springboot的简单理解

作者: laowangv2 | 来源:发表于2020-09-29 17:08 被阅读0次

    springboot之前:

    1. 自己管理各种依赖
    2. 在application-context中开启各种配置

    springboot之后:

    1. 只需要引入xxx-spring-boot-starter,starter中会帮我们管理各种依赖
    2. starter中引入xxx-spring-boot-autoconfigure包,帮我们做各种配置,也可以把2并入1中,只有一个starter

    原理

    如何实现

    依赖由maven管理,1不谈
    2其实就是要把依赖的jar包里的config生效

    springboot怎么做的

    看看springboot的注解

    @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:组合了 @Configuration 注解,实现配置文件的功能。
    @EnableAutoConfiguration:打开自动配置的功能。
    @ComponentScan:Spring组件扫描。
    其中,@EnableAutoConfiguration

    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @AutoConfigurationPackage
    @Import({AutoConfigurationImportSelector.class})
    public @interface EnableAutoConfiguration {
        String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
    
        Class<?>[] exclude() default {};
    
        String[] excludeName() default {};
    }
    

    这里import了AutoConfigurationImportSelector,这个类通过SpringFactoriesLoader.loadFactoryNames加载依赖包中的META-INF/spring.factories
    import、configuration、componentscan这几个注解都是通过ConfigurationClassPostProcessor实现的,这个后处理器是一个beanfactory后处理器

    • @import
      把类纳入容器管理,可以import三类class:普通类、实现importSelector的、实现importbeanDefinitionRegistrar的
    • ImportSelector
      主要作用是和@import一起导入需要的配置类。spring boot 的各种@EnableXXX就是@import一个ImportSelector,然后返回一堆autoconfigure类名,让spring去加载
    spring.factories

    如上所述,springboot会加载依赖中的spring.factories文件,那么只要在其中配置自己的config类,就可以被spring发现了。
    贴一个典型的spring.factories文件

    # Auto Configure
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration
    

    于是MybatisPlusAutoConfiguration会被框架发现,这个类通过@Configuration注解可以作为config类被spring纳入,类中框架作者声明的一些@Bean可以作为默认配置生效,效果同自己在application-context中配置。

    配置文件

    另一个关注的点是spring boot的配置文件,yml或properties是如何被加载的,以及我们使用配置文件中变量的实现

    1. 加载
      ConfigFileApplicationListener监听ApplicationEnvironmentPreparedEvent事件,然后触发加载,最终加载是通过内部类Loader,构造Loader是会加载yml和properties的PropertySourceLoader
    2. 使用
      • @PropertySource,指定文件
      • @Value,key
      • @ConfigurationProperties,指定前缀
      • Environment,上面说的都可以注入一个 Environment对象来获取

    spring-boot-autoconfigure

    spring-boot-autoconfigure模块已经帮我们内置了一大片常用的AutoConfiguration,通常都会有一些condition注解去触发,如果我们引入了相关的starter就会触发其中的配置。这也是所谓“约定大于配置”的一种体现.

    总结

    springboot帮我们做的两件事,一个是通过starter引入我们需要的框架,starter负责引入自己的依赖;另一个是通过autoconfigure生效一些默认的配置,简化我们的使用,这一步是通过spring.factories中定义的配置类实现的。
    以上就是暂时对springboot比较浅的理解,待后续深入。

    参考

    深入springboot原理——一步步分析springboot启动机制(starter机制)
    手把手带你剖析 Springboot 启动原理!
    Spring Boot读取配置的几种方式

    相关文章

      网友评论

          本文标题:springboot的简单理解

          本文链接:https://www.haomeiwen.com/subject/cbeeuktx.html