pkslow.com 南瓜慢说

  • AllArticles
  • Container
  • Spring
  • Life
  • Cloud
  • Collections
  • About
  • GitHub

  • Search
Terraform101 English Terraform Middleware config Go Private Kubernetes pkslow Test HTTPS Redis Docker Mac Plan Stream MongoDB Spring DevOps JVM String Map Set List Performance Email Springboot JavaCollections ArrayList Java

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

Created on: 2021-03-31 | Category: Springboot | 0 | View: 874

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


参考:springboot之使用SpringBootCondition


Code for all: GitHub

欢迎关注微信公众号<南瓜慢说>,将持续为你更新...

file

Recommendations:
Cloud Native
Terraform
Container: Docker/Kubernetes
Spring Boot / Spring Cloud
Https
如何制定切实可行的计划并好好执行

  • Author 作者: LarryDpk 南瓜慢说
  • Link 链接: https://www.pkslow.com/archives/springbootcondition
  • 版权声明: 本博客所有文章除特别声明外,不可转载!
# Terraform101 # English # Terraform # Middleware # config # Go # Private # Kubernetes # pkslow # Test # HTTPS # Redis # Docker # Mac # Plan # Stream # MongoDB # Spring # DevOps # JVM # String # Map # Set # List # Performance # Email # Springboot # JavaCollections # ArrayList # Java
Terraform101 English Terraform Middleware config Go Private Kubernetes pkslow Test HTTPS Redis Docker Mac Plan Stream MongoDB Spring DevOps JVM String Map Set List Performance Email Springboot JavaCollections ArrayList Java
Bean初始化操作initMethod、@PostConstruct和InitializingBean
简化RESTful开发,Spring Data REST让你少掉发
  • Contents
  • Site Overview
南瓜慢说

南瓜慢说

多年Java开发,主要专注后端技术:Java/Spring/Springboot/微服务/大数据等。

多读书,多分享;多写作,多整理。

241 Posts
9 Categories
30 Tags
RSS
0%
© 2020 — 2022 南瓜慢说 pkslow The WebSite keeping alive:   粤ICP备20036375号