-
Notifications
You must be signed in to change notification settings - Fork 38.2k
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
Use of @Value in compact constructor of a record should not register method injection #31433
Use of @Value in compact constructor of a record should not register method injection #31433
Conversation
Thanks for the PR but are you sure that the code you've shared above currently triggers the warning? Does it work at all? It fails for me as follows:
Turning a |
@snicoll I confirm that log is triggered.
|
Thanks. "logging a warning" broke my brain, I was looking for a warning, not an info message. The bits on testing and use of record remains though. |
@snicoll Sorry for the misunderstanding, it is an INFO log, but communicating a sort of warning, didn't know how to call it. It works well for me with the code I provided (+ basic Spring Boot project, latest, JDK 21 or 17). As for @Component
public class MyClass {
private String prop;
public MyClass(@Value("${myProp}") String prop) {
this.prop = prop;
}
// getter
} But you're kinda right. Now that I think about the use cases, it might not be that needed. If I have some service that needs a property value, and I use a record for it, I'd get an unwanted property accessor automatically, which is not great. I was thinking more like loading properties into a "configuration" carrying bean, but that's also possible with e.g.: @ConfigurationProperties(prefix = "config")
public record Config(String first, String second) {} which works well. |
Yeah, sorry. You're totally right. The |
@martin-lukas thanks very much for making your first contribution to Spring Framework. |
When I try to use
@Value
annotation a record property, I get a warning in the logs that autowiring should only be used on methods with parameters. What that seems to imply is that attaching@Value
on the property is actually attaching it on the generated methodproperty()
, which has no parameters.Example:
Output:
My fix is to just check first, if the bean is a record or not.