Skip to content

Commit 6158634

Browse files
committed
Clarified repeatable PropertySource annotation vs use as meta-annotation
Issue: SPR-16592 (cherry picked from commit c4e9ce8)
1 parent 8d8bb04 commit 6158634

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigUtils.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -287,18 +287,23 @@ static Set<AnnotationAttributes> attributesForRepeatable(AnnotationMetadata meta
287287
}
288288

289289
@SuppressWarnings("unchecked")
290-
static Set<AnnotationAttributes> attributesForRepeatable(AnnotationMetadata metadata,
291-
String containerClassName, String annotationClassName) {
290+
static Set<AnnotationAttributes> attributesForRepeatable(
291+
AnnotationMetadata metadata, String containerClassName, String annotationClassName) {
292292

293293
Set<AnnotationAttributes> result = new LinkedHashSet<AnnotationAttributes>();
294+
295+
// Direct annotation present?
294296
addAttributesIfNotNull(result, metadata.getAnnotationAttributes(annotationClassName, false));
295297

298+
// Container annotation present?
296299
Map<String, Object> container = metadata.getAnnotationAttributes(containerClassName, false);
297300
if (container != null && container.containsKey("value")) {
298301
for (Map<String, Object> containedAttributes : (Map<String, Object>[]) container.get("value")) {
299302
addAttributesIfNotNull(result, containedAttributes);
300303
}
301304
}
305+
306+
// Return merged result
302307
return Collections.unmodifiableSet(result);
303308
}
304309

spring-context/src/main/java/org/springframework/context/annotation/PropertySource.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -42,6 +42,7 @@
4242
* &#064;Configuration
4343
* &#064;PropertySource("classpath:/com/myco/app.properties")
4444
* public class AppConfig {
45+
*
4546
* &#064;Autowired
4647
* Environment env;
4748
*
@@ -53,8 +54,8 @@
5354
* }
5455
* }</pre>
5556
*
56-
* Notice that the {@code Environment} object is @{@link
57-
* org.springframework.beans.factory.annotation.Autowired Autowired} into the
57+
* Notice that the {@code Environment} object is
58+
* {@link org.springframework.beans.factory.annotation.Autowired @Autowired} into the
5859
* configuration class and then used when populating the {@code TestBean} object. Given
5960
* the configuration above, a call to {@code testBean.getName()} will return "myTestBean".
6061
*
@@ -79,6 +80,7 @@
7980
* &#064;Configuration
8081
* &#064;PropertySource("classpath:/com/${my.placeholder:default/path}/app.properties")
8182
* public class AppConfig {
83+
*
8284
* &#064;Autowired
8385
* Environment env;
8486
*
@@ -118,9 +120,9 @@
118120
*
119121
* The override ordering depends on the order in which these classes are registered
120122
* with the application context.
123+
*
121124
* <pre class="code">
122-
* AnnotationConfigApplicationContext ctx =
123-
* new AnnotationConfigApplicationContext();
125+
* AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
124126
* ctx.register(ConfigA.class);
125127
* ctx.register(ConfigB.class);
126128
* ctx.refresh();
@@ -139,6 +141,12 @@
139141
* and {@link org.springframework.core.env.MutablePropertySources MutablePropertySources}
140142
* javadocs for details.
141143
*
144+
* <p><b>NOTE: This annotation is repeatable according to Java 8 conventions.</b>
145+
* However, all such {@code @PropertySource} annotations need to be declared at the same
146+
* level: either directly on the configuration class or as meta-annotations within the
147+
* same custom annotation. Mixing of direct annotations and meta-annotations is not
148+
* recommended since direct annotations will effectively override meta-annotations.
149+
*
142150
* @author Chris Beams
143151
* @author Juergen Hoeller
144152
* @author Phillip Webb

0 commit comments

Comments
 (0)