diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 184a4b0b52..5383cc88c3 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -849,10 +849,12 @@ The example in the previous section can be rewritten in an immutable fashion as import java.net.InetAddress; import java.util.List; - import org.springframework.boot.context.properties.ImmutableConfigurationProperties; + import org.springframework.boot.context.properties.ConfigurationProperties; + import org.springframework.boot.context.properties.ConstructorBinding; import org.springframework.boot.context.properties.DefaultValue; - @ImmutableConfigurationProperties("acme") + @ConstructorBinding + @ConfigurationProperties("acme") public class AcmeProperties { private final boolean enabled; @@ -899,18 +901,17 @@ The example in the previous section can be rewritten in an immutable fashion as } ---- -In this setup, the `@ImmutableConfigurationProperties` annotation is used to indicate that constructor binding should be used. +In this setup, the `@ConstructorBinding` annotation is used to indicate that constructor binding should be used. This means that the binder will expect to find a constructor with the parameters that you wish to have bound. -Nested members of a `@ImmutableConfigurationProperties` class (such as `Security` in the example above) will also be bound via their constructor. +Nested members of a `@ConstructorBinding` class (such as `Security` in the example above) will also be bound via their constructor. Default values can be specified using `@DefaultValue` and the same conversion service will be applied to coerce the `String` value to the target type of a missing property. NOTE: To use constructor binding the class must be enabled using `@EnableConfigurationProperties` or configuration property scanning. You cannot use constructor binding with beans that are created by the regular Spring mechanisms (e.g. `@Component` beans, beans created via `@Bean` methods or beans loaded using `@Import`) -TIP: `@ImmutableConfigurationProperties` is actually a meta-annotation composed of `@ConfigurationProperties` and `@ConstructorBinding`. -If you have more than one constructor for your class you can also use `@ConstructorBinding` directly on actual constructor that should be bound. +TIP: If you have more than one constructor for your class you can also use `@ConstructorBinding` directly on the constructor that should be bound. diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/ExampleProperties.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/ExampleProperties.java index 8fd908ae60..7215ca98f7 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/ExampleProperties.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/ExampleProperties.java @@ -16,16 +16,19 @@ package org.springframework.boot.test.autoconfigure.web.client; -import org.springframework.boot.context.properties.ImmutableConfigurationProperties; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.ConstructorBinding; import org.springframework.boot.context.properties.bind.DefaultValue; /** - * Example {@link ImmutableConfigurationProperties @ImmutableConfigurationProperties} used - * to test the use of configuration properties scan with sliced test. + * Example {@link ConstructorBinding constructor-bound} + * {@link ConfigurationProperties @ConfigurationProperties} used to test the use of + * configuration properties scan with sliced test. * * @author Stephane Nicoll */ -@ImmutableConfigurationProperties("example") +@ConstructorBinding +@ConfigurationProperties("example") public class ExampleProperties { private final String name; diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationProperties.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationProperties.java index a32c14b01a..55d045fc99 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationProperties.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationProperties.java @@ -40,7 +40,6 @@ import org.springframework.core.annotation.AliasFor; * @since 1.0.0 * @see ConfigurationPropertiesScan * @see ConstructorBinding - * @see ImmutableConfigurationProperties * @see ConfigurationPropertiesBindingPostProcessor * @see EnableConfigurationProperties */ diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConstructorBinding.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConstructorBinding.java index 54dac121a4..43c1bd8d09 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConstructorBinding.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConstructorBinding.java @@ -30,7 +30,6 @@ import java.lang.annotation.Target; * @author Phillip Webb * @since 2.2.0 * @see ConfigurationProperties - * @see ImmutableConfigurationProperties */ @Target({ ElementType.TYPE, ElementType.CONSTRUCTOR }) @Retention(RetentionPolicy.RUNTIME) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ImmutableConfigurationProperties.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ImmutableConfigurationProperties.java deleted file mode 100644 index 112f8da6c3..0000000000 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ImmutableConfigurationProperties.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright 2012-2019 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.context.properties; - -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.core.annotation.AliasFor; - -/** - * A convenience annotation that can be used for immutable - * {@link ConfigurationProperties @ConfigurationProperties} that use - * {@link ConstructorBinding @ConstructorBinding}. - * - * @author Phillip Webb - * @since 2.2.0 - */ -@Target(ElementType.TYPE) -@Retention(RetentionPolicy.RUNTIME) -@Documented -@ConfigurationProperties -@ConstructorBinding -public @interface ImmutableConfigurationProperties { - - /** - * The prefix of the properties that are valid to bind to this object. Synonym for - * {@link #prefix()}. A valid prefix is defined by one or more words separated with - * dots (e.g. {@code "acme.system.feature"}). - * @return the prefix of the properties to bind - */ - @AliasFor(annotation = ConfigurationProperties.class) - String value() default ""; - - /** - * The prefix of the properties that are valid to bind to this object. Synonym for - * {@link #value()}. A valid prefix is defined by one or more words separated with - * dots (e.g. {@code "acme.system.feature"}). - * @return the prefix of the properties to bind - */ - @AliasFor(annotation = ConfigurationProperties.class) - String prefix() default ""; - -} diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanRegistrarTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanRegistrarTests.java index 5e34617966..865c41f640 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanRegistrarTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanRegistrarTests.java @@ -90,7 +90,8 @@ class ConfigurationPropertiesBeanRegistrarTests { } - @ImmutableConfigurationProperties("valuecp") + @ConstructorBinding + @ConfigurationProperties("valuecp") static class ValueObjectConfigurationProperties { ValueObjectConfigurationProperties(String name) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanTests.java index b1dd46968d..b0f50b451d 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanTests.java @@ -202,12 +202,6 @@ class ConfigurationPropertiesBeanTests { assertThat(bindType).isEqualTo(BindMethod.VALUE_OBJECT); } - @Test - void bindTypeForClassWhenNoMetaConstructorBindingOnTypeReturnsValueObject() { - BindMethod bindType = BindMethod.forClass(MetaConstructorBindingOnType.class); - assertThat(bindType).isEqualTo(BindMethod.VALUE_OBJECT); - } - @Test void bindTypeForClassWhenNoConstructorBindingOnConstructorReturnsValueObject() { BindMethod bindType = BindMethod.forClass(ConstructorBindingOnConstructor.class); @@ -383,14 +377,6 @@ class ConfigurationPropertiesBeanTests { } - @ImmutableConfigurationProperties - static class MetaConstructorBindingOnType { - - MetaConstructorBindingOnType(String name) { - } - - } - @ConfigurationProperties static class ConstructorBindingOnConstructor { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java index 30fde52d13..4243ba3afd 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java @@ -1806,7 +1806,8 @@ class ConfigurationPropertiesTests { } - @ImmutableConfigurationProperties(prefix = "test") + @ConstructorBinding + @ConfigurationProperties(prefix = "test") static class OtherInjectedProperties { final DataSizeProperties dataSizeProperties; @@ -1823,7 +1824,8 @@ class ConfigurationPropertiesTests { } - @ImmutableConfigurationProperties(prefix = "test") + @ConstructorBinding + @ConfigurationProperties(prefix = "test") @Validated static class ConstructorParameterProperties { @@ -1854,7 +1856,8 @@ class ConfigurationPropertiesTests { } - @ImmutableConfigurationProperties(prefix = "test") + @ConstructorBinding + @ConfigurationProperties(prefix = "test") @Validated static class ConstructorParameterValidatedProperties { @@ -1973,7 +1976,8 @@ class ConfigurationPropertiesTests { } - @ImmutableConfigurationProperties("test") + @ConstructorBinding + @ConfigurationProperties("test") static class NestedConstructorProperties { private final String name; @@ -2033,7 +2037,8 @@ class ConfigurationPropertiesTests { } - @ImmutableConfigurationProperties("test") + @ConstructorBinding + @ConfigurationProperties("test") static class DeducedNestedConstructorProperties { private final String name; diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesRegistrarTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesRegistrarTests.java index 4cc95a8888..d9c0442e89 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesRegistrarTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesRegistrarTests.java @@ -128,7 +128,8 @@ class EnableConfigurationPropertiesRegistrarTests { } - @ImmutableConfigurationProperties(prefix = "bar") + @ConstructorBinding + @ConfigurationProperties(prefix = "bar") static class BarProperties { BarProperties(String foo) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/scan/valid/ConfigurationPropertiesScanConfiguration.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/scan/valid/ConfigurationPropertiesScanConfiguration.java index d316e6031f..107cfe0184 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/scan/valid/ConfigurationPropertiesScanConfiguration.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/scan/valid/ConfigurationPropertiesScanConfiguration.java @@ -17,8 +17,8 @@ package org.springframework.boot.context.properties.scan.valid; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationPropertiesScan; +import org.springframework.boot.context.properties.ConstructorBinding; import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.boot.context.properties.ImmutableConfigurationProperties; import org.springframework.boot.context.properties.scan.valid.b.BScanConfiguration; /** @@ -46,7 +46,8 @@ public class ConfigurationPropertiesScanConfiguration { } - @ImmutableConfigurationProperties(prefix = "bar") + @ConstructorBinding + @ConfigurationProperties(prefix = "bar") static class BarProperties { BarProperties(String foo) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/scan/valid/b/BScanConfiguration.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/scan/valid/b/BScanConfiguration.java index 900e7a30cc..59a24bcf49 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/scan/valid/b/BScanConfiguration.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/scan/valid/b/BScanConfiguration.java @@ -16,7 +16,7 @@ package org.springframework.boot.context.properties.scan.valid.b; import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.boot.context.properties.ImmutableConfigurationProperties; +import org.springframework.boot.context.properties.ConstructorBinding; /** * @author Madhura Bhave @@ -28,7 +28,8 @@ public class BScanConfiguration { } - @ImmutableConfigurationProperties(prefix = "b.first") + @ConstructorBinding + @ConfigurationProperties(prefix = "b.first") public static class BFirstProperties implements BProperties { private final String name; diff --git a/spring-boot-project/spring-boot/src/test/kotlin/org/springframework/boot/context/properties/KotlinConfigurationPropertiesBeanRegistrarTests.kt b/spring-boot-project/spring-boot/src/test/kotlin/org/springframework/boot/context/properties/KotlinConfigurationPropertiesBeanRegistrarTests.kt index 5c64255583..1144590a6f 100644 --- a/spring-boot-project/spring-boot/src/test/kotlin/org/springframework/boot/context/properties/KotlinConfigurationPropertiesBeanRegistrarTests.kt +++ b/spring-boot-project/spring-boot/src/test/kotlin/org/springframework/boot/context/properties/KotlinConfigurationPropertiesBeanRegistrarTests.kt @@ -47,7 +47,8 @@ class KotlinConfigurationPropertiesBeanRegistrarTests { @ConfigurationProperties(prefix = "foo") class FooProperties - @ImmutableConfigurationProperties(prefix = "bar") + @ConstructorBinding + @ConfigurationProperties(prefix = "bar") class BarProperties(val name: String?, val counter: Int = 42) @ConfigurationProperties(prefix = "bing")