列表或数组的@ConditionalOnProperty?

时间:2017-01-28 23:28:22

标签: java spring properties

我正在使用Spring Boot 1.4.3 @AutoConfiguration,我根据用户指定的属性自动创建bean。用户可以指定一系列服务,其中名称版本是必填字段:

service[0].name=myServiceA
service[0].version=1.0

service[1].name=myServiceB
service[1].version=1.2

...

如果用户忘记在一个服务上忘记指定必填字段,我想退避而不创建任何bean。我可以使用@ConditionalOnProperty完成此操作吗?我想要这样的东西:

@Configuration
@ConditionalOnProperty({"service[i].name", "service[i].version"})
class AutoConfigureServices {
....
} 

3 个答案:

答案 0 :(得分:2)

这是我创建的自定义Condition。它需要一些抛光才能更通用(即不是硬编码字符串),但对我来说效果很好。

要使用,我使用@Conditional(RequiredRepeatablePropertiesCondition.class)

注释我的Configuration类
public class RequiredRepeatablePropertiesCondition extends SpringBootCondition {

    private static final Logger LOGGER = LoggerFactory.getLogger(RequiredRepeatablePropertiesCondition.class.getName());

    public static final String[] REQUIRED_KEYS = {
            "my.services[i].version",
            "my.services[i].name"
    };

    @Override
    public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
        List<String> missingProperties = new ArrayList<>();
        RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(context.getEnvironment());
        Map<String, Object> services = resolver.getSubProperties("my.services");
        if (services.size() == 0) {
            missingProperties.addAll(Arrays.asList(REQUIRED_KEYS));
            return getConditionOutcome(missingProperties);
        }
        //gather indexes to check: [0], [1], [3], etc
        Pattern p = Pattern.compile("\\[(\\d+)\\]");
        Set<String> uniqueIndexes = new HashSet<String>();
        for (String key : services.keySet()) {
            Matcher m = p.matcher(key);
            if (m.find()) {
                uniqueIndexes.add(m.group(1));
            }
        }
        //loop each index and check required props
        uniqueIndexes.forEach(index -> {
            for (String genericKey : REQUIRED_KEYS) {
                String multiServiceKey = genericKey.replace("[i]", "[" + index + "]");
                if (!resolver.containsProperty(multiServiceKey)) {
                    missingProperties.add(multiServiceKey);
                }
            }
        });
        return getConditionOutcome(missingProperties);
    }

    private ConditionOutcome getConditionOutcome(List<String> missingProperties) {
        if (missingProperties.isEmpty()) {
            return ConditionOutcome.match(ConditionMessage.forCondition(RequiredRepeatablePropertiesCondition.class.getCanonicalName())
                    .found("property", "properties")
                    .items(Arrays.asList(REQUIRED_KEYS)));
        }
        return ConditionOutcome.noMatch(
                ConditionMessage.forCondition(RequiredRepeatablePropertiesCondition.class.getCanonicalName())
            .didNotFind("property", "properties")
            .items(missingProperties)
        );
    }
}

答案 1 :(得分:0)

旧问题,但我希望我的回答对Spring2.x有帮助: 感谢@Brian,我检查了迁移指南,并从中受到示例代码的启发。这段代码对我有用:

final List<String> services = Binder.get(context.getEnvironment()).bind("my.services", List.class).orElse(null);

我确实尝试获取POJO列表(作为AutoConfigureService),但是我的课程不同于AutoConfigureServices。为此,我使用了:

final Services services = Binder.get(context.getEnvironment()).bind("my.services", Services.class).orElse(null);

好吧,继续玩:-D

答案 2 :(得分:0)

这是我在Spring自动配置中使用自定义条件的问题。类似于@Strumbels提出的内容,但更可重用。

@Conditional批注在应用程序启动期间非常早地执行。属性源已经加载,但是尚未创建ConfgurationProperties Bean。但是,我们可以通过将属性自己绑定到Java POJO来解决该问题。

首先,我介绍一个功能接口,该接口使我们能够定义任何自定义逻辑检查属性是否存在。在您的情况下,此方法将检查属性List是否为空/空,以及其中的所有项目是否有效。

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接口的自己的配置属性类。

@ConfigurationProperties("your.property.prefix")
@ConstructorBinding
public class YourConfigurationProperties implements OptionalProperties {

  // Service is your POJO representing the name and version subproperties
  private final List<Service> services;

  @Override
  public boolean isPresent() {
    return services != null && services.stream().all(Service::isValid);
  }

}

然后在Spring @Configuration类中。

@Configuration
@ConditionalOnConfigurationProperties(prefix = "", targetClass = YourConfigurationProperties.class)
class AutoConfigureServices {
....
} 

此解决方案有两个缺点:

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