|
16 | 16 |
|
17 | 17 | package org.springframework.boot.context.properties;
|
18 | 18 |
|
19 |
| -import java.util.Collections; |
| 19 | +import java.util.ArrayList; |
20 | 20 | import java.util.List;
|
21 | 21 |
|
| 22 | +import org.springframework.beans.factory.BeanFactory; |
| 23 | +import org.springframework.beans.factory.ListableBeanFactory; |
22 | 24 | import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
23 |
| -import org.springframework.beans.factory.annotation.Autowired; |
| 25 | +import org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils; |
24 | 26 | import org.springframework.boot.convert.ApplicationConversionService;
|
25 | 27 | import org.springframework.context.ApplicationContext;
|
26 | 28 | import org.springframework.context.ConfigurableApplicationContext;
|
@@ -49,37 +51,43 @@ public ConversionService getConversionService() {
|
49 | 51 | ConversionService.class);
|
50 | 52 | }
|
51 | 53 | catch (NoSuchBeanDefinitionException ex) {
|
52 |
| - return this.applicationContext.getAutowireCapableBeanFactory() |
53 |
| - .createBean(Factory.class).create(); |
| 54 | + return new Factory(this.applicationContext.getAutowireCapableBeanFactory()) |
| 55 | + .create(); |
54 | 56 | }
|
55 | 57 | }
|
56 | 58 |
|
57 | 59 | private static class Factory {
|
58 | 60 |
|
59 |
| - private List<Converter<?, ?>> converters = Collections.emptyList(); |
60 |
| - |
61 |
| - private List<GenericConverter> genericConverters = Collections.emptyList(); |
62 |
| - |
63 | 61 | /**
|
64 | 62 | * A list of custom converters (in addition to the defaults) to use when
|
65 | 63 | * converting properties for binding.
|
66 |
| - * @param converters the converters to set |
67 | 64 | */
|
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; |
73 | 67 |
|
74 | 68 | /**
|
75 | 69 | * A list of custom converters (in addition to the defaults) to use when
|
76 | 70 | * converting properties for binding.
|
77 |
| - * @param converters the converters to set |
78 | 71 | */
|
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; |
83 | 91 | }
|
84 | 92 |
|
85 | 93 | public ConversionService create() {
|
|
0 commit comments