Skip to content

Commit 4d15c13

Browse files
committed
Merge branch '6.2.x'
# Conflicts: # spring-context/src/test/java/org/springframework/context/aot/ContextAotProcessorTests.java
2 parents bb513de + 03620fc commit 4d15c13

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

spring-context/src/main/java/org/springframework/context/aot/ApplicationContextAotGenerator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,6 +50,7 @@ public class ApplicationContextAotGenerator {
5050
*/
5151
public ClassName processAheadOfTime(GenericApplicationContext applicationContext,
5252
GenerationContext generationContext) {
53+
5354
return withCglibClassHandler(new CglibClassHandler(generationContext), () -> {
5455
applicationContext.refreshForAotProcessing(generationContext.getRuntimeHints());
5556
ApplicationContextInitializationCodeGenerator codeGenerator =

spring-context/src/main/java/org/springframework/context/aot/ContextAotProcessor.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -80,8 +80,9 @@ protected Class<?> getApplicationClass() {
8080
@Override
8181
protected ClassName doProcess() {
8282
deleteExistingOutput();
83-
GenericApplicationContext applicationContext = prepareApplicationContext(getApplicationClass());
84-
return performAotProcessing(applicationContext);
83+
try (GenericApplicationContext applicationContext = prepareApplicationContext(getApplicationClass())) {
84+
return performAotProcessing(applicationContext);
85+
}
8586
}
8687

8788
/**

spring-context/src/main/java/org/springframework/context/aot/ReflectiveProcessorAotContributionBuilder.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -100,6 +100,7 @@ public ReflectiveProcessorAotContributionBuilder scan(@Nullable ClassLoader clas
100100
return (!this.classes.isEmpty() ? new AotContribution(this.classes) : null);
101101
}
102102

103+
103104
private static class AotContribution implements BeanFactoryInitializationAotContribution {
104105

105106
private final Class<?>[] classes;
@@ -113,9 +114,9 @@ public void applyTo(GenerationContext generationContext, BeanFactoryInitializati
113114
RuntimeHints runtimeHints = generationContext.getRuntimeHints();
114115
registrar.registerRuntimeHints(runtimeHints, this.classes);
115116
}
116-
117117
}
118118

119+
119120
private static class ReflectiveClassPathScanner extends ClassPathScanningCandidateComponentProvider {
120121

121122
private final @Nullable ClassLoader classLoader;

spring-context/src/test/java/org/springframework/context/aot/ContextAotProcessorTests.java

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,8 +45,9 @@ class ContextAotProcessorTests {
4545
void processGeneratesAssets(@TempDir Path directory) {
4646
GenericApplicationContext context = new AnnotationConfigApplicationContext();
4747
context.registerBean(SampleApplication.class);
48-
ContextAotProcessor processor = new DemoContextAotProcessor(SampleApplication.class, directory);
48+
DemoContextAotProcessor processor = new DemoContextAotProcessor(SampleApplication.class, directory);
4949
ClassName className = processor.process();
50+
assertThat(processor.context.isClosed()).isTrue();
5051
assertThat(className).isEqualTo(ClassName.get(SampleApplication.class.getPackageName(),
5152
"ContextAotProcessorTests_SampleApplication__ApplicationContextInitializer"));
5253
assertThat(directory).satisfies(hasGeneratedAssetsForSampleApplication());
@@ -61,9 +62,10 @@ void processingDeletesExistingOutput(@TempDir Path directory) throws IOException
6162
Path existingSourceOutput = createExisting(sourceOutput);
6263
Path existingResourceOutput = createExisting(resourceOutput);
6364
Path existingClassOutput = createExisting(classOutput);
64-
ContextAotProcessor processor = new DemoContextAotProcessor(SampleApplication.class,
65+
DemoContextAotProcessor processor = new DemoContextAotProcessor(SampleApplication.class,
6566
sourceOutput, resourceOutput, classOutput);
6667
processor.process();
68+
assertThat(processor.context.isClosed()).isTrue();
6769
assertThat(existingSourceOutput).doesNotExist();
6870
assertThat(existingResourceOutput).doesNotExist();
6971
assertThat(existingClassOutput).doesNotExist();
@@ -73,13 +75,14 @@ void processingDeletesExistingOutput(@TempDir Path directory) throws IOException
7375
void processWithEmptyNativeImageArgumentsDoesNotCreateNativeImageProperties(@TempDir Path directory) {
7476
GenericApplicationContext context = new AnnotationConfigApplicationContext();
7577
context.registerBean(SampleApplication.class);
76-
ContextAotProcessor processor = new DemoContextAotProcessor(SampleApplication.class, directory) {
78+
DemoContextAotProcessor processor = new DemoContextAotProcessor(SampleApplication.class, directory) {
7779
@Override
7880
protected List<String> getDefaultNativeImageArguments(String application) {
7981
return Collections.emptyList();
8082
}
8183
};
8284
processor.process();
85+
assertThat(processor.context.isClosed()).isTrue();
8386
assertThat(directory.resolve("resource/META-INF/native-image/com.example/example/native-image.properties"))
8487
.doesNotExist();
8588
context.close();
@@ -118,6 +121,8 @@ private Consumer<Path> hasGeneratedAssetsForSampleApplication() {
118121

119122
private static class DemoContextAotProcessor extends ContextAotProcessor {
120123

124+
AnnotationConfigApplicationContext context;
125+
121126
DemoContextAotProcessor(Class<?> application, Path rootPath) {
122127
this(application, rootPath.resolve("source"), rootPath.resolve("resource"), rootPath.resolve("class"));
123128
}
@@ -141,19 +146,19 @@ private static Settings createSettings(Path sourceOutput, Path resourceOutput,
141146
protected GenericApplicationContext prepareApplicationContext(Class<?> application) {
142147
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
143148
context.register(application);
149+
this.context = context;
144150
return context;
145151
}
146-
147152
}
148153

154+
149155
@Configuration(proxyBeanMethods = false)
150156
static class SampleApplication {
151157

152158
@Bean
153159
public String testBean() {
154160
return "Hello";
155161
}
156-
157162
}
158163

159164
}

0 commit comments

Comments
 (0)