SpringBoot自动装配原理

之前的文章、介绍过SpringMvc的自动装配原理:基于Tomcat容器启动的时候、Spring的contentLoadListener监听Tomcat的启动事件、然后去加载xml或者注解中的Bean信息到Spring容器中、那么SpringBoot和SpringMVC之间又有什么不同点了、我们知道SpringBoot是自带Tomcat的容器的、SpringBoot是如何自动装配的、

什么是SpringBoot自动配置?
​ SpringBoot的自动配置,指的是SpringBoot会自动将一些配置类的bean注册进ioc容器,我们可以需要的地方使用@autowired或者@resource等注解来使用它。
​ “自动”的表现形式就是我们只需要引我们想用功能的包,相关的配置我们完全不用管,springboot会自动注入这些配置bean,我们直接使用这些bean即可。

首先我们看到我们项目中启动类:

@SpringBootApplication
public class JavaLearnRecordApplication {

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

}

可以看到启动类上都会有一个注解:SpringBootApplication。我们就从这个注解开始分析。;

@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 {
    @AliasFor(
        annotation = EnableAutoConfiguration.class
    )
    Class<?>[] exclude() default {};

SpringBootApplication注解的部分代码片段如上:其中我们可以看到比较重要的两个注解:ComponentScan、EnableAutoConfiguration:

ComponentScan

这两个注解分别是干什么的:首先ComponentScan大家应该都比较清楚。通过ComponentScan告诉Spring、哪些Bean是需要在容器启动的时候去加载到容器中去的:主要扫描的是我们平时使用@Service @Component @Repository.等一系列的注解。

  • 如果你的其他包都在使用了@SpringBootApplication注解的main app所在的包及其下级包,则你什么都不用做,SpringBoot会自动帮你把其他包都扫描了
  • 如果你有一些bean所在的包,不在main app的包及其下级包,那么你需要手动加上@ComponentScan注解并指定那个bean所在的包

 EnableAutoConfiguration

接下来是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.class})导入的AutoConfigurationImportSelector的selectImports()方法通过SpringFactoriesLoader.loadFactoryNames()扫描在

spring-boot-autoconfigure-x.x.x.x.jar里就有一个这样的spring.factories文件。

spring-boot-autoconfigure-2.5.2.jar下的META-INF/spring.factories配置文件、

具体的内容如下图所示:

 由上图可以看出:以Key/Value的形式存储中一些***AutoConfiguration的class类名、包含我们长国用的Jdbc、Redis、cache、等一系列SpringBoot自带的组件的config类名

这个@EnableAutoConfiguration注解通过@SpringBootApplication被间接的标记在了Spring Boot的启动类上。在SpringApplication.run(...)的内部就会执行selectImports()方法,找到所有JavaConfig自动配置类的全限定名对应的class,然后将所有自动配置类加载到Spring容器中。

那么自动装配的条件是什么勒?

自动装配类不是SpringBoot一启动的时候就会把所有的config注册到容器中,其中有一些的条件、

如下图所示:

  • @ConditionalOnBean:当容器里有指定的bean的条件下。
  • @ConditionalOnMissingBean:当容器里不存在指定bean的条件下。
  • @ConditionalOnClass:当类路径下有指定类的条件下。
  • @ConditionalOnMissingClass:当类路径下不存在指定类的条件下。
  • @ConditionalOnProperty:指定的属性是否有指定的值,比如@ConditionalOnProperties(prefix=”xxx.xxx”, value=”enable”, matchIfMissing=true),代表当xxx.xxx为enable时条件的布尔值为true,如果没有设置的情况下也为true。
     

我们以ServletWebServerFactoryAutoConfiguration具体分析来看代码片段如下:

@Configuration(
    proxyBeanMethods = false
)
@AutoConfigureOrder(-2147483648)
@ConditionalOnClass({ServletRequest.class})
@ConditionalOnWebApplication(
    type = Type.SERVLET
)
@EnableConfigurationProperties({ServerProperties.class})
@Import({ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class, EmbeddedTomcat.class, EmbeddedJetty.class, EmbeddedUndertow.class})
public class ServletWebServerFactoryAutoConfiguration 

其中有一个注解:@EnableConfigurationProperties({ServerProperties.class})

@ConfigurationProperties(
    prefix = "server",
    ignoreUnknownFields = true
)
public class ServerProperties {
    private Integer port;
    private InetAddress address;
    @NestedConfigurationProperty

看到这里我们其实就可以大致理解了、通过ConfigurationProperties注解读取配置中心为server前缀的配置项的vlue来封装serverProperties对象。并通过EnableConfigurationProperties注解将封装好的serverProperties对象注册到Spring容器中。

参考文章:

https://blog.csdn.net/u014745069/article/details/83820511

Spring Boot学习笔记1:Spring, Spring Boot中的@Component 和@ComponentScan注解用法介绍_Lamb_IT的博客-CSDN博客

springboot自动配置是如何实现的?_insomsia的博客-CSDN博客_springboot自动配置 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值