Allow ApplicationContextAotGenerator to generated better class names

Update `ApplicationContextAotGenerator` so that it can generate class
names based on a `target` class and using the ID of the application
context. Prior to this commit, the generated class name was always
`__.BeanFactoryRegistrations`.

Closes gh-28565
This commit is contained in:
Phillip Webb
2022-06-03 16:43:13 -07:00
parent 243350054b
commit 4bd33cb6e0
9 changed files with 99 additions and 132 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.beans.factory.aot;
import org.springframework.aot.generate.MethodGenerator;
import org.springframework.aot.generate.MethodReference;
import org.springframework.lang.Nullable;
/**
* Interface that can be used to configure the code that will be generated to
@@ -34,6 +35,24 @@ public interface BeanFactoryInitializationCode {
*/
String BEAN_FACTORY_VARIABLE = "beanFactory";
/**
* Return the target class for this bean factory or {@code null} if there is
* no target.
* @return the target
*/
@Nullable
default Class<?> getTarget() {
return null;
}
/**
* Return the ID of the bean factory or and empty string if no ID is avaialble.
* @return the bean factory ID
*/
default String getId() {
return "";
}
/**
* Return a {@link MethodGenerator} that can be used to add more methods to
* the Initializing code.

View File

@@ -61,8 +61,9 @@ class BeanRegistrationsAotContribution
public void applyTo(GenerationContext generationContext,
BeanFactoryInitializationCode beanFactoryInitializationCode) {
ClassName className = generationContext.getClassNameGenerator()
.generateClassName("BeanFactory", "Registrations");
ClassName className = generationContext.getClassNameGenerator().generateClassName(
beanFactoryInitializationCode.getTarget(),
beanFactoryInitializationCode.getId() + "BeanFactoryRegistrations");
BeanRegistrationsCodeGenerator codeGenerator = new BeanRegistrationsCodeGenerator(
className);
GeneratedMethod registerMethod = codeGenerator.getMethodGenerator()