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。

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

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。