Skip to content

Commit 2094e54

Browse files
Dave Syerwilkinsona
Dave Syer
authored andcommitted
Use non-reflective APIs to retrieve config prop binding converters
See gh-14657
1 parent a0bbb98 commit 2094e54

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,15 @@
3030
*
3131
* @author Dave Syer
3232
*/
33-
@Qualifier
33+
@Qualifier(ConfigurationPropertiesBinding.VALUE)
3434
@Target({ ElementType.TYPE, ElementType.METHOD })
3535
@Retention(RetentionPolicy.RUNTIME)
3636
@Documented
3737
public @interface ConfigurationPropertiesBinding {
3838

39+
/**
40+
* Concrete value for the <code>@Qualifier</code>.
41+
*/
42+
String VALUE = "org.springframework.boot.context.properties.ConfigurationPropertiesBinding";
43+
3944
}

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

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616

1717
package org.springframework.boot.context.properties;
1818

19-
import java.util.Collections;
19+
import java.util.ArrayList;
2020
import java.util.List;
2121

22+
import org.springframework.beans.factory.BeanFactory;
23+
import org.springframework.beans.factory.ListableBeanFactory;
2224
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
23-
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils;
2426
import org.springframework.boot.convert.ApplicationConversionService;
2527
import org.springframework.context.ApplicationContext;
2628
import org.springframework.context.ConfigurableApplicationContext;
@@ -49,37 +51,43 @@ public ConversionService getConversionService() {
4951
ConversionService.class);
5052
}
5153
catch (NoSuchBeanDefinitionException ex) {
52-
return this.applicationContext.getAutowireCapableBeanFactory()
53-
.createBean(Factory.class).create();
54+
return new Factory(this.applicationContext.getAutowireCapableBeanFactory())
55+
.create();
5456
}
5557
}
5658

5759
private static class Factory {
5860

59-
private List<Converter<?, ?>> converters = Collections.emptyList();
60-
61-
private List<GenericConverter> genericConverters = Collections.emptyList();
62-
6361
/**
6462
* A list of custom converters (in addition to the defaults) to use when
6563
* converting properties for binding.
66-
* @param converters the converters to set
6764
*/
68-
@Autowired(required = false)
69-
@ConfigurationPropertiesBinding
70-
public void setConverters(List<Converter<?, ?>> converters) {
71-
this.converters = converters;
72-
}
65+
@SuppressWarnings("rawtypes")
66+
private List<Converter> converters;
7367

7468
/**
7569
* A list of custom converters (in addition to the defaults) to use when
7670
* converting properties for binding.
77-
* @param converters the converters to set
7871
*/
79-
@Autowired(required = false)
80-
@ConfigurationPropertiesBinding
81-
public void setGenericConverters(List<GenericConverter> converters) {
82-
this.genericConverters = converters;
72+
private List<GenericConverter> genericConverters;
73+
74+
Factory(BeanFactory beanFactory) {
75+
this.converters = beans(beanFactory, Converter.class,
76+
ConfigurationPropertiesBinding.VALUE);
77+
this.genericConverters = beans(beanFactory, GenericConverter.class,
78+
ConfigurationPropertiesBinding.VALUE);
79+
}
80+
81+
private static <T> List<T> beans(BeanFactory beanFactory, Class<T> type,
82+
String qualifier) {
83+
List<T> list = new ArrayList<>();
84+
if (!(beanFactory instanceof ListableBeanFactory)) {
85+
return list;
86+
}
87+
ListableBeanFactory listable = (ListableBeanFactory) beanFactory;
88+
list.addAll(BeanFactoryAnnotationUtils
89+
.qualifiedBeansOfType(listable, type, qualifier).values());
90+
return list;
8391
}
8492

8593
public ConversionService create() {

0 commit comments

Comments
 (0)