-
Notifications
You must be signed in to change notification settings - Fork 41.2k
Relocate @ConstructorBinding to bind package and update default behavior #32660
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
Comments
Binding to records is performed by the value object binder and should work – we have tests that verify that it does. Without a minimal sample and with only "does not work" as a description of the failure we aren't going to be able to help you. If you would like us to spend some more time investigating, please spend some time providing a complete yet minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue. |
You might also want to try the |
I am sorry, you are right, I should have provided an example to begin with. here is one. I am not sure if I am entirely doing things correctly... If I comment the constructor in
the test passes. |
I've also added one more example in that repo, with a nested record. This might need to be a separate issue, not sure. |
The problem in your sample is caused because you're trying to use the low-level The simplest fix for your sample is to inject the If you do want to use the Here's an updated @Component
public class SimpleService {
@Autowired
private ApplicationContext applicationContext;
@Autowired
private Environment environment;
public SimpleRecord simple() {
Iterable<ConfigurationPropertySource> configurationPropertySources = ConfigurationPropertySources
.from(((ConfigurableEnvironment) environment).getPropertySources());
PropertySourcesPlaceholdersResolver placeholdersResolver = new PropertySourcesPlaceholdersResolver(environment);
Consumer<PropertyEditorRegistry> propertyEditorInitializer = ((ConfigurableApplicationContext) applicationContext)
.getBeanFactory()::copyRegisteredEditorsTo;
BindConstructorProvider constructorProvider = (bindable, isNestedConstructorBinding) -> {
Class<?> type = bindable.getType().resolve(Object.class);
return type.isRecord() ? type.getConstructors()[0] : null;
};
Binder binder = new Binder(configurationPropertySources, placeholdersResolver,
ApplicationConversionService.getSharedInstance(), propertyEditorInitializer, null, constructorProvider);
return binder.bindOrCreate("retry", SimpleRecord.class);
}
} |
I'll take a look at the sample, thank you. The thing is we use this in spring-cloud-kubernetes (where I am a minor contributor), see here. So we do have boot-3... I do not want to sound impolite, but can you may be show how to do it (or a hint where to look) for spring-boot-3? I very much appreciate your effort here. thank you. |
Ohh, I see the
The Flagging this one for further team discussion as we need to decide if we want to support a way to use a custom |
you're right, this does work for a record with a single constructor, but we have a different problem other there. The record is nested inside
will correctly create the defaults and inject all the properties I have defined under While this:
will only apply defaults, without injecting any properties I have defined, for the Retry record. That btw is what I was trying to show in the sample I provided, with I hope I make sense here. P.S. I can workaround this, but it isn't elegant at all, imho. |
We spoke about this today and we're going to look to see if we can move |
The annotation processor is still looking for |
In
spring-cloud-kubernetes
we have support for configdata, as such, much of the underlying code uses snippets likes these:which works.
But I am trying to change some code to move configs to records, so I have code that does this, for example:
where
AbstractConfigProperties.RetryProperties
is arecord
. Unfortunately, this does not work and binding does not happen.My limited knowledge of Binder functionality does not allow me to figure out how to do it, and if it is possible at all. :( I can't provide a simple example here, but I could push the branch that I am currently working on and tell what test to run in order to reproduce this.
I am also not sure the name of the issue is appropriate, but after a few hours of debug I am inclined to think that
JavaBeanBinder
is the one responsible for this.Thank you for looking into this.
The text was updated successfully, but these errors were encountered: