java - 基于配置属性中的集合是否为空的 Spring Boot 条件

标签 java spring spring-boot

我有以下类(class):

@Component
@ConifgurationProperties("redis")
public class RedisProperties {
    private List<String> hosts;
    // getters, setters
}

@Component
public class StaticRedisHostsProvider implements RedisHostsProvider {
    private final RedisProperties redisProperties;

    public StaticRedisHostsProvider(RedisProperties redisProperties) {
        this.redisProperties = redisProperties;
    }

    @Override
    public List<String> getAll() {
        return redisProperties.getHosts();
    }
}

@Component
public DiscoveryBasedRedisHostsProvider { ... }

如果指定了 redis.hosts 属性,我希望使用 StaticRedisHostsProvider,否则使用 DiscoveryBasedRedisHostsProvider

我可以用 @ConditionalOnProperty(prefix = "redis", name = "hosts") 注释 StaticRedisHostsProvider,但是没有类似的 @ConditionalOnMissingProperty 注释,用于与 DiscoveryBasedRedisHostsProvider 一起使用。

我尝试使用 @ConditionalOnExpression("@redisProperties.hosts.empty"),但由于某些原因它不起作用:

Description:
A component required a bean named 'redisProperties' that could not be found.
Action:
Consider defining a bean named 'redisProperties' in your configuration.

有什么方法可以解决这个问题(也许使用 @Order 或类似的注释)?

最佳答案

这是我对在 Spring 自动配置中使用自定义条件的问题的看法。

@Conditional 注释在应用程序启动的早期执行。已加载属性源,但尚未创建 ConfgurationProperties bean。但是,我们可以通过自己将属性绑定(bind)到 Java POJO 来解决这个问题。

首先,我介绍了一个功能接口(interface),它使我们能够定义任何自定义逻辑来检查属性是否确实存在。在您的情况下,此方法将负责检查属性列表是否为空或 null。

public interface OptionalProperties {
  boolean isPresent();
}

现在让我们创建一个注释,它将使用 Spring @Conditional 进行元注释,并允许我们定义自定义参数。 prefix 表示属性命名空间,targetClass 表示属性应映射到的配置属性模型类。

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnConfigurationPropertiesCondition.class)
public @interface ConditionalOnConfigurationProperties {

  String prefix();

  Class<? extends OptionalProperties> targetClass();

}

现在是主要部分。自定义条件实现。

public class OnConfigurationPropertiesCondition extends SpringBootCondition {

  @Override
  public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
    MergedAnnotation<ConditionalOnConfigurationProperties> mergedAnnotation = metadata.getAnnotations().get(ConditionalOnConfigurationProperties.class);
    String prefix = mergedAnnotation.getString("prefix");
    Class<?> targetClass = mergedAnnotation.getClass("targetClass");
    // type precondition
    if (!OptionalProperties.class.isAssignableFrom(targetClass)) {
      return ConditionOutcome.noMatch("Target type does not implement the OptionalProperties interface.");
    }
    // the crux of this solution, binding properties to Java POJO
    Object bean = Binder.get(context.getEnvironment()).bind(prefix, targetClass).orElse(null);
    // if properties are not present at all return no match
    if (bean == null) {
      return ConditionOutcome.noMatch("Binding properties to target type resulted in null value.");
    }
    OptionalProperties props = (OptionalProperties) bean;

    // execute method from OptionalProperties interface 
    // to check if condition should be matched or not
    // can include any custom logic using property values in a type safe manner
    if (props.isPresent()) {
      return ConditionOutcome.match();
    } else {
      return ConditionOutcome.noMatch("Properties are not present.");
    }
  }

}

现在您应该创建自己的配置属性类来实现 OptionalProperties 接口(interface)。

@ConfigurationProperties("redis")
@ConstructorBinding
public class RedisProperties implements OptionalProperties {

  private final List<String> hosts;

  @Override
  public boolean isPresent() {
    return hosts != null && !hosts.isEmpty();
  }

}

然后在Spring的@Configuration类中。

@Configuration
class YourConfiguration {

  @ConditionalOnConfigurationProperty(prefix = "redis", targetClass = RedisProperties.class)
  StaticRedisHostsProvider staticRedisHostsProvider() {
    ...
  }

  @ConditionalOnMissingBean(StaticRedisHostsProvider.class)
  DiscoveryBasedRedisHostsProvider discoveryRedisHostsProvider() {
    ...
  }

} 

此解决方案有两个缺点:

  • 必须在两个位置指定属性前缀:@ConfigurationProperties 注释和 @ConditionalOnConfigurationProperties 注释。这可以通过在您的配置属性 POJO 中定义一个 public static final String PREFIX = "namespace" 来部分缓解。
  • 属性绑定(bind)过程在每次使用我们的自定义条件注释时单独执行,然后再次创建配置属性 bean 本身。它仅在应用程序启动期间发生,因此应该不是问题,但仍然效率低下。

关于java - 基于配置属性中的集合是否为空的 Spring Boot 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48925630/

相关文章:

java - Autowiring 导致 Spring Boot 应用程序异常

java - 从外部 jar 文件获取 spring boot 计划的 cron 表达式

java - java excel api 可以满足我的要求吗?

java - JSP 是 servlet 吗?

java - 我可以将相同java包的类文件拆分为Tomcat上的jar文件和classes文件夹吗?

java - 如何在 Spring MVC 中根据用户输入创建新的 url

java - Spring/JPA/persistence实体属性字段不能是final的?

Java 循环输出错误

java - 我如何将一个 bean 合并到另一个 bean 中以进行部分更新

java - Spring安全性返回401甚至permitAll()