Use non-reflective APIs to retrieve config prop binding converters

See gh-14657
pull/14816/merge
Dave Syer 6 years ago committed by Andy Wilkinson
parent a0bbb98b51
commit 2094e54ef2

@ -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 <code>@Qualifier</code>.
*/
String VALUE = "org.springframework.boot.context.properties.ConfigurationPropertiesBinding";
}

@ -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<Converter<?, ?>> converters = Collections.emptyList();
private List<GenericConverter> 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<Converter<?, ?>> converters) {
this.converters = converters;
}
@SuppressWarnings("rawtypes")
private List<Converter> 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<GenericConverter> converters) {
this.genericConverters = converters;
private List<GenericConverter> genericConverters;
Factory(BeanFactory beanFactory) {
this.converters = beans(beanFactory, Converter.class,
ConfigurationPropertiesBinding.VALUE);
this.genericConverters = beans(beanFactory, GenericConverter.class,
ConfigurationPropertiesBinding.VALUE);
}
private static <T> List<T> beans(BeanFactory beanFactory, Class<T> type,
String qualifier) {
List<T> 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() {

Loading…
Cancel
Save