-
Notifications
You must be signed in to change notification settings - Fork 41k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The getter and setter that's used during configuration property binding varies when a getter or setter has been overridden to use a subclass of the property's type #28917
Comments
@snicoll, this issue is referring to code in Spring Boot's JavaBeanBinder. Can you provide details on why you think this should be addressed in Framework? |
Sorry, that's my fault. I got my binder classes mixed up. We'll transfer it back. |
@jiangbingi Thanks for the report. Unfortunately, I don't think I understand the problem that you've described. Here is an example of a sub-class overriding a property: package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import com.example.demo.Gh28917Application.ExtensionProperties;
@SpringBootApplication
@EnableConfigurationProperties(ExtensionProperties.class)
public class Gh28917Application {
public static void main(String[] args) {
SpringApplication.run(Gh28917Application.class, "--extension.example=test");
}
static class BaseProperties {
private String example;
public String getExample() {
return example;
}
public void setExample(String example) {
System.out.println("Base = " + example);
this.example = example;
}
}
@ConfigurationProperties("extension")
static class ExtensionProperties extends BaseProperties {
public void setExample(String example) {
System.out.println("Extension = " + example);
}
}
} Running the above outputs Could you please provide a similar example that reproduces the problem you are seeing? |
package test;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@EnableConfigurationProperties(TestApplication.ExtensionProperties.class)
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, "--extension.field.group=my-group", "--extension.field.tag=my-tag");
}
static class BaseProperties {
private BaseField field;
public BaseField getField() {
return field;
}
public void setField(BaseField field) {
this.field = field;
}
static class BaseField {
private String group;
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
}
}
@ConfigurationProperties(prefix = "extension")
static class ExtensionProperties extends BaseProperties implements InitializingBean {
private BaseFieldExt field;
@Override
public BaseFieldExt getField() {
return field;
}
public void setField(BaseFieldExt field) {
this.field = field;
}
static class BaseFieldExt extends BaseField {
private String tag;
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
}
@Override
public void afterPropertiesSet() throws Exception {
if (field != null) {
System.out.println("bind ExtensionProperties success:" + field.getGroup() + "," + field.getTag());
} else if (super.getField() != null) {
System.out.println("bind ExtensionProperties fail. super property is not null:" + super.getField().getGroup());
}
}
}
} |
The binder is calling public void setField(BaseFieldExt field) {
this.field = field;
super.setField(field);
} |
@philwebb The reality is that ExtensionProperties.field has not been set.
|
The behaviour appears to be inconsistent. On repeated runs, sometimes the success message is output and sometimes the fail message is output:
|
The sorting of the declared methods is the problem. Methods are sorted by name and there are two methods named |
consider the better getter is determined by the type of field ? like this :
And it's possible to add field when adding method. |
In the example above, calling
When the superclass
When walking all the methods of the class and superclass, there are three methods named We should be able to exclude bridge methods from consideration when choosing getters and setters to enable use cases like this. |
Version: Spring Boot 2.5.7
Add BeanProperty from sub class first ,then ignore exist BeanProperty in super class (only use single parameter in
BiConsumer
),and finally bind to the properties of the super class, resulting in the loss of the subclass property extension.The text was updated successfully, but these errors were encountered: