spring boot中的条件装配bean的实现

条件装配

从Spring Framework 3.1开始,允许在Bean装配时增加前置条件判断。

啥是条件装配

在bean装配前的条件判断。比如@Profile(是在spring3.1中引入),@Contditional(spring4.0中引入)
实现方式:注解方式,编程方式。

假设我们现在有一个多数据求和计算的小需求,定义两种方式Java7和Java8,然后使用条件装配的方式分别装配不同的bean。

首先我们定义一个接口

public interface CalculateService {

  /**
   * 从多个整数 sum 求和
   * @param values 多个整数
   * @return sum 累加值
   */
  Integer sum(Integer... values);
}

其次是两种不同的实现方式,Java7的方式

@Profile("Java7")
@Service
public class Java7CalculateService implements CalculateService {

  @Override
  public Integer sum(Integer... values) {
    System.out.println("Java 7 for 循环实现 ");
    int sum = 0;
    for (int i = 0; i < values.length; i++) {
      sum += values[i];
    }
    return sum;
  }

}

Java8的实现

@Profile("Java8")
@Service
public class Java8CalculateService implements CalculateService {

  @Override
  public Integer sum(Integer... values) {
    System.out.println("Java 8 Lambda 实现");
    int sum = Stream.of(values).reduce(0, Integer::sum);
    return sum;
  }

  public static void main(String[] args) {
    CalculateService calculateService = new Java8CalculateService();
    System.out.println(calculateService.sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
  }

}

我们在这里使用了@Profile("Java8")注解来表明对应的profile。然后我定义一个启动类在里面配置装配哪一个bean

@SpringBootApplication(scanBasePackages = "com.service")
public class CalculateServiceBootstrap {

  public static void main(String[] args) {
    ConfigurableApplicationContext context = new SpringApplicationBuilder(CalculateServiceBootstrap.class)
        .web(WebApplicationType.NONE)
        .profiles("Java8")
        .run(args);

    // CalculateService Bean 是否存在
    CalculateService calculateService = context.getBean(CalculateService.class);

    System.out.println("calculateService.sum(1...10) : " +
        calculateService.sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));

    // 关闭上下文
    context.close();
  }
}

使用基于@ConditionalOnSystemProperty注解的方式实现。

首先我们定义一个注解,这里定义了一个属性和一个值。

/**
 * Java 系统属性 条件判断
 *
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnSystemPropertyCondition.class)
public @interface ConditionalOnSystemProperty {

  /**
   * Java 系统属性名称
   * @return
   */
  String name();

  /**
   * Java 系统属性值
   * @return
   */
  String value();
}

定义一个条件判断的类,当这个类中的条件满足是才会装载bean,这个实现类实现org.springframework.context.annotation.Condition,AnnotatedTypeMetadata可以获取的到注解中的name和value信息,假设我们现在实现判断系统属性和注解中的配置的一样就加载bean,System.getProperty("user.name")获取当前系统下的用户名,我的mac创建的用户名叫yanghongxing,如果我们在注解中配置的value是yanghongxing则装载这个bean。

/**
 * 系统属性条件判断
 *
 */
public class OnSystemPropertyCondition implements Condition {

  @Override
  public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {

    Map<String, Object> attributes = metadata.getAnnotationAttributes(ConditionalOnSystemProperty.class.getName());

    String propertyName = String.valueOf(attributes.get("name"));

    String propertyValue = String.valueOf(attributes.get("value"));

    String javaPropertyValue = System.getProperty(propertyName);

    return propertyValue.equals(javaPropertyValue);
  }
}

最后在启动类中启动

public class ConditionalOnSystemPropertyBootstrap {

  @Bean
  @ConditionalOnSystemProperty(name = "user.name", value = "yanghongxing")
  public String helloWorld() {
    return "Hello,World Honson";
  }

  public static void main(String[] args) {
    ConfigurableApplicationContext context = new SpringApplicationBuilder(ConditionalOnSystemPropertyBootstrap.class)
        .web(WebApplicationType.NONE)
        .run(args);
    // 通过名称和类型获取 helloWorld Bean
    String helloWorld = context.getBean("helloWorld", String.class);

    System.out.println("helloWorld Bean : " + helloWorld);

    // 关闭上下文
    context.close();
  }
}

我们可以在OnSystemPropertyCondition实现复杂的装载类的条件,判断是否装载某个bean。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Spring Task定时任务的配置和使用详解

    记录下Spring自带的定时任务用法. spring中使用定时任务 基于xml配置文件使用定时任务 首先配置spring开启定时任务 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/

  • 读取spring配置文件的方法(spring读取资源文件)

    1.spring配置文件 复制代码 代码如下: <bean id="configproperties"          class="org.springframework.beans.factory.config.PropertiesFactoryBean">          <property name="location" value="classpath:jdbc.properties"/>

  • 在SpringBoot下读取自定义properties配置文件的方法

    SpringBoot工程默认读取application.properties配置文件.如果需要自定义properties文件,如何读取呢? 一.在resource中新建.properties文件 在resource目录下新建一个config文件夹,然后新建一个.properties文件放在该文件夹下.如图remote.properties所示 二.编写配置文件 remote.uploadFilesUrl=/resource/files/ remote.uploadPicUrl=/resource

  • 详解SpringBoot配置连接池

    内置的连接池 目前spring Boot中默认支持的连接池有dbcp,dbcp2, tomcat, hikari三种连接池. 数据库连接可以使用DataSource池进行自动配置. 由于Tomcat数据源连接池的性能和并发,在tomcat可用时,我们总是优先使用它. 如果HikariCP可用,我们将使用它. 如果Commons DBCP可用,我们将使用它,但在生产环境不推荐使用它. 最后,如果Commons DBCP2可用,我们将使用它. 以上的几种连接池,可以通过在配置application文

  • spring boot微服务自定义starter原理详解

    这篇文章主要介绍了spring boot微服务自定义starter原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 使用spring boot开发微服务后,工程的数量大大增加(一定要按照领域来切,不要一个中间件客户端包一个),让各个jar从开发和运行时自包含成了一个重要的内容之一.spring boot starter就可以用来解决该问题(没事启动时别依赖于applicationContext.getBean获取bean进行处理,依赖关系

  • Spring实战之属性占位符配置器用法示例

    本文实例讲述了Spring实战之属性占位符配置器用法.分享给大家供大家参考,具体如下: 一 配置文件 <?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns

  • Spring实战之容器后处理器操作示例

    本文实例讲述了Spring实战之容器后处理器.分享给大家供大家参考,具体如下: 一 配置文件 <?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p=&

  • Spring @Conditional注解讲解及示例详解

    前言: @Conditional是Spring4新提供的注解,它的作用是按照一定的条件进行判断,满足条件给容器注册bean. @Conditional的定义: //此注解可以标注在类和方法上 @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Conditional { Class<? extends Condition>

  • 解决Spring国际化文案占位符失效问题的方法

    写在前面:接下来很长一段时间的文章主要会记录一些项目中实际遇到的问题及对应的解决方案,在相应代码分析时会直指问题所在,不会将无关的流程代码贴出,感兴趣的读者可以自行跟踪.同时希望大家能够将心得体会在评论区分享出来,让大家共同进步! 环境或版本:Spring 3.2.3 现象:利用Spring自带的MessageSource来处理国际化文案,us状态下的文案有部分占位符未被替换,cn状态下的正常.文案如下: tms.pallet.order.box.qty=The total palletized

  • SpringBoot如何优雅的处理校验参数的方法

    前言 做web开发有一点很烦人就是要校验参数,基本上每个接口都要对参数进行校验,比如一些格式校验 非空校验都是必不可少的.如果参数比较少的话还是容易 处理的一但参数比较多了的话代码中就会出现大量的IF ELSE就比如下面这样: 这个例子只是校验了一下空参数.如果需要验证邮箱格式和手机号格式校验的话代码会更多,所以介绍一下validator通过注解的方式进行校验参数. 什么是Validator Bean Validation是Java定义的一套基于注解的数据校验规范,目前已经从JSR 303的1.

  • Spring Boot环境属性占位符解析及类型转换详解

    前提 前面写过一篇关于Environment属性加载的源码分析和扩展,里面提到属性的占位符解析和类型转换是相对复杂的,这篇文章就是要分析和解读这两个复杂的问题.关于这两个问题,选用一个比较复杂的参数处理方法PropertySourcesPropertyResolver#getProperty,解析占位符的时候依赖到 PropertySourcesPropertyResolver#getPropertyAsRawString: protected String getPropertyAsRawSt

随机推荐