springboot中怎么利用bean实现条件装配

2023-06-15,

本篇文章为大家展示了springboot中怎么利用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")@Servicepublic 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")@Servicepublic 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。

上述内容就是springboot中怎么利用bean实现条件装配,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注本站行业资讯频道。

《springboot中怎么利用bean实现条件装配.doc》

下载本文的Word格式文档,以方便收藏与打印。