Merge branch '3.0.x' into 3.1.x

Closes gh-36359
This commit is contained in:
Andy Wilkinson
2023-07-12 12:00:11 +01:00
3 changed files with 59 additions and 3 deletions

View File

@@ -216,9 +216,6 @@ public final class ConfigurationPropertiesBean {
if (bindTarget.getBindMethod() == null && factoryMethod != null) {
bindTarget = bindTarget.withBindMethod(JAVA_BEAN_BIND_METHOD);
}
if (bindTarget.getBindMethod() == null) {
bindTarget = bindTarget.withBindMethod(deduceBindMethod(bindTarget));
}
if (bindTarget.getBindMethod() != VALUE_OBJECT_BIND_METHOD) {
bindTarget = bindTarget.withExistingValue(bean);
}

View File

@@ -72,6 +72,7 @@ import org.springframework.boot.testsupport.system.OutputCaptureExtension;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@@ -1152,6 +1153,14 @@ class ConfigurationPropertiesTests {
assertThat(bean.getNested().name()).isEqualTo("spring");
}
@Test
void loadWhenPotentiallyConstructorBoundPropertiesAreImportedUsesJavaBeanBinding() {
load(PotentiallyConstructorBoundPropertiesImporter.class, "test.prop=alpha");
PotentiallyConstructorBoundProperties properties = this.context
.getBean(PotentiallyConstructorBoundProperties.class);
assertThat(properties.getProp()).isEqualTo("alpha");
}
private AnnotationConfigApplicationContext load(Class<?> configuration, String... inlinedProperties) {
return load(new Class<?>[] { configuration }, inlinedProperties);
}
@@ -3004,4 +3013,34 @@ class ConfigurationPropertiesTests {
static record NestedRecord(String name) {
}
@EnableConfigurationProperties
@Import(PotentiallyConstructorBoundProperties.class)
static class PotentiallyConstructorBoundPropertiesImporter {
@Bean
String notAProperty() {
return "notAProperty";
}
}
@ConfigurationProperties("test")
static class PotentiallyConstructorBoundProperties {
private String prop;
PotentiallyConstructorBoundProperties(String notAProperty) {
}
String getProp() {
return this.prop;
}
void setProp(String prop) {
this.prop = prop;
}
}
}

View File

@@ -22,6 +22,7 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry
import org.springframework.beans.factory.support.RootBeanDefinition
import org.springframework.context.annotation.AnnotationConfigApplicationContext
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Import
import org.springframework.test.context.support.TestPropertySourceUtils
import org.assertj.core.api.Assertions.assertThat
@@ -68,6 +69,14 @@ class KotlinConfigurationPropertiesTests {
assertThat(properties.inner.bravo).isEqualTo("two")
}
@Test
fun `mutable data class properties can be imported`() {
this.context.register(MutableDataClassPropertiesImporter::class.java)
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, "mutable.prop=alpha");
this.context.refresh();
assertThat(this.context.getBean(MutableDataClassProperties::class.java).prop).isEqualTo("alpha")
}
@ConfigurationProperties(prefix = "foo")
class BingProperties(@Suppress("UNUSED_PARAMETER") bar: String) {
@@ -106,4 +115,15 @@ class KotlinConfigurationPropertiesTests {
}
@EnableConfigurationProperties
@Configuration(proxyBeanMethods = false)
@Import(MutableDataClassProperties::class)
class MutableDataClassPropertiesImporter {
}
@ConfigurationProperties(prefix = "mutable")
data class MutableDataClassProperties(
var prop: String = ""
)
}