Allow bean factory initialization to have a more flexible signature

This commit allows bean factory initialization to use a more flexible
signature than just consuming the DefaultListableBeanFactory. The
environment and the resource loader can now be specified if necessary.

See gh-29005
This commit is contained in:
Stephane Nicoll
2022-09-07 16:04:29 +02:00
parent ae706f3954
commit 2b45fd4388
3 changed files with 125 additions and 7 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.context.aot;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import javax.lang.model.element.Modifier;
@@ -27,15 +28,22 @@ import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.generate.MethodReference;
import org.springframework.aot.generate.MethodReference.ArgumentCodeGenerator;
import org.springframework.beans.factory.aot.BeanFactoryInitializationCode;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.javapoet.ClassName;
import org.springframework.javapoet.CodeBlock;
import org.springframework.javapoet.MethodSpec;
import org.springframework.javapoet.ParameterizedTypeName;
import org.springframework.javapoet.TypeName;
import org.springframework.javapoet.TypeSpec;
import org.springframework.lang.Nullable;
/**
* Internal code generator to create the {@link ApplicationContextInitializer}.
@@ -89,15 +97,15 @@ class ApplicationContextInitializationCodeGenerator implements BeanFactoryInitia
BEAN_FACTORY_VARIABLE, ContextAnnotationAutowireCandidateResolver.class);
code.addStatement("$L.setDependencyComparator($T.INSTANCE)",
BEAN_FACTORY_VARIABLE, AnnotationAwareOrderComparator.class);
ArgumentCodeGenerator argCodeGenerator = createInitializerMethodsArgumentCodeGenerator();
ArgumentCodeGenerator argCodeGenerator = createInitializerMethodArgumentCodeGenerator();
for (MethodReference initializer : this.initializers) {
code.addStatement(initializer.toInvokeCodeBlock(argCodeGenerator, this.generatedClass.getName()));
}
return code.build();
}
private ArgumentCodeGenerator createInitializerMethodsArgumentCodeGenerator() {
return ArgumentCodeGenerator.of(DefaultListableBeanFactory.class, BEAN_FACTORY_VARIABLE);
static ArgumentCodeGenerator createInitializerMethodArgumentCodeGenerator() {
return ArgumentCodeGenerator.from(new InitializerMethodArgumentCodeGenerator());
}
GeneratedClass getGeneratedClass() {
@@ -114,4 +122,30 @@ class ApplicationContextInitializationCodeGenerator implements BeanFactoryInitia
this.initializers.add(methodReference);
}
private static class InitializerMethodArgumentCodeGenerator implements Function<TypeName, CodeBlock> {
@Override
@Nullable
public CodeBlock apply(TypeName typeName) {
return (typeName instanceof ClassName className ? apply(className) : null);
}
@Nullable
private CodeBlock apply(ClassName className) {
String name = className.canonicalName();
if (name.equals(DefaultListableBeanFactory.class.getName())
|| name.equals(ConfigurableListableBeanFactory.class.getName())) {
return CodeBlock.of(BEAN_FACTORY_VARIABLE);
}
else if (name.equals(ConfigurableEnvironment.class.getName())
|| name.equals(Environment.class.getName())) {
return CodeBlock.of("$L.getConfigurableEnvironment()", APPLICATION_CONTEXT_VARIABLE);
}
else if (name.equals(ResourceLoader.class.getName())) {
return CodeBlock.of(APPLICATION_CONTEXT_VARIABLE);
}
return null;
}
}
}