Remove support of `@Autowired` for configuration properties bean

See gh-8762
pull/16235/head
Stephane Nicoll 6 years ago
parent fcdc414646
commit de21d71e20

@ -25,7 +25,6 @@ import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistry;
@ -151,11 +150,6 @@ class EnableConfigurationPropertiesImportSelector implements ImportSelector {
private boolean canBindAtCreationTime(Class<?> type) { private boolean canBindAtCreationTime(Class<?> type) {
List<Constructor<?>> constructors = determineConstructors(type); List<Constructor<?>> constructors = determineConstructors(type);
boolean autowiredPresent = constructors.stream().anyMatch(
(c) -> AnnotationUtils.findAnnotation(c, Autowired.class) != null);
if (autowiredPresent) {
return false;
}
return (constructors.size() == 1 return (constructors.size() == 1
&& constructors.get(0).getParameterCount() > 0); && constructors.get(0).getParameterCount() > 0);
} }

@ -808,7 +808,10 @@ public class ConfigurationPropertiesTests {
@Test @Test
public void loadWhenConfigurationPropertiesInjectsAnotherBeanShouldNotFail() { public void loadWhenConfigurationPropertiesInjectsAnotherBeanShouldNotFail() {
load(OtherInjectPropertiesConfiguration.class); assertThatExceptionOfType(ConfigurationPropertiesBindException.class)
.isThrownBy(() -> load(OtherInjectPropertiesConfiguration.class))
.withMessageContaining(OtherInjectedProperties.class.getName())
.withMessageContaining("Failed to bind properties under 'test'");
} }
@Test @Test
@ -1825,7 +1828,6 @@ public class ConfigurationPropertiesTests {
final DataSizeProperties dataSizeProperties; final DataSizeProperties dataSizeProperties;
@Autowired
OtherInjectedProperties(ObjectProvider<DataSizeProperties> dataSizeProperties) { OtherInjectedProperties(ObjectProvider<DataSizeProperties> dataSizeProperties) {
this.dataSizeProperties = dataSizeProperties.getIfUnique(); this.dataSizeProperties = dataSizeProperties.getIfUnique();
} }

@ -19,7 +19,6 @@ import java.io.IOException;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.beans.factory.support.GenericBeanDefinition;
@ -70,23 +69,13 @@ public class EnableConfigurationPropertiesImportSelectorTests {
assertThat(beanDefinition).isExactlyInstanceOf(GenericBeanDefinition.class); assertThat(beanDefinition).isExactlyInstanceOf(GenericBeanDefinition.class);
} }
@Test
public void typeWithAutowiredOnConstructorShouldRegisterGenericBeanDefinition()
throws Exception {
this.registrar.registerBeanDefinitions(
getAnnotationMetadata(TestConfiguration.class), this.beanFactory);
BeanDefinition beanDefinition = this.beanFactory.getBeanDefinition(
"bar-org.springframework.boot.context.properties.EnableConfigurationPropertiesImportSelectorTests$BarProperties");
assertThat(beanDefinition).isExactlyInstanceOf(GenericBeanDefinition.class);
}
@Test @Test
public void typeWithOneConstructorWithParametersShouldRegisterConfigurationPropertiesBeanDefinition() public void typeWithOneConstructorWithParametersShouldRegisterConfigurationPropertiesBeanDefinition()
throws Exception { throws Exception {
this.registrar.registerBeanDefinitions( this.registrar.registerBeanDefinitions(
getAnnotationMetadata(TestConfiguration.class), this.beanFactory); getAnnotationMetadata(TestConfiguration.class), this.beanFactory);
BeanDefinition beanDefinition = this.beanFactory.getBeanDefinition( BeanDefinition beanDefinition = this.beanFactory.getBeanDefinition(
"baz-org.springframework.boot.context.properties.EnableConfigurationPropertiesImportSelectorTests$BazProperties"); "bar-org.springframework.boot.context.properties.EnableConfigurationPropertiesImportSelectorTests$BarProperties");
assertThat(beanDefinition) assertThat(beanDefinition)
.isExactlyInstanceOf(ConfigurationPropertiesBeanDefinition.class); .isExactlyInstanceOf(ConfigurationPropertiesBeanDefinition.class);
} }
@ -135,7 +124,7 @@ public class EnableConfigurationPropertiesImportSelectorTests {
} }
@EnableConfigurationProperties({ FooProperties.class, BarProperties.class, @EnableConfigurationProperties({ FooProperties.class, BarProperties.class,
BazProperties.class, BingProperties.class }) BingProperties.class })
static class TestConfiguration { static class TestConfiguration {
} }
@ -163,22 +152,12 @@ public class EnableConfigurationPropertiesImportSelectorTests {
@ConfigurationProperties(prefix = "bar") @ConfigurationProperties(prefix = "bar")
public static class BarProperties { public static class BarProperties {
@Autowired
public BarProperties(String foo) { public BarProperties(String foo) {
} }
} }
@ConfigurationProperties(prefix = "baz")
public static class BazProperties {
public BazProperties(String foo) {
}
}
@ConfigurationProperties(prefix = "bing") @ConfigurationProperties(prefix = "bing")
public static class BingProperties { public static class BingProperties {

@ -2,7 +2,6 @@ package org.springframework.boot.context.properties
import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThat
import org.junit.Test import org.junit.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.support.DefaultListableBeanFactory import org.springframework.beans.factory.support.DefaultListableBeanFactory
import org.springframework.beans.factory.support.GenericBeanDefinition import org.springframework.beans.factory.support.GenericBeanDefinition
import org.springframework.core.type.AnnotationMetadata import org.springframework.core.type.AnnotationMetadata
@ -29,21 +28,12 @@ class KotlinEnableConfigurationPropertiesImportSelectorTests {
assertThat(beanDefinition).isExactlyInstanceOf(GenericBeanDefinition::class.java) assertThat(beanDefinition).isExactlyInstanceOf(GenericBeanDefinition::class.java)
} }
@Test
fun `type with autowired on constructor should register generic bean definition`() {
this.registrar.registerBeanDefinitions(
getAnnotationMetadata(TestConfiguration::class.java), this.beanFactory)
val beanDefinition = this.beanFactory.getBeanDefinition(
"bar-org.springframework.boot.context.properties.KotlinEnableConfigurationPropertiesImportSelectorTests\$BarProperties")
assertThat(beanDefinition).isExactlyInstanceOf(GenericBeanDefinition::class.java)
}
@Test @Test
fun `type with primary constructor and no autowired should register configuration properties bean definition`() { fun `type with primary constructor and no autowired should register configuration properties bean definition`() {
this.registrar.registerBeanDefinitions( this.registrar.registerBeanDefinitions(
getAnnotationMetadata(TestConfiguration::class.java), this.beanFactory) getAnnotationMetadata(TestConfiguration::class.java), this.beanFactory)
val beanDefinition = this.beanFactory.getBeanDefinition( val beanDefinition = this.beanFactory.getBeanDefinition(
"baz-org.springframework.boot.context.properties.KotlinEnableConfigurationPropertiesImportSelectorTests\$BazProperties") "bar-org.springframework.boot.context.properties.KotlinEnableConfigurationPropertiesImportSelectorTests\$BarProperties")
assertThat(beanDefinition).isExactlyInstanceOf( assertThat(beanDefinition).isExactlyInstanceOf(
ConfigurationPropertiesBeanDefinition::class.java) ConfigurationPropertiesBeanDefinition::class.java)
} }
@ -64,17 +54,14 @@ class KotlinEnableConfigurationPropertiesImportSelectorTests {
@EnableConfigurationProperties(FooProperties::class, BarProperties::class, @EnableConfigurationProperties(FooProperties::class, BarProperties::class,
BazProperties::class, BingProperties::class) BingProperties::class)
class TestConfiguration class TestConfiguration
@ConfigurationProperties(prefix = "foo") @ConfigurationProperties(prefix = "foo")
class FooProperties class FooProperties
@ConfigurationProperties(prefix = "bar") @ConfigurationProperties(prefix = "bar")
class BarProperties @Autowired constructor(val foo: String) class BarProperties(val name: String?, val counter: Int = 42)
@ConfigurationProperties(prefix = "baz")
class BazProperties(val name: String?, val counter: Int = 42)
@ConfigurationProperties(prefix = "bing") @ConfigurationProperties(prefix = "bing")
class BingProperties { class BingProperties {

Loading…
Cancel
Save