-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Support @Value
for record injection
#28774
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
Conversation
@Value
for record injection
This aims to fix #28770. |
@@ -1352,6 +1352,10 @@ protected BeanWrapper autowireConstructor( | |||
*/ | |||
@SuppressWarnings("deprecation") // for postProcessPropertyValues | |||
protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) { | |||
// record has not set methods | |||
if (mbd.getBeanClass().isRecord()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check is insufficient, since the bean definition may not have a "bean class".
In any case, I will address this after merging this PR.
So there is no need to modify this PR.
@@ -506,4 +516,8 @@ public TestBean testBean() throws IOException { | |||
} | |||
} | |||
|
|||
|
|||
@Component | |||
static record RecordBean(@Value("recordBeanName") String name) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To access the recordBeanName
system property, this would need to be changed to @Value("${recordBeanName}")
or @Value("#{systemProperties[recordBeanName]}")
.
I will address this after merging this PR, so there is no need to modify this PR.
void testValueInjectionWithRecord() { | ||
System.setProperty("recordBeanName", "recordBean"); | ||
GenericApplicationContext context = new AnnotationConfigApplicationContext(RecordBean.class); | ||
context.refresh(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI: a context cannot be refreshed multiple times.
This results in:
java.lang.IllegalStateException: GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once
I will address this after merging the PR.
Add
@Value
support for record injection by disablingpopulateBean()
for records, since records are immutable.Addresses #28770