|
20 | 20 | import io.microsphere.annotation.Nullable; |
21 | 21 | import io.microsphere.spring.core.convert.support.ConversionServiceResolver; |
22 | 22 | import org.springframework.beans.BeanWrapperImpl; |
| 23 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
23 | 24 | import org.springframework.boot.context.properties.ConfigurationProperties; |
24 | 25 | import org.springframework.boot.context.properties.source.ConfigurationProperty; |
25 | 26 | import org.springframework.boot.context.properties.source.ConfigurationPropertyName; |
|
32 | 33 | import java.util.Objects; |
33 | 34 |
|
34 | 35 | import static io.microsphere.collection.MapUtils.newHashMap; |
| 36 | +import static io.microsphere.reflect.ConstructorUtils.hasNonPrivateConstructorWithoutParameters; |
35 | 37 | import static io.microsphere.spring.boot.context.properties.source.util.ConfigurationPropertyUtils.toDashedForm; |
36 | 38 | import static io.microsphere.util.Assert.assertNotBlank; |
37 | 39 | import static io.microsphere.util.Assert.assertNotNull; |
38 | 40 | import static io.microsphere.util.ClassUtils.isConcreteClass; |
39 | 41 | import static org.springframework.beans.BeanUtils.copyProperties; |
40 | 42 | import static org.springframework.beans.BeanUtils.getPropertyDescriptors; |
| 43 | +import static org.springframework.beans.BeanUtils.instantiateClass; |
| 44 | +import static org.springframework.beans.factory.config.AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR; |
41 | 45 | import static org.springframework.util.ClassUtils.isPrimitiveOrWrapper; |
42 | 46 |
|
43 | 47 | /** |
@@ -80,7 +84,24 @@ public ConfigurationPropertiesBeanContext(Class<?> beanClass, ConfigurationPrope |
80 | 84 | this.annotation = annotation; |
81 | 85 | this.prefix = prefix; |
82 | 86 | this.context = context; |
83 | | - this.initializedBeanWrapper = createInitializedBeanWrapper(beanClass); |
| 87 | + this.initializedBeanWrapper = createInitializedBeanWrapper(beanClass, context); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Clone the {@link ConfigurationProperties @ConfigurationProperties} bean |
| 92 | + * |
| 93 | + * @param beanClass the bean class |
| 94 | + * @param context {@link ConfigurableApplicationContext} |
| 95 | + * @return the cloned bean |
| 96 | + */ |
| 97 | + @Nonnull |
| 98 | + protected Object cloneConfigurationPropertiesBean(Class<?> beanClass, ConfigurableApplicationContext context) { |
| 99 | + if (hasNonPrivateConstructorWithoutParameters(beanClass)) { |
| 100 | + return instantiateClass(beanClass); |
| 101 | + } |
| 102 | + ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); |
| 103 | + Object clonedBean = beanFactory.autowire(beanClass, AUTOWIRE_CONSTRUCTOR, true); |
| 104 | + return clonedBean; |
84 | 105 | } |
85 | 106 |
|
86 | 107 | /** |
@@ -193,9 +214,10 @@ public Object getInitializedBean() { |
193 | 214 | return this.initializedBeanWrapper.getWrappedInstance(); |
194 | 215 | } |
195 | 216 |
|
196 | | - private BeanWrapperImpl createInitializedBeanWrapper(Class<?> beanClass) { |
197 | | - BeanWrapperImpl beanWrapper = new BeanWrapperImpl(beanClass); |
198 | | - ConversionService conversionService = getConversionService(context); |
| 217 | + private BeanWrapperImpl createInitializedBeanWrapper(Class<?> beanClass, ConfigurableApplicationContext context) { |
| 218 | + Object clonedBean = cloneConfigurationPropertiesBean(beanClass, context); |
| 219 | + BeanWrapperImpl beanWrapper = new BeanWrapperImpl(clonedBean); |
| 220 | + ConversionService conversionService = getConversionService(this.context); |
199 | 221 | beanWrapper.setAutoGrowNestedPaths(true); |
200 | 222 | beanWrapper.setConversionService(conversionService); |
201 | 223 | return beanWrapper; |
|
0 commit comments