Remove @ImmutableConfigurationProperties

Closes gh-18563
This commit is contained in:
Andy Wilkinson 2019-10-14 10:42:26 +01:00
parent 1a7a3118d7
commit c75b06c76c
12 changed files with 36 additions and 98 deletions

View File

@ -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.

View File

@ -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;

View File

@ -40,7 +40,6 @@ import org.springframework.core.annotation.AliasFor;
* @since 1.0.0
* @see ConfigurationPropertiesScan
* @see ConstructorBinding
* @see ImmutableConfigurationProperties
* @see ConfigurationPropertiesBindingPostProcessor
* @see EnableConfigurationProperties
*/

View File

@ -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)

View File

@ -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 "";
}

View File

@ -90,7 +90,8 @@ class ConfigurationPropertiesBeanRegistrarTests {
}
@ImmutableConfigurationProperties("valuecp")
@ConstructorBinding
@ConfigurationProperties("valuecp")
static class ValueObjectConfigurationProperties {
ValueObjectConfigurationProperties(String name) {

View File

@ -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 {

View File

@ -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;

View File

@ -128,7 +128,8 @@ class EnableConfigurationPropertiesRegistrarTests {
}
@ImmutableConfigurationProperties(prefix = "bar")
@ConstructorBinding
@ConfigurationProperties(prefix = "bar")
static class BarProperties {
BarProperties(String foo) {

View File

@ -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) {

View File

@ -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;

View File

@ -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")