Skip to content

Commit cc530d4

Browse files
committed
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
1 parent 6c3132c commit cc530d4

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionPropertyValueCodeGeneratorDelegates.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ private static class BeanReferenceDelegate implements Delegate {
203203
public @Nullable CodeBlock generateCode(ValueCodeGenerator valueCodeGenerator, Object value) {
204204
if (value instanceof RuntimeBeanReference runtimeBeanReference &&
205205
runtimeBeanReference.getBeanType() != null) {
206-
return CodeBlock.of("new $T($T.class)", RuntimeBeanReference.class,
207-
runtimeBeanReference.getBeanType());
206+
return CodeBlock.of("new $T($S, $T.class)", RuntimeBeanReference.class,
207+
runtimeBeanReference.getBeanName(), runtimeBeanReference.getBeanType());
208208
}
209209
else if (value instanceof BeanReference beanReference) {
210210
return CodeBlock.of("new $T($S)", RuntimeBeanReference.class,

spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanDefinitionPropertyValueCodeGeneratorDelegatesTests.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -458,30 +458,42 @@ void generateWhenLinkedHashMap() {
458458
class BeanReferenceTests {
459459

460460
@Test
461-
void generatedWhenBeanNameReference() {
462-
RuntimeBeanNameReference beanReference = new RuntimeBeanNameReference("test");
461+
void generatedWhenRuntimeBeanNameReference() {
462+
BeanReference beanReference = new RuntimeBeanNameReference("test");
463463
compile(beanReference, (instance, compiler) -> {
464464
RuntimeBeanReference actual = (RuntimeBeanReference) instance;
465-
assertThat(actual.getBeanName()).isEqualTo(beanReference.getBeanName());
465+
assertThat(actual.getBeanName()).as("name").isEqualTo("test");
466+
assertThat(actual.getBeanType()).as("type").isNull();
466467
});
467468
}
468469

469470
@Test
470-
void generatedWhenBeanReferenceByName() {
471-
RuntimeBeanReference beanReference = new RuntimeBeanReference("test");
471+
void generatedWhenRuntimeBeanReferenceByName() {
472+
BeanReference beanReference = new RuntimeBeanReference("test");
472473
compile(beanReference, (instance, compiler) -> {
473474
RuntimeBeanReference actual = (RuntimeBeanReference) instance;
474-
assertThat(actual.getBeanName()).isEqualTo(beanReference.getBeanName());
475-
assertThat(actual.getBeanType()).isEqualTo(beanReference.getBeanType());
475+
assertThat(actual.getBeanName()).as("name").isEqualTo("test");
476+
assertThat(actual.getBeanType()).as("type").isNull();
476477
});
477478
}
478479

479480
@Test
480-
void generatedWhenBeanReferenceByType() {
481+
void generatedWhenRuntimeBeanReferenceByType() {
481482
BeanReference beanReference = new RuntimeBeanReference(String.class);
482483
compile(beanReference, (instance, compiler) -> {
483484
RuntimeBeanReference actual = (RuntimeBeanReference) instance;
484-
assertThat(actual.getBeanType()).isEqualTo(String.class);
485+
assertThat(actual.getBeanName()).as("name").isEqualTo(String.class.getName());
486+
assertThat(actual.getBeanType()).as("type").isEqualTo(String.class);
487+
});
488+
}
489+
490+
@Test // gh-35913
491+
void generatedWhenRuntimeBeanReferenceByNameAndType() {
492+
BeanReference beanReference = new RuntimeBeanReference("test", String.class);
493+
compile(beanReference, (instance, compiler) -> {
494+
RuntimeBeanReference actual = (RuntimeBeanReference) instance;
495+
assertThat(actual.getBeanName()).as("name").isEqualTo("test");
496+
assertThat(actual.getBeanType()).as("type").isEqualTo(String.class);
485497
});
486498
}
487499

0 commit comments

Comments
 (0)