Skip to content

Commit 9e559e7

Browse files
chore: merge main into release [skip ci]
2 parents 52573ca + 32c177e commit 9e559e7

1 file changed

Lines changed: 26 additions & 4 deletions

File tree

microsphere-spring-boot-core/src/main/java/io/microsphere/spring/boot/context/properties/bind/ConfigurationPropertiesBeanContext.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.microsphere.annotation.Nullable;
2121
import io.microsphere.spring.core.convert.support.ConversionServiceResolver;
2222
import org.springframework.beans.BeanWrapperImpl;
23+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
2324
import org.springframework.boot.context.properties.ConfigurationProperties;
2425
import org.springframework.boot.context.properties.source.ConfigurationProperty;
2526
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
@@ -32,12 +33,15 @@
3233
import java.util.Objects;
3334

3435
import static io.microsphere.collection.MapUtils.newHashMap;
36+
import static io.microsphere.reflect.ConstructorUtils.hasNonPrivateConstructorWithoutParameters;
3537
import static io.microsphere.spring.boot.context.properties.source.util.ConfigurationPropertyUtils.toDashedForm;
3638
import static io.microsphere.util.Assert.assertNotBlank;
3739
import static io.microsphere.util.Assert.assertNotNull;
3840
import static io.microsphere.util.ClassUtils.isConcreteClass;
3941
import static org.springframework.beans.BeanUtils.copyProperties;
4042
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;
4145
import static org.springframework.util.ClassUtils.isPrimitiveOrWrapper;
4246

4347
/**
@@ -80,7 +84,24 @@ public ConfigurationPropertiesBeanContext(Class<?> beanClass, ConfigurationPrope
8084
this.annotation = annotation;
8185
this.prefix = prefix;
8286
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;
84105
}
85106

86107
/**
@@ -193,9 +214,10 @@ public Object getInitializedBean() {
193214
return this.initializedBeanWrapper.getWrappedInstance();
194215
}
195216

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);
199221
beanWrapper.setAutoGrowNestedPaths(true);
200222
beanWrapper.setConversionService(conversionService);
201223
return beanWrapper;

0 commit comments

Comments
 (0)