mirror of
https://github.com/spring-projects/spring-framework
synced 2026-06-08 17:33:33 +00:00
Support RuntimeBeanReference(name, type) in AOT-generated code
The RuntimeBeanReference(name, type) constructor was introduced in 7.0; however, BeanDefinitionPropertyValueCodeGeneratorDelegates in our AOT infrastructure was not updated to support the new constructor. This commit revises BeanDefinitionPropertyValueCodeGeneratorDelegates to ensure that AOT generated code for a RuntimeBeanReference uses the RuntimeBeanReference(name, type) constructor, thereby retaining the originally configured bean name. See gh-35101 Closes gh-35913
This commit is contained in:
+2
-2
@@ -203,8 +203,8 @@ public abstract class BeanDefinitionPropertyValueCodeGeneratorDelegates {
|
||||
public @Nullable CodeBlock generateCode(ValueCodeGenerator valueCodeGenerator, Object value) {
|
||||
if (value instanceof RuntimeBeanReference runtimeBeanReference &&
|
||||
runtimeBeanReference.getBeanType() != null) {
|
||||
return CodeBlock.of("new $T($T.class)", RuntimeBeanReference.class,
|
||||
runtimeBeanReference.getBeanType());
|
||||
return CodeBlock.of("new $T($S, $T.class)", RuntimeBeanReference.class,
|
||||
runtimeBeanReference.getBeanName(), runtimeBeanReference.getBeanType());
|
||||
}
|
||||
else if (value instanceof BeanReference beanReference) {
|
||||
return CodeBlock.of("new $T($S)", RuntimeBeanReference.class,
|
||||
|
||||
+21
-9
@@ -458,30 +458,42 @@ class BeanDefinitionPropertyValueCodeGeneratorDelegatesTests {
|
||||
class BeanReferenceTests {
|
||||
|
||||
@Test
|
||||
void generatedWhenBeanNameReference() {
|
||||
RuntimeBeanNameReference beanReference = new RuntimeBeanNameReference("test");
|
||||
void generatedWhenRuntimeBeanNameReference() {
|
||||
BeanReference beanReference = new RuntimeBeanNameReference("test");
|
||||
compile(beanReference, (instance, compiler) -> {
|
||||
RuntimeBeanReference actual = (RuntimeBeanReference) instance;
|
||||
assertThat(actual.getBeanName()).isEqualTo(beanReference.getBeanName());
|
||||
assertThat(actual.getBeanName()).as("name").isEqualTo("test");
|
||||
assertThat(actual.getBeanType()).as("type").isNull();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void generatedWhenBeanReferenceByName() {
|
||||
RuntimeBeanReference beanReference = new RuntimeBeanReference("test");
|
||||
void generatedWhenRuntimeBeanReferenceByName() {
|
||||
BeanReference beanReference = new RuntimeBeanReference("test");
|
||||
compile(beanReference, (instance, compiler) -> {
|
||||
RuntimeBeanReference actual = (RuntimeBeanReference) instance;
|
||||
assertThat(actual.getBeanName()).isEqualTo(beanReference.getBeanName());
|
||||
assertThat(actual.getBeanType()).isEqualTo(beanReference.getBeanType());
|
||||
assertThat(actual.getBeanName()).as("name").isEqualTo("test");
|
||||
assertThat(actual.getBeanType()).as("type").isNull();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void generatedWhenBeanReferenceByType() {
|
||||
void generatedWhenRuntimeBeanReferenceByType() {
|
||||
BeanReference beanReference = new RuntimeBeanReference(String.class);
|
||||
compile(beanReference, (instance, compiler) -> {
|
||||
RuntimeBeanReference actual = (RuntimeBeanReference) instance;
|
||||
assertThat(actual.getBeanType()).isEqualTo(String.class);
|
||||
assertThat(actual.getBeanName()).as("name").isEqualTo(String.class.getName());
|
||||
assertThat(actual.getBeanType()).as("type").isEqualTo(String.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Test // gh-35913
|
||||
void generatedWhenRuntimeBeanReferenceByNameAndType() {
|
||||
BeanReference beanReference = new RuntimeBeanReference("test", String.class);
|
||||
compile(beanReference, (instance, compiler) -> {
|
||||
RuntimeBeanReference actual = (RuntimeBeanReference) instance;
|
||||
assertThat(actual.getBeanName()).as("name").isEqualTo("test");
|
||||
assertThat(actual.getBeanType()).as("type").isEqualTo(String.class);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user