Ensure direct @PropertySource annotations override meta-annotations
Prior to this commit, there was an issue with the semantics of property source overrides. Specifically, a @PropertySource annotation present as a meta-annotation on a @Configuration class was registered with higher precedence than a @PropertySource annotation declared closer to (or directly on) the @Configuration class. Consequently, there was no way for a "local" @PropertySource annotation to override properties registered via @PropertySource as a meta-annotation. This commit addresses this issue by introducing a new overloaded getMergedRepeatableAnnotationAttributes() variant in AnnotatedTypeMetadata that allows the caller to supply a sortByReversedMetaDistance flag. When set to `true`, the annotation search results will be sorted in reversed order based on each annotation's meta distance, which effectively orders meta-annotations before annotations that are declared directly on the underlying element. ConfigurationClassParser and AnnotationConfigUtils have been updated to use this new repeatable annotation search method for @PropertySource. Closes gh-31074
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
package org.springframework.core.type;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -179,14 +180,56 @@ public interface AnnotatedTypeMetadata {
|
||||
* @return the set of all merged repeatable {@code AnnotationAttributes} found,
|
||||
* or an empty set if none were found
|
||||
* @since 6.1
|
||||
* @see #getMergedRepeatableAnnotationAttributes(Class, Class, boolean, boolean)
|
||||
*/
|
||||
default Set<AnnotationAttributes> getMergedRepeatableAnnotationAttributes(
|
||||
Class<? extends Annotation> annotationType, Class<? extends Annotation> containerType,
|
||||
boolean classValuesAsString) {
|
||||
|
||||
Adapt[] adaptations = Adapt.values(classValuesAsString, true);
|
||||
return getAnnotations().stream()
|
||||
.filter(MergedAnnotationPredicates.typeIn(containerType, annotationType))
|
||||
return getMergedRepeatableAnnotationAttributes(annotationType, containerType, classValuesAsString, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve all <em>repeatable annotations</em> of the given type within the
|
||||
* annotation hierarchy <em>above</em> the underlying element (as direct
|
||||
* annotation or meta-annotation); and for each annotation found, merge that
|
||||
* annotation's attributes with <em>matching</em> attributes from annotations
|
||||
* in lower levels of the annotation hierarchy and store the results in an
|
||||
* instance of {@link AnnotationAttributes}.
|
||||
* <p>{@link org.springframework.core.annotation.AliasFor @AliasFor} semantics
|
||||
* are fully supported, both within a single annotation and within annotation
|
||||
* hierarchies.
|
||||
* <p>If the {@code sortByReversedMetaDistance} flag is set to {@code true},
|
||||
* the results will be sorted in {@link Comparator#reversed() reversed} order
|
||||
* based on each annotation's {@linkplain MergedAnnotation#getDistance()
|
||||
* meta distance}, which effectively orders meta-annotations before annotations
|
||||
* that are declared directly on the underlying element.
|
||||
* @param annotationType the annotation type to find
|
||||
* @param containerType the type of the container that holds the annotations
|
||||
* @param classValuesAsString whether to convert class references to {@code String}
|
||||
* class names for exposure as values in the returned {@code AnnotationAttributes},
|
||||
* instead of {@code Class} references which might potentially have to be loaded
|
||||
* first
|
||||
* @param sortByReversedMetaDistance {@code true} if the results should be
|
||||
* sorted in reversed order based on each annotation's meta distance
|
||||
* @return the set of all merged repeatable {@code AnnotationAttributes} found,
|
||||
* or an empty set if none were found
|
||||
* @since 6.1
|
||||
* @see #getMergedRepeatableAnnotationAttributes(Class, Class, boolean)
|
||||
*/
|
||||
default Set<AnnotationAttributes> getMergedRepeatableAnnotationAttributes(
|
||||
Class<? extends Annotation> annotationType, Class<? extends Annotation> containerType,
|
||||
boolean classValuesAsString, boolean sortByReversedMetaDistance) {
|
||||
|
||||
Stream<MergedAnnotation<Annotation>> stream = getAnnotations().stream()
|
||||
.filter(MergedAnnotationPredicates.typeIn(containerType, annotationType));
|
||||
|
||||
if (sortByReversedMetaDistance) {
|
||||
stream = stream.sorted(reversedMetaDistance());
|
||||
}
|
||||
|
||||
Adapt[] adaptations = Adapt.values(false, true);
|
||||
return stream
|
||||
.map(annotation -> annotation.asAnnotationAttributes(adaptations))
|
||||
.flatMap(attributes -> {
|
||||
if (containerType.equals(attributes.annotationType())) {
|
||||
@@ -197,4 +240,9 @@ public interface AnnotatedTypeMetadata {
|
||||
.collect(Collectors.toCollection(LinkedHashSet::new));
|
||||
}
|
||||
|
||||
|
||||
private static Comparator<MergedAnnotation<Annotation>> reversedMetaDistance() {
|
||||
return Comparator.<MergedAnnotation<Annotation>> comparingInt(MergedAnnotation::getDistance).reversed();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user