springboot-自动装配原理

马士兵 springboot 源码分析

Spring发展历史

在这里插入图片描述

spring v3.0 注解

@ComponentScan

在这里插入图片描述

@Configuration

在这里插入图片描述

@Import

<import>

在这里插入图片描述

@ImportResource:过渡解决方案

@Import 引入配置类

在这里插入图片描述

@Import 将指定的类型注入到容器中去

在这里插入图片描述

@Import: ImportSelector接口

在这里插入图片描述

@Import: ImportBeanDefinitionRegistrar

在这里插入图片描述

@Enable*

Redis配置为例

  1. 添加@Configuration
    在这里插入图片描述

  2. 定义@EnableRedisAutoConfiguration注解
    在这里插入图片描述

  3. 在配置类上添加注解
    在这里插入图片描述


Spring 4.0 注解

@Conditional 条件注解

决定是否将bean注入到容器中去。

@Conditional 注解定义
@Target({ElementType.TYPE, ElementType.METHOD}) //注解可以添加在类 or method上
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Conditional {
    Class<? extends Condition>[] value();
}

Condition

@FunctionalInterface
public interface Condition {
	boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata);  
}
例1

在这里插入图片描述
MyCondition
在这里插入图片描述


Spring5.0 注解

@Indexed

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

作用
为了解决@ComponentScan扫描jar的多次IO 带来的性能问题,将所有的bean通过索引的方式,记录在索引文件中。
META-INF/spring.components.
在这里插入图片描述



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 {


}


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

}


@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
}

自动装配原理: spring-boot-XXX-starter

  • spring-boot-XXX-starter : 官方starter
  • YYY-spring-boot-starter: 非官方

spring.factories

AutoConfigurationImportSelector 中调用SpringFactoriesLoader.loadFactoryName()方法,加载自动配置类。
最终通过,读取META-INF/spring.factories文件,获取所有的配置信息。
在这里插入图片描述

不同的XXX-springboot-starter包中,会存在各自的spring.factories文件。
在这里插入图片描述

spring-autoconfigure-metadata

META-INF/spring-autoconfigure-metadata.properties来限定 autoconfig的加载条件。
在这里插入图片描述

手写一个starter

  1. 定义一个XXXConfiguration配置类。

    配合@ConfigurationProperties 和@EnableConfigurationProperties 注解使用。
    在这里插入图片描述
    在这里插入图片描述

  2. META-INF/spring.factories 中的org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 行后,添加步骤1 配置类全限定名称。

自定义配置项-自动提示: additional-spring-configuration-metadata.json

在自定义starter项目中,META-INF/additional-spring-configuration-metadata.json添加如下形式信息:
在这里插入图片描述



actuator

actuator

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值