Skip to content

Commit 6ecd3ae

Browse files
committed
Make cloneConfigurationPropertiesBean generic
Change cloneConfigurationPropertiesBean to return a typed instance (<T> T) and add the corresponding @param <T> javadoc; cast the autowired bean to T. Add unit test testCloneConfigurationPropertiesBean with Test1Properties and Test2Properties to verify constructor autowiring of Environment and ConfigurableApplicationContext, and add required imports/assertions.
1 parent 32c177e commit 6ecd3ae

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,17 @@ public ConfigurationPropertiesBeanContext(Class<?> beanClass, ConfigurationPrope
9292
*
9393
* @param beanClass the bean class
9494
* @param context {@link ConfigurableApplicationContext}
95+
* @param <T> the type of the bean
9596
* @return the cloned bean
9697
*/
9798
@Nonnull
98-
protected Object cloneConfigurationPropertiesBean(Class<?> beanClass, ConfigurableApplicationContext context) {
99+
protected <T> T cloneConfigurationPropertiesBean(Class<T> beanClass, ConfigurableApplicationContext context) {
99100
if (hasNonPrivateConstructorWithoutParameters(beanClass)) {
100101
return instantiateClass(beanClass);
101102
}
102103
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
103104
Object clonedBean = beanFactory.autowire(beanClass, AUTOWIRE_CONSTRUCTOR, true);
104-
return clonedBean;
105+
return (T) clonedBean;
105106
}
106107

107108
/**

microsphere-spring-boot-core/src/test/java/io/microsphere/spring/boot/context/properties/bind/ConfigurationPropertiesBeanContextTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,20 @@
2323
import org.springframework.beans.factory.BeanNameAware;
2424
import org.springframework.boot.autoconfigure.web.ServerProperties;
2525
import org.springframework.boot.context.properties.source.ConfigurationProperty;
26+
import org.springframework.context.ConfigurableApplicationContext;
2627
import org.springframework.context.support.GenericApplicationContext;
28+
import org.springframework.core.env.Environment;
2729

2830
import java.beans.PropertyDescriptor;
2931
import java.lang.reflect.Method;
3032

3133
import static io.microsphere.spring.boot.context.properties.bind.ConfigurationPropertiesBeanContext.isCandidateClass;
3234
import static io.microsphere.spring.boot.context.properties.bind.ConfigurationPropertiesBeanContext.isCandidateProperty;
35+
import static io.microsphere.spring.test.util.SpringTestUtils.testInSpringContainer;
3336
import static java.lang.Integer.valueOf;
3437
import static org.junit.jupiter.api.Assertions.assertEquals;
3538
import static org.junit.jupiter.api.Assertions.assertFalse;
39+
import static org.junit.jupiter.api.Assertions.assertNotNull;
3640
import static org.junit.jupiter.api.Assertions.assertNull;
3741
import static org.junit.jupiter.api.Assertions.assertSame;
3842
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -58,6 +62,21 @@ void setUp() {
5862
beanInfo.getPrefix(), context);
5963
}
6064

65+
@Test
66+
void testCloneConfigurationPropertiesBean() {
67+
testInSpringContainer(context -> {
68+
ServerProperties serverProperties = this.beanContext.cloneConfigurationPropertiesBean(ServerProperties.class, context);
69+
assertNotNull(serverProperties);
70+
71+
Test1Properties test1Properties = this.beanContext.cloneConfigurationPropertiesBean(Test1Properties.class, context);
72+
assertSame(context.getEnvironment(), test1Properties.environment);
73+
74+
Test2Properties test2Properties = this.beanContext.cloneConfigurationPropertiesBean(Test2Properties.class, context);
75+
assertSame(context.getEnvironment(), test2Properties.environment);
76+
assertSame(context, test2Properties.context);
77+
});
78+
}
79+
6180
@Test
6281
void testInitialize() {
6382
ServerProperties serverProperties = new ServerProperties();
@@ -112,4 +131,25 @@ void testIsCandidateClass() {
112131
assertTrue(isCandidateClass(ServerProperties.class));
113132
assertFalse(isCandidateClass(String.class));
114133
}
134+
135+
static class Test1Properties {
136+
137+
private Environment environment;
138+
139+
public Test1Properties(Environment environment) {
140+
this.environment = environment;
141+
}
142+
}
143+
144+
static class Test2Properties {
145+
146+
private Environment environment;
147+
148+
private ConfigurableApplicationContext context;
149+
150+
public Test2Properties(Environment environment, ConfigurableApplicationContext context) {
151+
this.environment = environment;
152+
this.context = context;
153+
}
154+
}
115155
}

0 commit comments

Comments
 (0)