Add AOT support for by-type RuntimeBeanReference

Closes gh-28841
This commit is contained in:
Stephane Nicoll 2022-07-19 17:56:35 +02:00
parent cd4f4d978d
commit 1acb41ff43
2 changed files with 32 additions and 5 deletions

View File

@ -513,7 +513,12 @@ class BeanDefinitionPropertyValueCodeGenerator {
@Override
@Nullable
public CodeBlock generateCode(Object value, ResolvableType type) {
if (value instanceof BeanReference beanReference) {
if (value instanceof RuntimeBeanReference runtimeBeanReference
&& runtimeBeanReference.getBeanType() != null) {
return CodeBlock.of("new $T($T.class)", RuntimeBeanReference.class,
runtimeBeanReference.getBeanType());
}
else if (value instanceof BeanReference beanReference) {
return CodeBlock.of("new $T($S)", RuntimeBeanReference.class,
beanReference.getBeanName());
}

View File

@ -40,6 +40,7 @@ import org.springframework.aot.test.generator.compile.Compiled;
import org.springframework.aot.test.generator.compile.TestCompiler;
import org.springframework.beans.factory.config.BeanReference;
import org.springframework.beans.factory.config.RuntimeBeanNameReference;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.support.ManagedSet;
@ -465,10 +466,31 @@ class BeanDefinitionPropertyValueCodeGeneratorTests {
class BeanReferenceTests {
@Test
void generatedWhenBeanReference() {
BeanReference beanReference = new RuntimeBeanNameReference("test");
compile(beanReference, (instance, compiler) ->
assertThat(((BeanReference) instance).getBeanName()).isEqualTo(beanReference.getBeanName()));
void generatedWhenBeanNameReference() {
RuntimeBeanNameReference beanReference = new RuntimeBeanNameReference("test");
compile(beanReference, (instance, compiler) -> {
RuntimeBeanReference actual = (RuntimeBeanReference) instance;
assertThat(actual.getBeanName()).isEqualTo(beanReference.getBeanName());
});
}
@Test
void generatedWhenBeanReferenceByName() {
RuntimeBeanReference beanReference = new RuntimeBeanReference("test");
compile(beanReference, (instance, compiler) -> {
RuntimeBeanReference actual = (RuntimeBeanReference) instance;
assertThat(actual.getBeanName()).isEqualTo(beanReference.getBeanName());
assertThat(actual.getBeanType()).isEqualTo(beanReference.getBeanType());
});
}
@Test
void generatedWhenBeanReferenceByType() {
BeanReference beanReference = new RuntimeBeanReference(String.class);
compile(beanReference, (instance, compiler) -> {
RuntimeBeanReference actual = (RuntimeBeanReference) instance;
assertThat(actual.getBeanType()).isEqualTo(String.class);
});
}
}