Skip to content

Commit fe39598

Browse files
committed
Adapt to latest API change in Spring Framework
See spring-projects/spring-framework#28585
1 parent 114b896 commit fe39598

File tree

4 files changed

+21
-70
lines changed

4 files changed

+21
-70
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ChildManagementContextInitializer.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.springframework.beans.factory.aot.BeanRegistrationExcludeFilter;
3131
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
3232
import org.springframework.beans.factory.support.RegisteredBean;
33-
import org.springframework.boot.AotProcessor;
3433
import org.springframework.boot.LazyInitializationBeanFactoryPostProcessor;
3534
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextFactory;
3635
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
@@ -96,10 +95,9 @@ public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registe
9695
BeanFactory parentBeanFactory = ((ConfigurableApplicationContext) this.parentContext).getBeanFactory();
9796
if (registeredBean.getBeanClass().equals(getClass())
9897
&& registeredBean.getBeanFactory().equals(parentBeanFactory)) {
99-
AotProcessor activeAotProcessor = AotProcessor.getActive(this.parentContext);
10098
ConfigurableApplicationContext managementContext = createManagementContext();
10199
registerBeans(managementContext);
102-
return new AotContribution(activeAotProcessor, managementContext);
100+
return new AotContribution(managementContext);
103101
}
104102
return null;
105103
}
@@ -155,23 +153,18 @@ ChildManagementContextInitializer withApplicationContextInitializer(
155153
*/
156154
private static class AotContribution implements BeanRegistrationAotContribution {
157155

158-
private final AotProcessor activeAotProcessor;
159-
160156
private final GenericApplicationContext managementContext;
161157

162-
AotContribution(AotProcessor activeAotProcessor, ConfigurableApplicationContext managementContext) {
158+
AotContribution(ConfigurableApplicationContext managementContext) {
163159
Assert.isInstanceOf(GenericApplicationContext.class, managementContext);
164-
this.activeAotProcessor = activeAotProcessor;
165160
this.managementContext = (GenericApplicationContext) managementContext;
166161
}
167162

168163
@Override
169164
public void applyTo(GenerationContext generationContext, BeanRegistrationCode beanRegistrationCode) {
170-
Class<?> target = (this.activeAotProcessor != null) ? this.activeAotProcessor.getApplication() : null;
171-
ClassName generatedInitializerClassName = generationContext.getClassNameGenerator()
172-
.generateClassName(target, "ManagementContextRegistrations");
173-
new ApplicationContextAotGenerator().generateApplicationContext(this.managementContext, target,
174-
"Management", generationContext, generatedInitializerClassName);
165+
GenerationContext managementGenerationContext = generationContext.withName("Management");
166+
ClassName generatedInitializerClassName = new ApplicationContextAotGenerator()
167+
.generateApplicationContext(this.managementContext, managementGenerationContext);
175168
GeneratedMethod postProcessorMethod = beanRegistrationCode.getMethodGenerator()
176169
.generateMethod("addManagementInitializer").using((builder) -> {
177170
builder.addJavadoc("Use AOT management context initialization");

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ChildManagementContextInitializerAotTests.java

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@
2525
import org.junit.jupiter.api.Test;
2626
import org.junit.jupiter.api.extension.ExtendWith;
2727

28+
import org.springframework.aot.generate.ClassNameGenerator;
2829
import org.springframework.aot.generate.DefaultGenerationContext;
2930
import org.springframework.aot.generate.InMemoryGeneratedFiles;
30-
import org.springframework.aot.generate.MethodGenerator;
31-
import org.springframework.aot.generate.MethodReference;
3231
import org.springframework.aot.test.generator.compile.CompileWithTargetClassAccess;
3332
import org.springframework.aot.test.generator.compile.TestCompiler;
34-
import org.springframework.beans.factory.aot.BeanRegistrationCode;
3533
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration;
3634
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration;
3735
import org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementContextAutoConfiguration;
@@ -79,10 +77,10 @@ void aotContributedInitializerStartsManagementContext(CapturedOutput output) {
7977
EndpointAutoConfiguration.class));
8078
contextRunner.withPropertyValues("server.port=0", "management.server.port=0").prepare((context) -> {
8179
InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles();
82-
DefaultGenerationContext generationContext = new DefaultGenerationContext(generatedFiles);
83-
ClassName className = ClassName.get("com.example", "TestInitializer");
84-
new ApplicationContextAotGenerator().generateApplicationContext(
85-
(GenericApplicationContext) context.getSourceApplicationContext(), generationContext, className);
80+
DefaultGenerationContext generationContext = new DefaultGenerationContext(
81+
new ClassNameGenerator(TestTarget.class), generatedFiles);
82+
ClassName className = new ApplicationContextAotGenerator().generateApplicationContext(
83+
(GenericApplicationContext) context.getSourceApplicationContext(), generationContext);
8684
generationContext.writeGeneratedContent();
8785
TestCompiler compiler = TestCompiler.forSystem();
8886
compiler.withFiles(generatedFiles).compile((compiled) -> {
@@ -105,21 +103,7 @@ private <T extends CharSequence> Consumer<T> numberOfOccurrences(String substrin
105103
};
106104
}
107105

108-
static class MockBeanRegistrationCode implements BeanRegistrationCode {
109-
110-
@Override
111-
public ClassName getClassName() {
112-
return null;
113-
}
114-
115-
@Override
116-
public MethodGenerator getMethodGenerator() {
117-
return null;
118-
}
119-
120-
@Override
121-
public void addInstancePostProcessor(MethodReference methodReference) {
122-
}
106+
static class TestTarget {
123107

124108
}
125109

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/AotProcessor.java

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@
2525
import java.util.Arrays;
2626
import java.util.Collections;
2727
import java.util.List;
28-
import java.util.Map;
29-
import java.util.concurrent.ConcurrentHashMap;
3028
import java.util.function.Consumer;
3129

30+
import org.springframework.aot.generate.ClassNameGenerator;
3231
import org.springframework.aot.generate.DefaultGenerationContext;
3332
import org.springframework.aot.generate.FileSystemGeneratedFiles;
3433
import org.springframework.aot.generate.GeneratedFiles.Kind;
@@ -62,8 +61,6 @@ public class AotProcessor {
6261
private static final Consumer<ExecutableHint.Builder> INVOKE_CONSTRUCTOR_HINT = (hint) -> hint
6362
.setModes(ExecutableMode.INVOKE);
6463

65-
private static final Map<ApplicationContext, AotProcessor> aotProcessors = new ConcurrentHashMap<>();
66-
6764
private final Class<?> application;
6865

6966
private final String[] applicationArgs;
@@ -101,31 +98,17 @@ public AotProcessor(Class<?> application, String[] applicationArgs, Path sourceO
10198
this.artifactId = artifactId;
10299
}
103100

104-
/**
105-
* Return the application class being processed.
106-
* @return the application class
107-
*/
108-
public Class<?> getApplication() {
109-
return this.application;
110-
}
111-
112101
/**
113102
* Trigger the processing of the application managed by this instance.
114103
*/
115-
void process() {
104+
public void process() {
116105
deleteExistingOutput();
117106
AotProcessorHook hook = new AotProcessorHook();
118107
SpringApplicationHooks.withHook(hook, this::callApplicationMainMethod);
119108
GenericApplicationContext applicationContext = hook.getApplicationContext();
120109
Assert.notNull(applicationContext, "No application context available after calling main method of '"
121110
+ this.application.getName() + "'. Does it run a SpringApplication?");
122-
aotProcessors.put(applicationContext, this);
123-
try {
124-
performAotProcessing(applicationContext);
125-
}
126-
finally {
127-
aotProcessors.remove(applicationContext);
128-
}
111+
performAotProcessing(applicationContext);
129112
}
130113

131114
private void deleteExistingOutput() {
@@ -161,12 +144,11 @@ private void callApplicationMainMethod() {
161144

162145
private void performAotProcessing(GenericApplicationContext applicationContext) {
163146
FileSystemGeneratedFiles generatedFiles = new FileSystemGeneratedFiles(this::getRoot);
164-
DefaultGenerationContext generationContext = new DefaultGenerationContext(generatedFiles);
147+
DefaultGenerationContext generationContext = new DefaultGenerationContext(
148+
new ClassNameGenerator(this.application), generatedFiles);
165149
ApplicationContextAotGenerator generator = new ApplicationContextAotGenerator();
166-
ClassName generatedInitializerClassName = generationContext.getClassNameGenerator()
167-
.generateClassName(this.application, "ApplicationContextInitializer");
168-
generator.generateApplicationContext(applicationContext, this.application, "", generationContext,
169-
generatedInitializerClassName);
150+
ClassName generatedInitializerClassName = generator.generateApplicationContext(applicationContext,
151+
generationContext);
170152
registerEntryPointHint(generationContext, generatedInitializerClassName);
171153
generationContext.writeGeneratedContent();
172154
writeHints(generationContext.getRuntimeHints());
@@ -243,16 +225,6 @@ public static void main(String[] args) throws Exception {
243225
.process();
244226
}
245227

246-
/**
247-
* Return the AOT processor that is actively processing the given
248-
* {@link ApplicationContext}.
249-
* @param applicationContext the application context to check
250-
* @return the {@link AotProcessor} or {@code null}
251-
*/
252-
public static AotProcessor getActive(ApplicationContext applicationContext) {
253-
return aotProcessors.get(applicationContext);
254-
}
255-
256228
/**
257229
* Hook used to capture the {@link ApplicationContext} and trigger early exit of main
258230
* method.

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBeanFactoryInitializationAotProcessorTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import org.junit.jupiter.api.Test;
2727

28+
import org.springframework.aot.generate.ClassNameGenerator;
2829
import org.springframework.aot.generate.DefaultGenerationContext;
2930
import org.springframework.aot.generate.GenerationContext;
3031
import org.springframework.aot.generate.InMemoryGeneratedFiles;
@@ -267,7 +268,8 @@ private RuntimeHints process(Class<?>... types) {
267268
private RuntimeHints process(ConfigurableListableBeanFactory beanFactory) {
268269
BeanFactoryInitializationAotContribution contribution = this.processor.processAheadOfTime(beanFactory);
269270
assertThat(contribution).isNotNull();
270-
GenerationContext generationContext = new DefaultGenerationContext(new InMemoryGeneratedFiles());
271+
GenerationContext generationContext = new DefaultGenerationContext(new ClassNameGenerator(Object.class),
272+
new InMemoryGeneratedFiles());
271273
contribution.applyTo(generationContext, mock(BeanFactoryInitializationCode.class));
272274
return generationContext.getRuntimeHints();
273275
}

0 commit comments

Comments
 (0)