我正在使用 Spring Boot 1.4.3
@AutoConfiguration
,我根据用户指定的属性自动创建 bean。用户可以指定一系列服务,其中 name 和 version 是必填字段:
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 {
....
}
这是我创建的自定义
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)
);
}
}
您可以利用
org.springframework.boot.autoconfigure.condition.OnPropertyListCondition
类。例如,假设您想要检查 service
属性是否至少有一个值:
class MyListCondition extends OnPropertyListCondition {
MyListCondition() {
super("service", () -> ConditionMessage.forCondition("service"));
}
}
@Configuration
@Condition(MyListCondition.class)
class AutoConfigureServices {
}
请参阅 org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration#wsdlDefinitionBeanFactoryPostProcessor 上使用的 org.springframework.boot.autoconfigure.webservices.OnWsdlLocationsCondition,了解 Spring 本身的示例。
老问题,但我希望我的回答对 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
这是我对在 Spring 自动配置中使用自定义条件的问题的看法。有点类似于@Strumbels 的提议,但更可重用。
@Conditional
注释在应用程序启动过程中很早就执行。属性源已加载,但 ConfgurationProperties bean 尚未创建。不过,我们可以通过自己将属性绑定到 Java POJO 来解决这个问题。
首先,我介绍一个功能接口,它使我们能够定义任何自定义逻辑检查属性是否确实存在。在您的情况下,此方法将负责检查属性列表是否为空/空以及其中的所有项目是否有效。
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);
}
}
然后在春季
@Configuration
班。
@Configuration
@ConditionalOnConfigurationProperties(prefix = "", targetClass = YourConfigurationProperties.class)
class AutoConfigureServices {
....
}
此解决方案有两个缺点:
@ConfigurationProperties
注释上和在 @ConditionalOnConfigurationProperties
注释上。通过在配置属性 POJO 中定义 public static final String PREFIX = "namespace"
可以部分缓解这种情况。据我了解,您的问题是如何验证必填字段,为此我的建议是使用 @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}')}")