Support @Value for record injection
Add @Value support for record injection by disabling populateBean() for records, since records are immutable. See gh-28770 Closes gh-28774
This commit is contained in:
parent
c859211f7a
commit
7c9fc575ff
|
@ -1353,6 +1353,10 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
|
|||
* @param bw the BeanWrapper with bean instance
|
||||
*/
|
||||
protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) {
|
||||
// record has not set methods
|
||||
if (mbd.getBeanClass().isRecord()) {
|
||||
return;
|
||||
}
|
||||
if (bw == null) {
|
||||
if (mbd.hasPropertyValues()) {
|
||||
throw new BeanCreationException(
|
||||
|
|
|
@ -43,6 +43,7 @@ import org.springframework.context.support.GenericApplicationContext;
|
|||
import org.springframework.core.annotation.AliasFor;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.testfixture.stereotype.Component;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
@ -233,6 +234,15 @@ class AutowiredConfigurationTests {
|
|||
context.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testValueInjectionWithRecord() {
|
||||
System.setProperty("recordBeanName", "recordBean");
|
||||
GenericApplicationContext context = new AnnotationConfigApplicationContext(RecordBean.class);
|
||||
context.refresh();
|
||||
RecordBean recordBean = context.getBean(RecordBean.class);
|
||||
assertThat(recordBean.name()).isEqualTo("recordBean");
|
||||
}
|
||||
|
||||
private int contentLength() throws IOException {
|
||||
return (int) new ClassPathResource("do_not_delete_me.txt").contentLength();
|
||||
}
|
||||
|
@ -506,4 +516,8 @@ class AutowiredConfigurationTests {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
@Component
|
||||
static record RecordBean(@Value("recordBeanName") String name) {
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue