如何使用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
以上就是如何使用SpringBootCondition更自由地定義條件化配置的詳細內容,更多關於SpringBootCondition 定義條件化配置的資料請關註WalkonNet其它相關文章!
推薦閱讀:
- None Found