Skip to content

Commit 5f49d4a

Browse files
committed
Rename @ConfigurationPropertiesImport
Rename `@ConfigurationPropertiesImport` to `@ImportAsConfigurationPropertiesBean` and also refine the registrar so that it can be used with type directly annotated with `@ConfigurationProperties`. Closes gh-23172
1 parent 7d5f331 commit 5f49d4a

File tree

25 files changed

+260
-130
lines changed

25 files changed

+260
-130
lines changed

spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,7 +1227,7 @@ Spring Boot provides infrastructure to bind `@ConfigurationProperties` types and
12271227
You can either enable configuration properties on a class-by-class basis or enable configuration property scanning that works in a similar manner to component scanning.
12281228

12291229
Sometimes, classes annotated with `@ConfigurationProperties` might not be suitable for scanning, for example, if you're developing your own auto-configuration or you want to enable them conditionally.
1230-
In these cases, specify the list of types to process using the `@EnableConfigurationProperties` or `@ConfigurationPropertiesImport` annotations.
1230+
In these cases, specify the list of types to process using the `@EnableConfigurationProperties` or `@ImportAsConfigurationPropertiesBean` annotations.
12311231
This can be done on any `@Configuration` class, as shown in the following example:
12321232

12331233
[source,java,indent=0]
@@ -1253,7 +1253,7 @@ If you want to define specific packages to scan, you can do so as shown in the f
12531253

12541254
[NOTE]
12551255
====
1256-
When the `@ConfigurationProperties` bean is registered using configuration property scanning or via `@EnableConfigurationProperties` or `@ConfigurationPropertiesImport`, the bean has a conventional name: `<prefix>-<fqn>`, where `<prefix>` is the environment key prefix specified in the `@ConfigurationProperties` annotation and `<fqn>` is the fully qualified name of the bean.
1256+
When the `@ConfigurationProperties` bean is registered using configuration property scanning or via `@EnableConfigurationProperties` or `@ImportAsConfigurationPropertiesBean`, the bean has a conventional name: `<prefix>-<fqn>`, where `<prefix>` is the environment key prefix specified in the `@ConfigurationProperties` annotation and `<fqn>` is the fully qualified name of the bean.
12571257
If the annotation does not provide any prefix, only the fully qualified name of the bean is used.
12581258
12591259
The bean name in the example above is `acme-com.example.AcmeProperties`.
@@ -1333,18 +1333,18 @@ To configure a bean from the `Environment` properties, add `@ConfigurationProper
13331333
Any JavaBean property defined with the `another` prefix is mapped onto that `ExampleItem` bean in manner similar to the preceding `AcmeProperties` example.
13341334

13351335
If you want to use constructor binding with a third-party class, you can't use a `@Bean` method since Spring will need to create the object instance.
1336-
For those situations, you can use an `@ConfigurationPropertiesImport` annotation on your `@Configuration` or `@SpringBootApplication` class.
1336+
For those situations, you can use an `@ImportAsConfigurationPropertiesBean` annotation on your `@Configuration` or `@SpringBootApplication` class.
13371337

13381338
[source,java,indent=0]
13391339
----
13401340
@SpringBootApplication
1341-
@ConfigurationPropertiesImport(type = ExampleItem.class, prefix = "another")
1341+
@ImportAsConfigurationPropertiesBean(type = ExampleItem.class, prefix = "another")
13421342
public class MyApp {
13431343
...
13441344
}
13451345
----
13461346

1347-
TIP: `@ConfigurationPropertiesImport` also works for JavaBean bindings as long as the type has a single no-arg constructor
1347+
TIP: `@ImportAsConfigurationPropertiesBean` also works for JavaBean bindings as long as the type has a single no-arg constructor
13481348

13491349

13501350

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessor.java

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
8080

8181
static final String NAME_ANNOTATION = "org.springframework.boot.context.properties.bind.Name";
8282

83-
static final String CONFIGURATION_PROPERTIES_IMPORT_ANNOATION = "org.springframework.boot.context.properties.ConfigurationPropertiesImport";
83+
static final String IMPORT_AS_CONFIGURATION_PROPERTIES_BEAN_ANNOATION = "org.springframework.boot.context.properties.ImportAsConfigurationPropertiesBean";
8484

85-
static final String CONFIGURATION_PROPERTIES_IMPORTS_ANNOATION = "org.springframework.boot.context.properties.ConfigurationPropertiesImports";
85+
static final String IMPORT_AS_CONFIGURATION_PROPERTIES_BEANS_ANNOATION = "org.springframework.boot.context.properties.ImportAsConfigurationPropertiesBeans";
8686

8787
private static final Set<String> SUPPORTED_OPTIONS = Collections
8888
.unmodifiableSet(Collections.singleton(ADDITIONAL_METADATA_LOCATIONS_OPTION));
@@ -125,12 +125,12 @@ protected String nameAnnotation() {
125125
return NAME_ANNOTATION;
126126
}
127127

128-
protected String configurationPropertiesImportAnnotation() {
129-
return CONFIGURATION_PROPERTIES_IMPORT_ANNOATION;
128+
protected String importAsConfigurationPropertiesBeanAnnotation() {
129+
return IMPORT_AS_CONFIGURATION_PROPERTIES_BEAN_ANNOATION;
130130
}
131131

132-
protected String configurationPropertiesImportsAnnotation() {
133-
return CONFIGURATION_PROPERTIES_IMPORTS_ANNOATION;
132+
protected String importAsConfigurationPropertiesBeansAnnotation() {
133+
return IMPORT_AS_CONFIGURATION_PROPERTIES_BEANS_ANNOATION;
134134
}
135135

136136
@Override
@@ -151,16 +151,16 @@ public synchronized void init(ProcessingEnvironment env) {
151151
this.metadataEnv = new MetadataGenerationEnvironment(env, configurationPropertiesAnnotation(),
152152
nestedConfigurationPropertyAnnotation(), deprecatedConfigurationPropertyAnnotation(),
153153
constructorBindingAnnotation(), defaultValueAnnotation(), endpointAnnotation(),
154-
readOperationAnnotation(), nameAnnotation(), configurationPropertiesImportAnnotation(),
155-
configurationPropertiesImportsAnnotation());
154+
readOperationAnnotation(), nameAnnotation(), importAsConfigurationPropertiesBeanAnnotation(),
155+
importAsConfigurationPropertiesBeansAnnotation());
156156
}
157157

158158
@Override
159159
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
160160
this.metadataCollector.processing(roundEnv);
161161
processConfigurationProperties(roundEnv);
162162
processEndpoint(roundEnv);
163-
processConfigurationPropertiesImport(roundEnv);
163+
processImportAsConfigurationProperties(roundEnv);
164164
if (roundEnv.processingOver()) {
165165
try {
166166
writeMetaData();
@@ -188,22 +188,22 @@ private void processEndpoint(RoundEnvironment roundEnv) {
188188
}
189189
}
190190

191-
private void processConfigurationPropertiesImport(RoundEnvironment roundEnv) {
192-
TypeElement configurationPropertiesImportType = this.metadataEnv
193-
.getConfigurationPropertiesImportAnnotationElement();
194-
TypeElement configurationPropertiesImportsType = this.metadataEnv
195-
.getConfigurationPropertiesImportsAnnotationElement();
196-
if (configurationPropertiesImportType == null && configurationPropertiesImportsType == null) {
191+
private void processImportAsConfigurationProperties(RoundEnvironment roundEnv) {
192+
TypeElement importAsConfigurationPropertiesBeanType = this.metadataEnv
193+
.getImportAsConfigurationPropertiesBeansAnnotation();
194+
TypeElement importAsConfigurationPropertiesBeansType = this.metadataEnv
195+
.getImportAsConfigurationPropertiesBeansAnnotationElement();
196+
if (importAsConfigurationPropertiesBeanType == null && importAsConfigurationPropertiesBeansType == null) {
197197
return;
198198
}
199199
Set<Element> elements = new LinkedHashSet<>();
200-
if (configurationPropertiesImportType != null) {
201-
elements.addAll(roundEnv.getElementsAnnotatedWith(configurationPropertiesImportType));
200+
if (importAsConfigurationPropertiesBeanType != null) {
201+
elements.addAll(roundEnv.getElementsAnnotatedWith(importAsConfigurationPropertiesBeanType));
202202
}
203-
if (configurationPropertiesImportsType != null) {
204-
elements.addAll(roundEnv.getElementsAnnotatedWith(configurationPropertiesImportsType));
203+
if (importAsConfigurationPropertiesBeansType != null) {
204+
elements.addAll(roundEnv.getElementsAnnotatedWith(importAsConfigurationPropertiesBeansType));
205205
}
206-
elements.forEach(this::processConfigurationPropertiesImport);
206+
elements.forEach(this::processImportAsConfigurationPropertiesBean);
207207
}
208208

209209
private Map<Element, List<Element>> getElementsAnnotatedOrMetaAnnotatedWith(RoundEnvironment roundEnv,
@@ -314,18 +314,28 @@ private void processEndpoint(AnnotationMirror annotation, TypeElement element) {
314314
}
315315
}
316316

317-
private void processConfigurationPropertiesImport(Element element) {
318-
this.metadataEnv.getConfigurationPropertiesImportAnnotations(element)
319-
.forEach(this::processConfigurationPropertiesImport);
317+
private void processImportAsConfigurationPropertiesBean(Element element) {
318+
this.metadataEnv.getImportAsConfigurationPropertiesBeanAnnotations(element)
319+
.forEach(this::processImportAsConfigurationPropertiesBean);
320320
}
321321

322322
@SuppressWarnings("unchecked")
323-
private void processConfigurationPropertiesImport(AnnotationMirror annotation) {
323+
private void processImportAsConfigurationPropertiesBean(AnnotationMirror annotation) {
324324
String prefix = getPrefix(annotation);
325-
List<TypeMirror> types = (List<TypeMirror>) this.metadataEnv.getAnnotationElementValues(annotation).get("type");
326-
for (TypeMirror type : types) {
327-
Element element = this.metadataEnv.getTypeUtils().asElement(type);
328-
processAnnotatedTypeElement(prefix, (TypeElement) element, true, new Stack<>());
325+
processImportAsConfigurationPropertiesBeanTypes(prefix,
326+
(List<TypeMirror>) this.metadataEnv.getAnnotationElementValues(annotation).get("type"));
327+
processImportAsConfigurationPropertiesBeanTypes(prefix,
328+
(List<TypeMirror>) this.metadataEnv.getAnnotationElementValues(annotation).get("value"));
329+
}
330+
331+
private void processImportAsConfigurationPropertiesBeanTypes(String prefix, List<TypeMirror> types) {
332+
if (types != null) {
333+
for (TypeMirror type : types) {
334+
Element element = this.metadataEnv.getTypeUtils().asElement(type);
335+
AnnotationMirror annotation = this.metadataEnv.getConfigurationPropertiesAnnotation(element);
336+
prefix = (annotation != null) ? getPrefix(annotation) : prefix;
337+
processAnnotatedTypeElement(prefix, (TypeElement) element, true, new Stack<>());
338+
}
329339
}
330340
}
331341

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/MetadataGenerationEnvironment.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,15 @@ class MetadataGenerationEnvironment {
9797

9898
private final String nameAnnotation;
9999

100-
private final String configurationPropertiesImportAnnotation;
100+
private final String importAsConfigurationPropertiesBeanAnnotation;
101101

102-
private final String configurationPropertiesImportsAnnotation;
102+
private final String importAsConfigurationPropertiesBeansAnnotation;
103103

104104
MetadataGenerationEnvironment(ProcessingEnvironment environment, String configurationPropertiesAnnotation,
105105
String nestedConfigurationPropertyAnnotation, String deprecatedConfigurationPropertyAnnotation,
106106
String constructorBindingAnnotation, String defaultValueAnnotation, String endpointAnnotation,
107-
String readOperationAnnotation, String nameAnnotation, String configurationPropertiesImportAnnotation,
108-
String configurationPropertiesImportsAnnotation) {
107+
String readOperationAnnotation, String nameAnnotation, String importAsConfigurationPropertiesBeanAnnotation,
108+
String importAsConfigurationPropertiesBeansAnnotation) {
109109
this.typeUtils = new TypeUtils(environment);
110110
this.elements = environment.getElementUtils();
111111
this.messager = environment.getMessager();
@@ -118,8 +118,8 @@ class MetadataGenerationEnvironment {
118118
this.endpointAnnotation = endpointAnnotation;
119119
this.readOperationAnnotation = readOperationAnnotation;
120120
this.nameAnnotation = nameAnnotation;
121-
this.configurationPropertiesImportAnnotation = configurationPropertiesImportAnnotation;
122-
this.configurationPropertiesImportsAnnotation = configurationPropertiesImportsAnnotation;
121+
this.importAsConfigurationPropertiesBeanAnnotation = importAsConfigurationPropertiesBeanAnnotation;
122+
this.importAsConfigurationPropertiesBeansAnnotation = importAsConfigurationPropertiesBeansAnnotation;
123123
}
124124

125125
private static FieldValuesParser resolveFieldValuesParser(ProcessingEnvironment env) {
@@ -265,12 +265,12 @@ TypeElement getConfigurationPropertiesAnnotationElement() {
265265
return this.elements.getTypeElement(this.configurationPropertiesAnnotation);
266266
}
267267

268-
TypeElement getConfigurationPropertiesImportAnnotationElement() {
269-
return this.elements.getTypeElement(this.configurationPropertiesImportAnnotation);
268+
TypeElement getImportAsConfigurationPropertiesBeansAnnotation() {
269+
return this.elements.getTypeElement(this.importAsConfigurationPropertiesBeanAnnotation);
270270
}
271271

272-
TypeElement getConfigurationPropertiesImportsAnnotationElement() {
273-
return this.elements.getTypeElement(this.configurationPropertiesImportsAnnotation);
272+
TypeElement getImportAsConfigurationPropertiesBeansAnnotationElement() {
273+
return this.elements.getTypeElement(this.importAsConfigurationPropertiesBeansAnnotation);
274274
}
275275

276276
AnnotationMirror getConfigurationPropertiesAnnotation(Element element) {
@@ -297,13 +297,13 @@ AnnotationMirror getNameAnnotation(Element element) {
297297
return getAnnotation(element, this.nameAnnotation);
298298
}
299299

300-
List<AnnotationMirror> getConfigurationPropertiesImportAnnotations(Element element) {
300+
List<AnnotationMirror> getImportAsConfigurationPropertiesBeanAnnotations(Element element) {
301301
List<AnnotationMirror> annotations = new ArrayList<>();
302-
AnnotationMirror importBean = getAnnotation(element, this.configurationPropertiesImportAnnotation);
302+
AnnotationMirror importBean = getAnnotation(element, this.importAsConfigurationPropertiesBeanAnnotation);
303303
if (importBean != null) {
304304
annotations.add(importBean);
305305
}
306-
AnnotationMirror importBeans = getAnnotation(element, this.configurationPropertiesImportsAnnotation);
306+
AnnotationMirror importBeans = getAnnotation(element, this.importAsConfigurationPropertiesBeansAnnotation);
307307
if (importBeans != null) {
308308
AnnotationValue value = importBeans.getElementValues().values().iterator().next();
309309
for (Object contained : (List<?>) value.getValue()) {

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/PropertyDescriptorResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class PropertyDescriptorResolver {
5050
* factory method}, if any.
5151
* @param type the target type
5252
* @param fromImport it the type was imported via a
53-
* {@code @ConfigurationPropertiesImport}
53+
* {@code @ImportAsConfigurationPropertiesBean}
5454
* @param factoryMethod the method that triggered the metadata for that {@code type}
5555
* or {@code null}
5656
* @return the candidate properties for metadata generation

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ImportBeanTests.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,22 @@
2020

2121
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
2222
import org.springframework.boot.configurationprocessor.metadata.Metadata;
23-
import org.springframework.boot.configurationsample.ConfigurationPropertiesImport;
24-
import org.springframework.boot.configurationsample.ConfigurationPropertiesImports;
23+
import org.springframework.boot.configurationsample.ImportAsConfigurationPropertiesBean;
24+
import org.springframework.boot.configurationsample.ImportAsConfigurationPropertiesBeans;
25+
import org.springframework.boot.configurationsample.importbean.ImportAnnotatedJavaBean;
2526
import org.springframework.boot.configurationsample.importbean.ImportJavaBeanConfigurationPropertiesBean;
2627
import org.springframework.boot.configurationsample.importbean.ImportMultipleTypeConfigurationPropertiesBean;
2728
import org.springframework.boot.configurationsample.importbean.ImportRepeatedConfigurationPropertiesBean;
2829
import org.springframework.boot.configurationsample.importbean.ImportValueObjectConfigurationPropertiesBean;
30+
import org.springframework.boot.configurationsample.importbean.ImportedAnnotatedJavaBean;
2931
import org.springframework.boot.configurationsample.importbean.ImportedJavaBean;
3032
import org.springframework.boot.configurationsample.importbean.ImportedValueObject;
3133

3234
import static org.assertj.core.api.Assertions.assertThat;
3335

3436
/**
35-
* Tests for {@link ConfigurationPropertiesImport} and
36-
* {@link ConfigurationPropertiesImports}.
37+
* Tests for {@link ImportAsConfigurationPropertiesBean} and
38+
* {@link ImportAsConfigurationPropertiesBeans}.
3739
*
3840
* @author Phillip Webb
3941
*/
@@ -69,4 +71,11 @@ void importRepeatedConfigurationPropertiesBean() {
6971
assertThat(metadata).has(Metadata.withProperty("jb.name", String.class).fromSource(ImportedJavaBean.class));
7072
}
7173

74+
@Test
75+
void importAnnotatedJavaBean() {
76+
ConfigurationMetadata metadata = compile(ImportAnnotatedJavaBean.class);
77+
assertThat(metadata)
78+
.has(Metadata.withProperty("test.name", String.class).fromSource(ImportedAnnotatedJavaBean.class));
79+
}
80+
7281
}

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/MetadataGenerationEnvironmentFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public MetadataGenerationEnvironment apply(ProcessingEnvironment environment) {
4040
TestConfigurationMetadataAnnotationProcessor.ENDPOINT_ANNOTATION,
4141
TestConfigurationMetadataAnnotationProcessor.READ_OPERATION_ANNOTATION,
4242
TestConfigurationMetadataAnnotationProcessor.NAME_ANNOTATION,
43-
TestConfigurationMetadataAnnotationProcessor.CONFIGURATION_PROPERTIES_IMPORT_ANNOATION,
44-
TestConfigurationMetadataAnnotationProcessor.CONFIGURATION_PROPERTIES_IMPORTS_ANNOATION);
43+
TestConfigurationMetadataAnnotationProcessor.IMPORT_AS_CONFIGURATION_PROPERTIES_BEAN_ANNOATION,
44+
TestConfigurationMetadataAnnotationProcessor.IMPORT_AS_CONFIGURATION_PROPERTIES_BEANS_ANNOATION);
4545
}
4646

4747
}

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/test/TestConfigurationMetadataAnnotationProcessor.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ public class TestConfigurationMetadataAnnotationProcessor extends ConfigurationM
5757

5858
public static final String NAME_ANNOTATION = "org.springframework.boot.configurationsample.Name";
5959

60-
public static final String CONFIGURATION_PROPERTIES_IMPORT_ANNOATION = "org.springframework.boot.configurationsample.ConfigurationPropertiesImport";
60+
public static final String IMPORT_AS_CONFIGURATION_PROPERTIES_BEAN_ANNOATION = "org.springframework.boot.configurationsample.ImportAsConfigurationPropertiesBean";
6161

62-
public static final String CONFIGURATION_PROPERTIES_IMPORTS_ANNOATION = "org.springframework.boot.configurationsample.ConfigurationPropertiesImports";
62+
public static final String IMPORT_AS_CONFIGURATION_PROPERTIES_BEANS_ANNOATION = "org.springframework.boot.configurationsample.ImportAsConfigurationPropertiesBeans";
6363

6464
private ConfigurationMetadata metadata;
6565

@@ -110,13 +110,13 @@ protected String nameAnnotation() {
110110
}
111111

112112
@Override
113-
protected String configurationPropertiesImportAnnotation() {
114-
return CONFIGURATION_PROPERTIES_IMPORT_ANNOATION;
113+
protected String importAsConfigurationPropertiesBeanAnnotation() {
114+
return IMPORT_AS_CONFIGURATION_PROPERTIES_BEAN_ANNOATION;
115115
}
116116

117117
@Override
118-
protected String configurationPropertiesImportsAnnotation() {
119-
return CONFIGURATION_PROPERTIES_IMPORTS_ANNOATION;
118+
protected String importAsConfigurationPropertiesBeansAnnotation() {
119+
return IMPORT_AS_CONFIGURATION_PROPERTIES_BEANS_ANNOATION;
120120
}
121121

122122
@Override
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@
2626
import org.springframework.core.annotation.AliasFor;
2727

2828
/**
29-
* Alternative to Spring Boot's {@code ConfigurationPropertiesImport} for testing (removes
30-
* the need for a dependency on the real annotation).
29+
* Alternative to Spring Boot's {@code ImportAsConfigurationPropertiesBean} for testing
30+
* (removes the need for a dependency on the real annotation).
3131
*
3232
* @author Phillip Webb
3333
*/
3434
@Target(ElementType.TYPE)
3535
@Retention(RetentionPolicy.RUNTIME)
3636
@Documented
3737
@ConfigurationProperties
38-
@Repeatable(ConfigurationPropertiesImports.class)
39-
public @interface ConfigurationPropertiesImport {
38+
@Repeatable(ImportAsConfigurationPropertiesBeans.class)
39+
public @interface ImportAsConfigurationPropertiesBean {
4040

4141
Class<?>[] type();
4242

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@
2323
import java.lang.annotation.Target;
2424

2525
/**
26-
* Alternative to Spring Boot's {@code ConfigurationPropertiesImports} for testing
26+
* Alternative to Spring Boot's {@code ImportAsConfigurationPropertiesBeans} for testing
2727
* (removes the need for a dependency on the real annotation).
2828
*
2929
* @author Phillip Webb
3030
*/
3131
@Target(ElementType.TYPE)
3232
@Retention(RetentionPolicy.RUNTIME)
3333
@Documented
34-
public @interface ConfigurationPropertiesImports {
34+
public @interface ImportAsConfigurationPropertiesBeans {
3535

36-
ConfigurationPropertiesImport[] value();
36+
ImportAsConfigurationPropertiesBean[] value();
3737

3838
}

0 commit comments

Comments
 (0)