溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發(fā)布時間:2021-04-01 11:09:59 來源:億速云 閱讀:228 作者:小新 欄目:開發(fā)技術

這篇文章給大家分享的是有關怎么使用SpringBoot中的Condition更自由地定義條件化配置的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

Conditional如何使用

@Conditional 是 SpringFramework 的功能, SpringBoot 在它的基礎上定義了 @ConditionalOnClass , @ConditionalOnProperty 的一系列的注解來實現(xiàn)更豐富的內容。

定義一個自定義標簽

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 動態(tài)配置,判斷該值是否等于lbl,是則創(chuàng)建MyConditionalService實例
com.example.conditional=lbl
#支持自定義aop
spring.aop.auto=true

SpringBootCondition 定義條件化配置

1 條件化配置

Spring提供了多種實現(xiàn)化條件化配置的選擇,如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類來實現(xiàn)。

2 繼承SpringBootCondition

自己根據(jù)需求實現(xiàn)自己的判斷邏輯,我的實現(xiàn)如下:

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問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI