Skip to content

Commit 7e7b135

Browse files
committed
Fix constructor-bound properties rebinding
Adjust `ConfigurationPropertiesBeanContext` to respect constructor binding during property initialization and rebinding. Instead of recalculating a bind constructor per nested type, it now consistently uses the context-level constructor and re-initializes bean properties when constructor binding is active, avoiding incorrect per-property rebinding behavior. The related test class was also updated to consistently reference instance fields via `this` for clarity.
1 parent 845efae commit 7e7b135

2 files changed

Lines changed: 16 additions & 13 deletions

File tree

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ private void initBeanProperties() {
210210
private void initBeanProperties(ResolvableType beanType, ConfigurationPropertyName prefixName, String nestedPath) {
211211
Class<?> beanClass = beanType.getRawClass();
212212
if (isCandidateClass(beanClass)) {
213-
Constructor<?> bindConstructor = getBindConstructor(beanType);
213+
Constructor<?> bindConstructor = this.bindConstructor;
214214
if (bindConstructor == null) {
215215
PropertyDescriptor[] descriptors = getPropertyDescriptors(beanClass);
216216
initBeanProperties(beanType, descriptors, prefixName, nestedPath);
@@ -227,7 +227,11 @@ private void initBeanProperties(ResolvableType beanType, ConfigurationPropertyNa
227227
* @see Binder#bind(String, Bindable)
228228
*/
229229
void bindPropertyValues() {
230-
this.beanProperties.values().forEach(this::bindPropertyValue);
230+
if (this.bindConstructor == null) {
231+
this.beanProperties.values().forEach(this::bindPropertyValue);
232+
} else {
233+
initBeanProperties();
234+
}
231235
}
232236

233237
boolean bindPropertyValue(ConfigurationPropertiesBeanProperty beanProperty) {
@@ -519,8 +523,7 @@ static Object clone(Object value) {
519523
return value;
520524
}
521525

522-
static Map<String, ConfigurationPropertiesBeanContext> buildConfigurationPropertiesBeanContexts
523-
(ConfigurableApplicationContext context) {
526+
static Map<String, ConfigurationPropertiesBeanContext> buildConfigurationPropertiesBeanContexts(ConfigurableApplicationContext context) {
524527
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
525528
String[] beanDefinitionNames = beanFactory.getBeanDefinitionNames();
526529
Map<String, ConfigurationPropertiesBeanContext> beanContexts = newHashMap();

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,16 @@ void testWebProperties(int index) {
153153
}
154154

155155
// assert the configured values
156-
assertEquals(US, webProperties.getLocale());
157-
assertEquals(FIXED, webProperties.getLocaleResolver());
156+
assertEquals(US, this.webProperties.getLocale());
157+
assertEquals(FIXED, this.webProperties.getLocaleResolver());
158158

159-
WebProperties.Resources resources = webProperties.getResources();
159+
WebProperties.Resources resources = this.webProperties.getResources();
160160
String[] staticLocations = resources.getStaticLocations();
161161
assertArrayEquals(ofArray("/static/", "/public/", "/resources/"), staticLocations);
162162

163163
this.eventHolder.reset();
164164

165-
setProperty("spring.web.locale", "zh_CN", webProperties);
165+
setProperty("spring.web.locale", "zh_CN", this.webProperties);
166166

167167
ConfigurationPropertiesBeanPropertyChangedEvent event = this.eventHolder.getValue();
168168
assertSame(this.webProperties, event.getSource());
@@ -180,14 +180,14 @@ void testTestConfigurationProperties(int index) {
180180
}
181181

182182
Map<String, String> properties = ofMap("key-1", "value-1", "key-2", "value-2", "key-3", "value-3");
183-
assertEquals("test-name", testConfigurationProperties.getName());
184-
assertEquals(properties, testConfigurationProperties.getProperties());
185-
assertArrayEquals(ofArray("a", "b", "c"), testConfigurationProperties.getAliases());
186-
assertEquals(ofList(7070, 8080, 9090), testConfigurationProperties.getPorts());
183+
assertEquals("test-name", this.testConfigurationProperties.getName());
184+
assertEquals(properties, this.testConfigurationProperties.getProperties());
185+
assertArrayEquals(ofArray("a", "b", "c"), this.testConfigurationProperties.getAliases());
186+
assertEquals(ofList(7070, 8080, 9090), this.testConfigurationProperties.getPorts());
187187

188188
this.eventHolder.reset();
189189

190-
setProperty("test.properties.key-1", "value-x", testConfigurationProperties);
190+
setProperty("test.properties.key-1", "value-x", this.testConfigurationProperties);
191191

192192
ConfigurationPropertiesBeanPropertyChangedEvent event = this.eventHolder.getValue();
193193
assertSame(this.testConfigurationProperties, event.getSource());

0 commit comments

Comments
 (0)