From de21d71e20001e5496c9b5c03c8b9134fdb1d157 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 14 Mar 2019 11:16:22 +0100 Subject: [PATCH] Remove support of `@Autowired` for configuration properties bean See gh-8762 --- ...ConfigurationPropertiesImportSelector.java | 6 ----- .../ConfigurationPropertiesTests.java | 6 +++-- ...gurationPropertiesImportSelectorTests.java | 25 ++----------------- ...figurationPropertiesImportSelectorTests.kt | 19 +++----------- 4 files changed, 9 insertions(+), 47 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesImportSelector.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesImportSelector.java index 0cbe162d2b..12f88c162d 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesImportSelector.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesImportSelector.java @@ -25,7 +25,6 @@ import java.util.stream.Collectors; import org.springframework.beans.BeanUtils; 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.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.BeanDefinitionRegistry; @@ -151,11 +150,6 @@ class EnableConfigurationPropertiesImportSelector implements ImportSelector { private boolean canBindAtCreationTime(Class type) { List> constructors = determineConstructors(type); - boolean autowiredPresent = constructors.stream().anyMatch( - (c) -> AnnotationUtils.findAnnotation(c, Autowired.class) != null); - if (autowiredPresent) { - return false; - } return (constructors.size() == 1 && constructors.get(0).getParameterCount() > 0); } 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 5f454bd4c5..2374e493ee 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 @@ -808,7 +808,10 @@ public class ConfigurationPropertiesTests { @Test 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 @@ -1825,7 +1828,6 @@ public class ConfigurationPropertiesTests { final DataSizeProperties dataSizeProperties; - @Autowired OtherInjectedProperties(ObjectProvider dataSizeProperties) { this.dataSizeProperties = dataSizeProperties.getIfUnique(); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesImportSelectorTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesImportSelectorTests.java index 1320bcacfd..7ed4c76c14 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesImportSelectorTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/EnableConfigurationPropertiesImportSelectorTests.java @@ -19,7 +19,6 @@ import java.io.IOException; import org.junit.Test; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.GenericBeanDefinition; @@ -70,23 +69,13 @@ public class EnableConfigurationPropertiesImportSelectorTests { 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 public void typeWithOneConstructorWithParametersShouldRegisterConfigurationPropertiesBeanDefinition() throws Exception { this.registrar.registerBeanDefinitions( getAnnotationMetadata(TestConfiguration.class), this.beanFactory); BeanDefinition beanDefinition = this.beanFactory.getBeanDefinition( - "baz-org.springframework.boot.context.properties.EnableConfigurationPropertiesImportSelectorTests$BazProperties"); + "bar-org.springframework.boot.context.properties.EnableConfigurationPropertiesImportSelectorTests$BarProperties"); assertThat(beanDefinition) .isExactlyInstanceOf(ConfigurationPropertiesBeanDefinition.class); } @@ -135,7 +124,7 @@ public class EnableConfigurationPropertiesImportSelectorTests { } @EnableConfigurationProperties({ FooProperties.class, BarProperties.class, - BazProperties.class, BingProperties.class }) + BingProperties.class }) static class TestConfiguration { } @@ -163,22 +152,12 @@ public class EnableConfigurationPropertiesImportSelectorTests { @ConfigurationProperties(prefix = "bar") public static class BarProperties { - @Autowired public BarProperties(String foo) { } } - @ConfigurationProperties(prefix = "baz") - public static class BazProperties { - - public BazProperties(String foo) { - - } - - } - @ConfigurationProperties(prefix = "bing") public static class BingProperties { diff --git a/spring-boot-project/spring-boot/src/test/kotlin/org/springframework/boot/context/properties/KotlinEnableConfigurationPropertiesImportSelectorTests.kt b/spring-boot-project/spring-boot/src/test/kotlin/org/springframework/boot/context/properties/KotlinEnableConfigurationPropertiesImportSelectorTests.kt index 32a7e07f67..765089e7f6 100644 --- a/spring-boot-project/spring-boot/src/test/kotlin/org/springframework/boot/context/properties/KotlinEnableConfigurationPropertiesImportSelectorTests.kt +++ b/spring-boot-project/spring-boot/src/test/kotlin/org/springframework/boot/context/properties/KotlinEnableConfigurationPropertiesImportSelectorTests.kt @@ -2,7 +2,6 @@ package org.springframework.boot.context.properties import org.assertj.core.api.Assertions.assertThat import org.junit.Test -import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.support.DefaultListableBeanFactory import org.springframework.beans.factory.support.GenericBeanDefinition import org.springframework.core.type.AnnotationMetadata @@ -29,21 +28,12 @@ class KotlinEnableConfigurationPropertiesImportSelectorTests { 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 fun `type with primary constructor and no autowired should register configuration properties bean definition`() { this.registrar.registerBeanDefinitions( getAnnotationMetadata(TestConfiguration::class.java), this.beanFactory) val beanDefinition = this.beanFactory.getBeanDefinition( - "baz-org.springframework.boot.context.properties.KotlinEnableConfigurationPropertiesImportSelectorTests\$BazProperties") + "bar-org.springframework.boot.context.properties.KotlinEnableConfigurationPropertiesImportSelectorTests\$BarProperties") assertThat(beanDefinition).isExactlyInstanceOf( ConfigurationPropertiesBeanDefinition::class.java) } @@ -64,17 +54,14 @@ class KotlinEnableConfigurationPropertiesImportSelectorTests { @EnableConfigurationProperties(FooProperties::class, BarProperties::class, - BazProperties::class, BingProperties::class) + BingProperties::class) class TestConfiguration @ConfigurationProperties(prefix = "foo") class FooProperties @ConfigurationProperties(prefix = "bar") - class BarProperties @Autowired constructor(val foo: String) - - @ConfigurationProperties(prefix = "baz") - class BazProperties(val name: String?, val counter: Int = 42) + class BarProperties(val name: String?, val counter: Int = 42) @ConfigurationProperties(prefix = "bing") class BingProperties {