Skip to content

Commit 1c10805

Browse files
committed
Close ApplicationContext after AOT processing
Closes gh-34841
1 parent d0b186a commit 1c10805

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

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/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-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.
@@ -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)