亲宝软件园·资讯

展开

SpringBootCondition 定义条件化配置 怎样使用SpringBootCondition更自由地定义条件化配置

南瓜慢说 人气:0
想了解怎样使用SpringBootCondition更自由地定义条件化配置的相关内容吗,南瓜慢说在本文为您仔细讲解SpringBootCondition 定义条件化配置的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:SpringBoot,Condition,SpringBootCondition,定义条件化配置,下面大家一起来学习吧。

Conditional如何使用

@Conditional 是 SpringFramework 的功能, SpringBoot 在它的基础上定义了 @ConditionalOnClass , @ConditionalOnProperty 的一系列的注解来实现更丰富的内容。

定义一个自定义标签

import com.example.conditional.MyConditional;
import org.springframework.context.annotation.Conditional;
 
import java.lang.annotation.*;
 
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(MyConditional.class)
public @interface MyConditionalIAnnotation {
  String key();
  String value();
}

自定义Conditional

import com.example.conditional.interfaceI.MyConditionalIAnnotation;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.core.type.AnnotatedTypeMetadata;
 
import java.util.Map;
 
 
public class MyConditional extends SpringBootCondition {
 
  @Override
  public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
    Map<String, Object> annotationAttributes = metadata.getAnnotationAttributes(MyConditionalIAnnotation.class.getName());
    Object key = annotationAttributes.get("key");//
    Object value = annotationAttributes.get("value");
    if(key == null || value == null){
      return new ConditionOutcome(false, "error");
    }
 
    //获取environment中的值
    String key1 = context.getEnvironment().getProperty(key.toString());
    if (value.equals(key1)) {
      //如果environment中的值与指定的value一致,则返回true
      return new ConditionOutcome(true, "ok");
    }
    return new ConditionOutcome(false, "error");
 
  }
}

config配置

import com.example.conditional.interfaceI.MyConditionalIAnnotation;
import com.example.conditional.service.MyConditionalService;
import org.apache.log4j.Logger;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class MyConditionalConfig {
  public static Logger logger=Logger.getLogger(MyConditionalService.class);
 
  /**
   * 判断MyConditional 是否符合条件,是则运行MyConditionalService
   * @return
   */
  @MyConditionalIAnnotation(key = "com.example.conditional", value = "lbl")
  @ConditionalOnClass(MyConditionalService.class)
  @Bean
  public MyConditionalService initMyConditionService() {
    logger.info("MyConditionalService已加载。");
    return new MyConditionalService();
  }
}

配置文件:application.propeties

spring.application.name=gateway
server.port=8084
#conditional 动态配置,判断该值是否等于lbl,是则创建MyConditionalService实例
com.example.conditional=lbl
#支持自定义aop
spring.aop.auto=true

SpringBootCondition 定义条件化配置

1 条件化配置

Spring提供了多种实现化条件化配置的选择,如ConditionalOnProperty和ConditionalOnClass等。

用法如下:

@ConditionalOnProperty(prefix = "pkslow", name = "service", havingValue = "larry")

还有:

@ConditionalOnBean(仅仅在当前上下文中存在某个对象时,才会实例化一个Bean)
@ConditionalOnClass(某个class位于类路径上,才会实例化一个Bean)
@ConditionalOnExpression(当表达式为true的时候,才会实例化一个Bean)
@ConditionalOnMissingBean(仅仅在当前上下文中不存在某个对象时,才会实例化一个Bean)
@ConditionalOnMissingClass(某个class类路径上不存在的时候,才会实例化一个Bean)
@ConditionalOnNotWebApplication(不是web应用)

但有时候我们需要更灵活的自定义条件配置,这时可以通过继承SpringBootCondition类来实现。

2 继承SpringBootCondition

自己根据需求实现自己的判断逻辑,我的实现如下:

public class PkslowCondition extends SpringBootCondition {
 @Override
 public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
  BindResult<List<String>> maxBindResult = Binder.get(context.getEnvironment()).bind("pkslow.condition.max", Bindable.listOf(String.class));
  BindResult<List<String>> minBindResult = Binder.get(context.getEnvironment()).bind("pkslow.condition.min", Bindable.listOf(String.class));

  if ( (maxBindResult.isBound() && !maxBindResult.get().isEmpty()) && (minBindResult.isBound() && !minBindResult.get().isEmpty()) ) {
   List<String> maxs = maxBindResult.get();
   List<String> mins = minBindResult.get();
   int max = Integer.parseInt(maxs.get(0));
   int min = Integer.parseInt(mins.get(0));

   if (max < 1000 && min > 0) {
    return ConditionOutcome.match();
   }

  }

  return ConditionOutcome.noMatch("pkslow.condition.max/pkslow.condition.min not matches");
 }
}

表示需要有配置属性pkslow.condition.max/pkslow.condition.min才会生效,并且要求max<1000且min>0。

3 使用

完成自定义的条件类后,就可以使用它来限定一个配置类是否要生效了,使用如下:

@Conditional(PkslowCondition.class)
@Configuration
public class PkslowConfig {
  @PostConstruct
  public void postConstruct() {
    System.out.println("PkslowConfig called");
  }
}

4 总结

代码请查看:https://github.com/LarryDpk/pkslow-samples

加载全部内容

相关教程
猜你喜欢
用户评论