Make @ConditionalOn[Boolean]Property @Repeatable
Update `ConditionalOnProperty`, `ConditionalOnBooleanProperty` and `OnPropertyCondition` to support `@Repeatable`. Closes gh-2541
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2012-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.condition;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
|
||||
/**
|
||||
* Container annotation that aggregates several
|
||||
* {@link ConditionalOnProperty @ConditionalOnProperty} annotations.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 3.5.0
|
||||
* @see ConditionalOnBooleanProperty
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.TYPE, ElementType.METHOD })
|
||||
@Documented
|
||||
@Conditional(OnPropertyCondition.class)
|
||||
public @interface ConditionalOnBooleanProperties {
|
||||
|
||||
/**
|
||||
* Return the contained
|
||||
* {@link ConditionalOnBooleanProperty @ConditionalOnBooleanProperty} annotations.
|
||||
* @return the contained annotations
|
||||
*/
|
||||
ConditionalOnBooleanProperty[] value();
|
||||
|
||||
}
|
||||
@@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.condition;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Repeatable;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
@@ -43,6 +44,7 @@ import org.springframework.core.env.Environment;
|
||||
@Target({ ElementType.TYPE, ElementType.METHOD })
|
||||
@Documented
|
||||
@Conditional(OnPropertyCondition.class)
|
||||
@Repeatable(ConditionalOnBooleanProperties.class)
|
||||
public @interface ConditionalOnBooleanProperty {
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2012-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.condition;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
|
||||
/**
|
||||
* Container annotation that aggregates several
|
||||
* {@link ConditionalOnProperty @ConditionalOnProperty} annotations.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 3.5.0
|
||||
* @see ConditionalOnProperty
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.TYPE, ElementType.METHOD })
|
||||
@Documented
|
||||
@Conditional(OnPropertyCondition.class)
|
||||
public @interface ConditionalOnProperties {
|
||||
|
||||
/**
|
||||
* Return the contained {@link ConditionalOnProperty @ConditionalOnProperty}
|
||||
* annotations.
|
||||
* @return the contained annotations
|
||||
*/
|
||||
ConditionalOnProperty[] value();
|
||||
|
||||
}
|
||||
@@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.condition;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Repeatable;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
@@ -94,6 +95,7 @@ import org.springframework.core.env.Environment;
|
||||
@Target({ ElementType.TYPE, ElementType.METHOD })
|
||||
@Documented
|
||||
@Conditional(OnPropertyCondition.class)
|
||||
@Repeatable(ConditionalOnProperties.class)
|
||||
public @interface ConditionalOnProperty {
|
||||
|
||||
/**
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.condition;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@@ -28,6 +29,7 @@ import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.AnnotationAttributes;
|
||||
import org.springframework.core.annotation.MergedAnnotation;
|
||||
import org.springframework.core.annotation.MergedAnnotationPredicates;
|
||||
import org.springframework.core.annotation.MergedAnnotations;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.core.env.PropertyResolver;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
@@ -50,11 +52,8 @@ class OnPropertyCondition extends SpringBootCondition {
|
||||
|
||||
@Override
|
||||
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
List<MergedAnnotation<Annotation>> annotations = Stream
|
||||
.concat(metadata.getAnnotations().stream(ConditionalOnProperty.class.getName()),
|
||||
metadata.getAnnotations().stream(ConditionalOnBooleanProperty.class.getName()))
|
||||
.filter(MergedAnnotationPredicates.unique(MergedAnnotation::getMetaTypes))
|
||||
.toList();
|
||||
MergedAnnotations mergedAnnotations = metadata.getAnnotations();
|
||||
List<MergedAnnotation<Annotation>> annotations = stream(mergedAnnotations).toList();
|
||||
List<ConditionMessage> noMatch = new ArrayList<>();
|
||||
List<ConditionMessage> match = new ArrayList<>();
|
||||
for (MergedAnnotation<Annotation> annotation : annotations) {
|
||||
@@ -67,6 +66,34 @@ class OnPropertyCondition extends SpringBootCondition {
|
||||
return ConditionOutcome.match(ConditionMessage.of(match));
|
||||
}
|
||||
|
||||
private Stream<MergedAnnotation<Annotation>> stream(MergedAnnotations mergedAnnotations) {
|
||||
return Stream.concat(stream(mergedAnnotations, ConditionalOnProperty.class, ConditionalOnProperties.class),
|
||||
stream(mergedAnnotations, ConditionalOnBooleanProperty.class, ConditionalOnBooleanProperties.class));
|
||||
}
|
||||
|
||||
private Stream<MergedAnnotation<Annotation>> stream(MergedAnnotations mergedAnnotations,
|
||||
Class<? extends Annotation> type, Class<? extends Annotation> containerType) {
|
||||
return Stream.concat(stream(mergedAnnotations, type), streamRepeated(mergedAnnotations, type, containerType));
|
||||
}
|
||||
|
||||
private Stream<MergedAnnotation<Annotation>> streamRepeated(MergedAnnotations mergedAnnotations,
|
||||
Class<? extends Annotation> type, Class<? extends Annotation> containerType) {
|
||||
return stream(mergedAnnotations, containerType).flatMap((container) -> streamRepeated(container, type));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Stream<MergedAnnotation<Annotation>> streamRepeated(MergedAnnotation<Annotation> container,
|
||||
Class<? extends Annotation> type) {
|
||||
MergedAnnotation<? extends Annotation>[] repeated = container.getAnnotationArray(MergedAnnotation.VALUE, type);
|
||||
return Arrays.stream((MergedAnnotation<Annotation>[]) repeated);
|
||||
}
|
||||
|
||||
private Stream<MergedAnnotation<Annotation>> stream(MergedAnnotations annotations,
|
||||
Class<? extends Annotation> containerType) {
|
||||
return annotations.stream(containerType.getName())
|
||||
.filter(MergedAnnotationPredicates.unique(MergedAnnotation::getMetaTypes));
|
||||
}
|
||||
|
||||
private ConditionOutcome determineOutcome(MergedAnnotation<Annotation> annotation, PropertyResolver resolver) {
|
||||
Class<Annotation> annotationType = annotation.getType();
|
||||
Spec spec = new Spec(annotationType, annotation.asAnnotationAttributes());
|
||||
|
||||
@@ -178,6 +178,22 @@ class ConditionalOnBooleanPropertyTests {
|
||||
.contains("@ConditionalOnBooleanProperty (test=true) found different value in property 'test'");
|
||||
}
|
||||
|
||||
@Test
|
||||
void repeatablePropertiesConditionReportWhenMatched() {
|
||||
load(RepeatablePropertiesRequiredConfiguration.class, "property1=true", "property2=true");
|
||||
assertThat(this.context.containsBean("foo")).isTrue();
|
||||
String report = getConditionEvaluationReport();
|
||||
assertThat(report).contains("@ConditionalOnBooleanProperty (property1=true) matched");
|
||||
assertThat(report).contains("@ConditionalOnBooleanProperty (property2=true) matched");
|
||||
}
|
||||
|
||||
@Test
|
||||
void repeatablePropertiesConditionReportWhenDoesNotMatch() {
|
||||
load(RepeatablePropertiesRequiredConfiguration.class, "property1=true");
|
||||
assertThat(getConditionEvaluationReport())
|
||||
.contains("@ConditionalOnBooleanProperty (property2=true) did not find property 'property2'");
|
||||
}
|
||||
|
||||
private <T extends Exception> Consumer<T> causeMessageContaining(String message) {
|
||||
return (ex) -> assertThat(ex.getCause()).hasMessageContaining(message);
|
||||
}
|
||||
@@ -266,4 +282,16 @@ class ConditionalOnBooleanPropertyTests {
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnBooleanProperty("property1")
|
||||
@ConditionalOnBooleanProperty("property2")
|
||||
static class RepeatablePropertiesRequiredConfiguration {
|
||||
|
||||
@Bean
|
||||
String foo() {
|
||||
return "foo";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -274,19 +274,35 @@ class ConditionalOnPropertyTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void conditionReportWhenMatched() {
|
||||
void multiplePropertiesConditionReportWhenMatched() {
|
||||
load(MultiplePropertiesRequiredConfiguration.class, "property1=value1", "property2=value2");
|
||||
assertThat(this.context.containsBean("foo")).isTrue();
|
||||
assertThat(getConditionEvaluationReport()).contains("@ConditionalOnProperty ([property1,property2]) matched");
|
||||
}
|
||||
|
||||
@Test
|
||||
void conditionReportWhenDoesNotMatch() {
|
||||
void multiplePropertiesConditionReportWhenDoesNotMatch() {
|
||||
load(MultiplePropertiesRequiredConfiguration.class, "property1=value1");
|
||||
assertThat(getConditionEvaluationReport())
|
||||
.contains("@ConditionalOnProperty ([property1,property2]) did not find property 'property2'");
|
||||
}
|
||||
|
||||
@Test
|
||||
void repeatablePropertiesConditionReportWhenMatched() {
|
||||
load(RepeatablePropertiesRequiredConfiguration.class, "property1=value1", "property2=value2");
|
||||
assertThat(this.context.containsBean("foo")).isTrue();
|
||||
String report = getConditionEvaluationReport();
|
||||
assertThat(report).contains("@ConditionalOnProperty (property1) matched");
|
||||
assertThat(report).contains("@ConditionalOnProperty (property2) matched");
|
||||
}
|
||||
|
||||
@Test
|
||||
void repeatablePropertiesConditionReportWhenDoesNotMatch() {
|
||||
load(RepeatablePropertiesRequiredConfiguration.class, "property1=value1");
|
||||
assertThat(getConditionEvaluationReport())
|
||||
.contains("@ConditionalOnProperty (property2) did not find property 'property2'");
|
||||
}
|
||||
|
||||
private void load(Class<?> config, String... environment) {
|
||||
TestPropertyValues.of(environment).applyTo(this.environment);
|
||||
this.context = new SpringApplicationBuilder(config).environment(this.environment)
|
||||
@@ -315,6 +331,18 @@ class ConditionalOnPropertyTests {
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnProperty("property1")
|
||||
@ConditionalOnProperty("property2")
|
||||
static class RepeatablePropertiesRequiredConfiguration {
|
||||
|
||||
@Bean
|
||||
String foo() {
|
||||
return "foo";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnProperty(prefix = "spring.", name = "the-relaxed-property")
|
||||
static class RelaxedPropertiesRequiredConfiguration {
|
||||
|
||||
Reference in New Issue
Block a user