Revert "Support constructor binding on 3rd party classes"

This commit reverts the support of constructor binding on 3rd party
classes using @ImportConfigurationPropertiesBean

See gh-23172

Closes gh-23593
This commit is contained in:
Stephane Nicoll
2020-10-06 16:06:10 +02:00
parent 2595258494
commit 1296b4dfe6
39 changed files with 139 additions and 1413 deletions

View File

@@ -1242,7 +1242,7 @@ Spring Boot provides infrastructure to bind `@ConfigurationProperties` types and
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.
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.
In these cases, specify the list of types to process using the `@EnableConfigurationProperties` or `@ImportAsConfigurationPropertiesBean` annotations.
In these cases, specify the list of types to process using the `@EnableConfigurationProperties` annotation.
This can be done on any `@Configuration` class, as shown in the following example:
[source,java,indent=0]
@@ -1268,7 +1268,7 @@ If you want to define specific packages to scan, you can do so as shown in the f
[NOTE]
====
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.
When the `@ConfigurationProperties` bean is registered using configuration property scanning or via `@EnableConfigurationProperties`, 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.
If the annotation does not provide any prefix, only the fully qualified name of the bean is used.
The bean name in the example above is `acme-com.example.AcmeProperties`.
@@ -1336,26 +1336,12 @@ To configure a bean from the `Environment` properties, add `@ConfigurationProper
----
@ConfigurationProperties(prefix = "another")
@Bean
public ExampleItem exampleItem() {
public AnotherComponent anotherComponent() {
...
}
----
Any JavaBean property defined with the `another` prefix is mapped onto that `ExampleItem` bean in manner similar to the preceding `AcmeProperties` example.
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.
For those situations, you can use an `@ImportAsConfigurationPropertiesBean` annotation on your `@Configuration` or `@SpringBootApplication` class.
[source,java,indent=0]
----
@SpringBootApplication
@ImportAsConfigurationPropertiesBean(type = ExampleItem.class, prefix = "another")
public class MyApp {
...
}
----
TIP: `@ImportAsConfigurationPropertiesBean` also works for JavaBean bindings as long as the type has a single no-arg constructor
Any JavaBean property defined with the `another` prefix is mapped onto that `AnotherComponent` bean in manner similar to the preceding `AcmeProperties` example.