【Spring杂烩】初探Spring的条件装配

2024-01-14 12:38

本文主要是介绍【Spring杂烩】初探Spring的条件装配,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

初尝Spring的条件装配


为了了解SpringBoot自动化装配的机制,所以必须先了解Spring的一些基础,Spring的条件装配就是其中之一

  • 前提概要
    • 条件装配知识脑图
    • 条件装配的定义
    • 条件装配的实现
    • 条件装配的作用
    • 条件装配的应用
  • 条件装配的源码体现
    • 注解模式
    • 编程模式
  • 自定义条件装配
    • 注解模式
    • 编程模式
  • 参考资料

前提概要


条件装配知识脑图
条件装配的定义
  • Bean装配的前置条件判断
  • 简而言之就是根据条件来判断这个Bean是否要被Spring容器装配
条件装配的实现

条件装配有两种实现方式:

  • 注解模式:@Profile
  • 编程模式:@Conditional
条件装配的作用

可以根据条件来选择是否需要在Spring容器中装配这个Bean,比如说:

  • 当Servlet依赖没有的时候,不装配WebMvc的各种Bean
  • 当某个Bean已经不存在的时候,就不重复装配
  • 根据不同的系统环境,装配不同的Bean
条件装配的应用
  • Spring Framework
    @Profile
  • Spring Boot
    @ConditionalOnMissingBean
    @ConditionalOnClass

条件装配的源码体现


注解模式

…这个没什么好说的,@Profile貌似使用的比较少,但是我们通常也知道一个SpringBoot多环境配置的特性,可以通过注解模式实现。但是在Spring 4.0开始,@Profile模式被修改过,实际的底层实现依然是编程模式

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(ProfileCondition.class)
public @interface Profile {/*** The set of profiles for which the annotated component should be registered.*/String[] value();}

从源码中,我们可以看到,它使用了编程模式的@Conditional注解,并指定了一个Condition接口的实现类ProfileCondition


编程模式

我们就从@ConditionalOnMissingBean(是否存在某个Bean)说起。

第一步,在IDE中寻找了一下,使用了@ConditionalOnMissingBean的地方,我们就从spring boot的webmvc的配置类开始说起 WebMvcAutoConfiguration

@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {}

我们看到 WebMvcAutoConfiguration配置类的定义,可以看到,类上是有@ConditionalOnMissingBean注解的声明,并且指定了一个类WebMvcConfigurationSupport,意思就是说当Spring容器中没有WebMvcConfigurationSupport这个Bean的时候,就会装配WebMvcAutoConfiguration 配置类

第二步,我们看check一下@ConditionalOnMissingBean注解的源码

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnBeanCondition.class)
public @interface ConditionalOnMissingBean {Class<?>[] value() default {};String[] type() default {};Class<?>[] ignored() default {};String[] ignoredType() default {};Class<? extends Annotation>[] annotation() default {};String[] name() default {};SearchStrategy search() default SearchStrategy.ALL;Class<?>[] parameterizedContainer() default {};
}

嗯,成员属性还挺多的,但我们也注意到注解的声明中,存在@Conditional注解,其指定了一个Condition接口的实现类OnBeanCondition.

第三步,我们查看一下@Conditional注解指向的实现类OnBeanCondition. Oh~,它还蛮复杂的,所以我们只能简化一下,只看与Condition接口相关的方法,最后发现OnBeanCondition类,最终是通过继承SpringBootCondition去实现Condition方法的,为了方便理解,这里放了结构图:
在这里插入图片描述

第四步,寻找matchs()方法,发现是由SpringBootContidion实现的(SpringBootContidion就是SpringBoot对Condition的再封装

	@Overridepublic final boolean matches(ConditionContext context,AnnotatedTypeMetadata metadata) {String classOrMethodName = getClassOrMethodName(metadata);try {//如何获取ConditionOutcome outcome = getMatchOutcome(context, metadata);logOutcome(classOrMethodName, outcome);recordEvaluation(context, classOrMethodName, outcome);//重点return outcome.isMatch();}catch (NoClassDefFoundError ex) {throw new IllegalStateException("Could not evaluate condition on " + classOrMethodName + " due to "+ ex.getMessage() + " not "+ "found. Make sure your own configuration does not rely on "+ "that class. This can also happen if you are "+ "@ComponentScanning a springframework package (e.g. if you "+ "put a @ComponentScan in the default package by mistake)",ex);}catch (RuntimeException ex) {throw new IllegalStateException("Error processing condition on " + getName(metadata), ex);}}

以上是SpringBootCondition实现的matches方法,OnBeanCondition继承了下来。我们可以看到这个方法的返回值是通过outcome.isMatch(),而这个outcome又是通过getMatchOutcome()方法获取的,这个方法又是谁的呢?是SpringBootCondition抽象类的抽象方法,最终是OnBeanCondition实现的

第五步,查看OnBeanCondition的getMatchOutcome()方法,发现…emmm,更加复杂了。那就再简化简化

  • 总之这个方法会判断传入的metadate是不是ConditionalOnMissingBean注解类型,类似switch的功能,判断好久跳转到对应的逻辑块执行.
  • 然后调用一个getMatchingBeans()方法,获取Bean然后调用一个getMatchingBeans()方法,获取Bean
  • 根据返回的matchResult判断是否有对应的Bean,返回一个matchMessage结果
  • 最后这个matchMessage就会被SpringBootCondition获取到,用于给matches方法判断是true,还是false

第六步,当matchs方法返回了true 或者false之后,Spring容器就会根据这个flag判断是否装配WebMvcAutoConfiguration配置类

小结:

  • 的确,内部的逻辑比较复杂,所以暂时就忽略了很多部分,有一些地方我自己也没有弄懂,所以就不能太详细的说明,所以如有错误,请指出
  • 虽然OnBeanCondition非常的复杂,那是因为SpringBoot本身就是一个复杂的东西,所以我们自己实现的时候,未必需要实现的跟它一样复杂。

自定义条件装配


注解模式
目录结构,总共四个类

在这里插入图片描述
这里要实现一个计算服务,根据不同的JDK版本,执行不同的代码

  • CalculateService
    计算服务接口
  • Java7CalculateService
    Java 7 计算服务实现类,通过for循环实现
  • Java8CalculateService
    Java 8 计算服务实现类,通过lambda和Stream实现
  • Demo2Application
    SpringBoot启动引导类
CalculateService
public interface CalculateService {Integer sum(Integer...values);
}
Java7CalculateService
*** Java7计算服务* @author liwenjie*/
@Profile("Java7")
@Service
@Slf4j
public class Java7CalculateService implements CalculateService {@Overridepublic Integer sum(Integer... values) {int sum = 0;for (Integer i : values){sum = sum + i;}log.error("Java7 Calculating");return sum;}
}
**Java8CalculateService
/*** Java8计算服务* @author liwenjie*/
@Profile("Java8")
@Service
@Slf4j
public class Java8CalculateService implements CalculateService {@Overridepublic Integer sum(Integer... values) {log.error("Java8 Calculating");return Stream.of(values).reduce(0,Integer::sum);}
}
Demo2Application
@Slf4j
@SpringBootApplication
public class Demo2Application {//获取计算服务实现类@AutowiredCalculateService calculateService;//通过SpringApplication的方式修改profiles//当然你也可以在application.properties中配置spring.profiles.active=Java7public static void main(String[] args) {new SpringApplicationBuilder(Demo2Application.class).profiles("Java7").run(args);}@PostConstructpublic void init(){calculateService.sum(1,2,3,4,5);}}
  • 最后就会实际运行的就是Java7的实现类
  • 要注意的是,我这里使用了lombok插件(强烈推荐),所以没有的把log.error修改为System.out.println就好了


编程模式
目录结构,三个类
  • ConditionOnSystemProperty注解
    我们这里的作用就是判断系统的Property值是否跟注解的属性值相同,如果相同则装配该Bean
  • OnSystemPropertyCondition实现类
    具体的Condition实现类
  • Demo2Application类
    SpringBoot启动引导类
ConditionOnSystemProperty

模拟@ConditionalOnMissingBean注解

@Target({ElementType.TYPE,ElementType.METHOD})
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Conditional(OnSystemPropertyCondition.class)
public @interface ConditionOnSystemProperty {/***  系统名称,property的key* @return*/String name();/*** 系统属性值,property的value* @return*/String value();
}
OnSystemPropertyCondition
public class OnSystemPropertyCondition implements Condition {@Overridepublic boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {//获取ConditionOnSystemProperty的成员属性,最终map有两个元素,name和valueMap<String ,Object> attributes = metadata.getAnnotationAttributes(ConditionOnSystemProperty.class.getName());//获取属性名称String propertyName = String.valueOf(attributes.get("name"));//获取属性值String propertyValue = String.valueOf(attributes.get("value"));//从系统的Property中传入key,获取valueString systemPropertyValue = System.getProperty(propertyName);//最后比较propertyValue 和 systemPropertyValue 是否相等,并返回return systemPropertyValue.equals(propertyValue);}
}
Demo2Application

@SpringBootApplication
public class Demo2Application {@Bean@ConditionOnSystemProperty(name="user.name",value = "xxxx")public String Hello(){return "Hello World";}public static void main(String[] args) {new SpringApplicationBuilder(Demo2Application.class).run(args);}}
  • 结果就是,如果系统的property中没有key为user.name的元素,或者key为user.name的值不为xxxx,那么Spring容器就不会装配hello这个Bean
小结:
  • 自定义条件判断注解,注解内部需要有@Conditional,并指向一个Condition接口的实现类
  • Condition接口的实现类需要实现matchs方法
  • Spring会根据matchs方法返回flag来决定是否装配被自定义条件判断注解修饰的Bean

参考资料


  • SpringBoot2 | 条件注解 @ConditionalOnBean 原理源码分析(七)- 作者:张书康

这篇关于【Spring杂烩】初探Spring的条件装配的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/605191

相关文章

SpringBoot实现RSA+AES自动接口解密的实战指南

《SpringBoot实现RSA+AES自动接口解密的实战指南》在当今数据泄露频发的网络环境中,接口安全已成为开发者不可忽视的核心议题,RSA+AES混合加密方案因其安全性高、性能优越而被广泛采用,本... 目录一、项目依赖与环境准备1.1 Maven依赖配置1.2 密钥生成与配置二、加密工具类实现2.1

在Java中实现线程之间的数据共享的几种方式总结

《在Java中实现线程之间的数据共享的几种方式总结》在Java中实现线程间数据共享是并发编程的核心需求,但需要谨慎处理同步问题以避免竞态条件,本文通过代码示例给大家介绍了几种主要实现方式及其最佳实践,... 目录1. 共享变量与同步机制2. 轻量级通信机制3. 线程安全容器4. 线程局部变量(ThreadL

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick