@ConditionalOnProperty用于列表或数组?

8

我正在使用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 {
....
} 

1
我并不确定这本身是否有效,但是如果你在构造函数中设置这些值,那么可能会抛出异常(无论是在构造函数中显式抛出还是在 Spring 检查存在性时隐式抛出)。 - chrylis -cautiouslyoptimistic-
那是个好主意。如果在实例化任何bean之前,构造函数在自动配置类上运行,也许我可以以某种方式防止创建bean?不幸的是,我不能抛出异常,因为忘记道具不应该是致命的。 - Strumbles
为什么不呢?你有一个半配置的Bean。 - chrylis -cautiouslyoptimistic-
如果bean在构造时只配置了一半,那么构造函数将无法工作。@Autoconfiguration@Conditionals会阻止bean被创建。这个想法是用户依赖于具有自动配置的jar包,但它不应该通过抛出致命错误来阻止他们的应用程序加载。 - Strumbles
5个回答

2
你可以利用 org.springframework.boot.autoconfigure.condition.OnPropertyListCondition 类。例如,假设你想检查 service 属性是否至少有一个值:
class MyListCondition extends OnPropertyListCondition {
    MyListCondition() {
        super("service", () -> ConditionMessage.forCondition("service"));
    }
}

@Configuration
@Condition(MyListCondition.class)
class AutoConfigureServices {

}

请参见 Spring 自身中 org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration#wsdlDefinitionBeanFactoryPostProcessor 上使用的 org.springframework.boot.autoconfigure.webservices.OnWsdlLocationsCondition 的示例。

2
这是我创建的自定义Condition。它需要一些改进,使其更加通用(即不硬编码字符串),但对我来说效果很好。
要使用它,我在我的配置类上使用了注释@Conditional(RequiredRepeatablePropertiesCondition.class)
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)
        );
    }
}

请注意,此代码仅适用于Spring Boot 1.x,Spring Boot 2使用不同的API替换了此代码,请参阅其迁移指南。特别是RelaxedPropertyResolver已不再存在,也没有直接替代RelaxedPropertyResolver.getSubProperties(...)PropertySourceUtils.getSubProperties(...) - bric3
1
@Brice 是正确的,感谢提供迁移指南的链接。我尝试了他们示例中的代码,对我来说运行良好:Binder.get(context.getEnvironment()).bind("my.services", List.class).orElse(null);它返回 List<String>。我没有尝试将其解析为 POJO(像 AutoConfigureService 那样),但对于简单的字符串很有帮助。 - zegee29

0

关于在Spring自动配置中使用自定义条件的问题,我有一些看法。与@Strumbels提出的类似,但更具可重用性。

@Conditional注释在应用程序启动期间非常早就执行了。属性源已经加载,但是ConfigurationProperties bean尚未创建。但是,我们可以通过将属性绑定到Java POJO来解决这个问题。

首先,我介绍一个函数接口,它将使我们能够定义任何自定义逻辑,检查属性是否实际存在。在您的情况下,此方法将负责检查属性列表是否为空/ 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 接口。

@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。它仅在应用程序启动期间发生,因此不应该是一个问题,但仍然存在效率问题。

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


0
根据我的理解,你的问题是如何验证必填字段,对此我建议使用@ConfigurationProperties("root")注解,然后将所有字段都添加为@NotNull,就像这样:
@Getter
@Validated
@RequiredArgsConstructor
@ConfigurationProperties("root")
public class YourProperties {

  private final Set<Item> service;

  @Getter
  @Validated
  @RequiredArgsConstructor
  public static class Item {

    @NotNull
    private final String name;

    @NotNull
    private final String version;
  }
}

如果您希望继续使用条件方法,您可以使用ConditionalOnExpression,但是请注意项目数量是无限的。
@ConditionalOnExpression("#{T(org.springframework.util.StringUtils).hasText('${service[0].name}')}")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接