diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBinding.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBinding.java index 1de279d321..3e2f555c7f 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBinding.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBinding.java @@ -30,10 +30,15 @@ import org.springframework.beans.factory.annotation.Qualifier; * * @author Dave Syer */ -@Qualifier +@Qualifier(ConfigurationPropertiesBinding.VALUE) @Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface ConfigurationPropertiesBinding { + /** + * Concrete value for the @Qualifier. + */ + String VALUE = "org.springframework.boot.context.properties.ConfigurationPropertiesBinding"; + } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConversionServiceDeducer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConversionServiceDeducer.java index 8af981e802..36dbf72118 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConversionServiceDeducer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConversionServiceDeducer.java @@ -16,11 +16,13 @@ package org.springframework.boot.context.properties; -import java.util.Collections; +import java.util.ArrayList; import java.util.List; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.beans.factory.NoSuchBeanDefinitionException; -import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils; import org.springframework.boot.convert.ApplicationConversionService; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; @@ -49,37 +51,43 @@ class ConversionServiceDeducer { ConversionService.class); } catch (NoSuchBeanDefinitionException ex) { - return this.applicationContext.getAutowireCapableBeanFactory() - .createBean(Factory.class).create(); + return new Factory(this.applicationContext.getAutowireCapableBeanFactory()) + .create(); } } private static class Factory { - private List> converters = Collections.emptyList(); - - private List genericConverters = Collections.emptyList(); - /** * A list of custom converters (in addition to the defaults) to use when * converting properties for binding. - * @param converters the converters to set */ - @Autowired(required = false) - @ConfigurationPropertiesBinding - public void setConverters(List> converters) { - this.converters = converters; - } + @SuppressWarnings("rawtypes") + private List converters; /** * A list of custom converters (in addition to the defaults) to use when * converting properties for binding. - * @param converters the converters to set */ - @Autowired(required = false) - @ConfigurationPropertiesBinding - public void setGenericConverters(List converters) { - this.genericConverters = converters; + private List genericConverters; + + Factory(BeanFactory beanFactory) { + this.converters = beans(beanFactory, Converter.class, + ConfigurationPropertiesBinding.VALUE); + this.genericConverters = beans(beanFactory, GenericConverter.class, + ConfigurationPropertiesBinding.VALUE); + } + + private static List beans(BeanFactory beanFactory, Class type, + String qualifier) { + List list = new ArrayList<>(); + if (!(beanFactory instanceof ListableBeanFactory)) { + return list; + } + ListableBeanFactory listable = (ListableBeanFactory) beanFactory; + list.addAll(BeanFactoryAnnotationUtils + .qualifiedBeansOfType(listable, type, qualifier).values()); + return list; } public ConversionService create() {