温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么使用SpringBoot中的Condition更自由地定义条件化配置

发布时间:2021-04-01 11:09:59 来源:亿速云 阅读:220 作者:小新 栏目:开发技术

这篇文章给大家分享的是有关怎么使用SpringBoot中的Condition更自由地定义条件化配置的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

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");
  }
}

感谢各位的阅读!关于“怎么使用SpringBoot中的Condition更自由地定义条件化配置”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI