Skip to content

Commit de21d71

Browse files
committed
Remove support of @Autowired for configuration properties bean
See spring-projectsgh-8762
1 parent fcdc414 commit de21d71

File tree

4 files changed

+9
-47
lines changed

4 files changed

+9
-47
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesImportSelector.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
import org.springframework.beans.BeanUtils;
2727
import org.springframework.beans.factory.BeanFactory;
28-
import org.springframework.beans.factory.annotation.Autowired;
2928
import org.springframework.beans.factory.config.BeanDefinition;
3029
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
3130
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
@@ -151,11 +150,6 @@ private BeanDefinition createBeanDefinition(
151150

152151
private boolean canBindAtCreationTime(Class<?> type) {
153152
List<Constructor<?>> constructors = determineConstructors(type);
154-
boolean autowiredPresent = constructors.stream().anyMatch(
155-
(c) -> AnnotationUtils.findAnnotation(c, Autowired.class) != null);
156-
if (autowiredPresent) {
157-
return false;
158-
}
159153
return (constructors.size() == 1
160154
&& constructors.get(0).getParameterCount() > 0);
161155
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,10 @@ public void loadWhenConfigurationPropertiesContainsMapWithPositiveAndNegativeInt
808808

809809
@Test
810810
public void loadWhenConfigurationPropertiesInjectsAnotherBeanShouldNotFail() {
811-
load(OtherInjectPropertiesConfiguration.class);
811+
assertThatExceptionOfType(ConfigurationPropertiesBindException.class)
812+
.isThrownBy(() -> load(OtherInjectPropertiesConfiguration.class))
813+
.withMessageContaining(OtherInjectedProperties.class.getName())
814+
.withMessageContaining("Failed to bind properties under 'test'");
812815
}
813816

814817
@Test
@@ -1825,7 +1828,6 @@ static class OtherInjectedProperties {
18251828

18261829
final DataSizeProperties dataSizeProperties;
18271830

1828-
@Autowired
18291831
OtherInjectedProperties(ObjectProvider<DataSizeProperties> dataSizeProperties) {
18301832
this.dataSizeProperties = dataSizeProperties.getIfUnique();
18311833
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesImportSelectorTests.java

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import org.junit.Test;
2121

22-
import org.springframework.beans.factory.annotation.Autowired;
2322
import org.springframework.beans.factory.config.BeanDefinition;
2423
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
2524
import org.springframework.beans.factory.support.GenericBeanDefinition;
@@ -70,23 +69,13 @@ public void typeWithDefaultConstructorShouldRegisterGenericBeanDefinition()
7069
assertThat(beanDefinition).isExactlyInstanceOf(GenericBeanDefinition.class);
7170
}
7271

73-
@Test
74-
public void typeWithAutowiredOnConstructorShouldRegisterGenericBeanDefinition()
75-
throws Exception {
76-
this.registrar.registerBeanDefinitions(
77-
getAnnotationMetadata(TestConfiguration.class), this.beanFactory);
78-
BeanDefinition beanDefinition = this.beanFactory.getBeanDefinition(
79-
"bar-org.springframework.boot.context.properties.EnableConfigurationPropertiesImportSelectorTests$BarProperties");
80-
assertThat(beanDefinition).isExactlyInstanceOf(GenericBeanDefinition.class);
81-
}
82-
8372
@Test
8473
public void typeWithOneConstructorWithParametersShouldRegisterConfigurationPropertiesBeanDefinition()
8574
throws Exception {
8675
this.registrar.registerBeanDefinitions(
8776
getAnnotationMetadata(TestConfiguration.class), this.beanFactory);
8877
BeanDefinition beanDefinition = this.beanFactory.getBeanDefinition(
89-
"baz-org.springframework.boot.context.properties.EnableConfigurationPropertiesImportSelectorTests$BazProperties");
78+
"bar-org.springframework.boot.context.properties.EnableConfigurationPropertiesImportSelectorTests$BarProperties");
9079
assertThat(beanDefinition)
9180
.isExactlyInstanceOf(ConfigurationPropertiesBeanDefinition.class);
9281
}
@@ -135,7 +124,7 @@ private AnnotationMetadata getAnnotationMetadata(Class<?> source) throws IOExcep
135124
}
136125

137126
@EnableConfigurationProperties({ FooProperties.class, BarProperties.class,
138-
BazProperties.class, BingProperties.class })
127+
BingProperties.class })
139128
static class TestConfiguration {
140129

141130
}
@@ -163,22 +152,12 @@ static class FooProperties {
163152
@ConfigurationProperties(prefix = "bar")
164153
public static class BarProperties {
165154

166-
@Autowired
167155
public BarProperties(String foo) {
168156

169157
}
170158

171159
}
172160

173-
@ConfigurationProperties(prefix = "baz")
174-
public static class BazProperties {
175-
176-
public BazProperties(String foo) {
177-
178-
}
179-
180-
}
181-
182161
@ConfigurationProperties(prefix = "bing")
183162
public static class BingProperties {
184163

spring-boot-project/spring-boot/src/test/kotlin/org/springframework/boot/context/properties/KotlinEnableConfigurationPropertiesImportSelectorTests.kt

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package org.springframework.boot.context.properties
22

33
import org.assertj.core.api.Assertions.assertThat
44
import org.junit.Test
5-
import org.springframework.beans.factory.annotation.Autowired
65
import org.springframework.beans.factory.support.DefaultListableBeanFactory
76
import org.springframework.beans.factory.support.GenericBeanDefinition
87
import org.springframework.core.type.AnnotationMetadata
@@ -29,21 +28,12 @@ class KotlinEnableConfigurationPropertiesImportSelectorTests {
2928
assertThat(beanDefinition).isExactlyInstanceOf(GenericBeanDefinition::class.java)
3029
}
3130

32-
@Test
33-
fun `type with autowired on constructor should register generic bean definition`() {
34-
this.registrar.registerBeanDefinitions(
35-
getAnnotationMetadata(TestConfiguration::class.java), this.beanFactory)
36-
val beanDefinition = this.beanFactory.getBeanDefinition(
37-
"bar-org.springframework.boot.context.properties.KotlinEnableConfigurationPropertiesImportSelectorTests\$BarProperties")
38-
assertThat(beanDefinition).isExactlyInstanceOf(GenericBeanDefinition::class.java)
39-
}
40-
4131
@Test
4232
fun `type with primary constructor and no autowired should register configuration properties bean definition`() {
4333
this.registrar.registerBeanDefinitions(
4434
getAnnotationMetadata(TestConfiguration::class.java), this.beanFactory)
4535
val beanDefinition = this.beanFactory.getBeanDefinition(
46-
"baz-org.springframework.boot.context.properties.KotlinEnableConfigurationPropertiesImportSelectorTests\$BazProperties")
36+
"bar-org.springframework.boot.context.properties.KotlinEnableConfigurationPropertiesImportSelectorTests\$BarProperties")
4737
assertThat(beanDefinition).isExactlyInstanceOf(
4838
ConfigurationPropertiesBeanDefinition::class.java)
4939
}
@@ -64,17 +54,14 @@ class KotlinEnableConfigurationPropertiesImportSelectorTests {
6454

6555

6656
@EnableConfigurationProperties(FooProperties::class, BarProperties::class,
67-
BazProperties::class, BingProperties::class)
57+
BingProperties::class)
6858
class TestConfiguration
6959

7060
@ConfigurationProperties(prefix = "foo")
7161
class FooProperties
7262

7363
@ConfigurationProperties(prefix = "bar")
74-
class BarProperties @Autowired constructor(val foo: String)
75-
76-
@ConfigurationProperties(prefix = "baz")
77-
class BazProperties(val name: String?, val counter: Int = 42)
64+
class BarProperties(val name: String?, val counter: Int = 42)
7865

7966
@ConfigurationProperties(prefix = "bing")
8067
class BingProperties {

0 commit comments

Comments
 (0)