Skip to content

Commit 82a0154

Browse files
committed
Expose a system property when AOT processing is running
This commit exposes a "spring.aot.processing" system property when the AOT engine is running. This can be used by code that need to react differently when the application is being refreshed for AOT processing. Closes gh-29340
1 parent 0048756 commit 82a0154

File tree

4 files changed

+59
-9
lines changed

4 files changed

+59
-9
lines changed

Diff for: spring-context/src/main/java/org/springframework/context/aot/AbstractAotProcessor.java

+27-3
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,28 @@
3030
/**
3131
* Abstract base class for filesystem-based ahead-of-time (AOT) processing.
3232
*
33-
* <p>Concrete implementations are typically used to kick off optimization of an
34-
* application or test suite in a build tool.
33+
* <p>Concrete implementations should override {@link #doProcess()} that kicks
34+
* off the optimization of the target, usually an application.
3535
*
3636
* @author Stephane Nicoll
3737
* @author Andy Wilkinson
3838
* @author Phillip Webb
3939
* @author Sam Brannen
4040
* @since 6.0
41+
* @param <T> the type of the processing result
4142
* @see FileSystemGeneratedFiles
4243
* @see FileNativeConfigurationWriter
4344
* @see org.springframework.context.aot.ContextAotProcessor
4445
* @see org.springframework.test.context.aot.TestAotProcessor
4546
*/
46-
public abstract class AbstractAotProcessor {
47+
public abstract class AbstractAotProcessor<T> {
48+
49+
/**
50+
* The name of a system property that is made available when the processor
51+
* runs.
52+
* @see #doProcess()
53+
*/
54+
private static final String AOT_PROCESSING = "spring.aot.processing";
4755

4856
private final Settings settings;
4957

@@ -64,6 +72,22 @@ protected Settings getSettings() {
6472
return this.settings;
6573
}
6674

75+
/**
76+
* Run AOT processing.
77+
* @return the result of the processing.
78+
*/
79+
public final T process() {
80+
try {
81+
System.setProperty(AOT_PROCESSING, "true");
82+
return doProcess();
83+
}
84+
finally {
85+
System.clearProperty(AOT_PROCESSING);
86+
}
87+
}
88+
89+
protected abstract T doProcess();
90+
6791
/**
6892
* Delete the source, resource, and class output directories.
6993
*/

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
* @since 6.0
4747
* @see org.springframework.test.context.aot.TestAotProcessor
4848
*/
49-
public abstract class ContextAotProcessor extends AbstractAotProcessor {
49+
public abstract class ContextAotProcessor extends AbstractAotProcessor<ClassName> {
5050

5151
private final Class<?> applicationClass;
5252

@@ -64,7 +64,7 @@ protected ContextAotProcessor(Class<?> applicationClass, Settings settings) {
6464

6565

6666
/**
67-
* Get the the application entry point (typically a class with a {@code main()} method).
67+
* Get the application entry point (typically a class with a {@code main()} method).
6868
*/
6969
protected Class<?> getApplicationClass() {
7070
return this.applicationClass;
@@ -77,7 +77,8 @@ protected Class<?> getApplicationClass() {
7777
* @return the {@code ClassName} of the {@code ApplicationContextInitializer}
7878
* entry point
7979
*/
80-
public ClassName process() {
80+
@Override
81+
protected ClassName doProcess() {
8182
deleteExistingOutput();
8283
GenericApplicationContext applicationContext = prepareApplicationContext(getApplicationClass());
8384
return performAotProcessing(applicationContext);

Diff for: spring-context/src/test/java/org/springframework/context/aot/AotProcessorTests.java

+24-1
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,26 @@
2727
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
2828

2929
/**
30-
* Tests for {@link AbstractAotProcessor}, settings, and builder.
30+
* Tests for {@link AbstractAotProcessor}.
3131
*
3232
* @author Sam Brannen
33+
* @author Stephane Nicoll
3334
* @since 6.0
3435
*/
3536
class AotProcessorTests {
3637

38+
@Test
39+
void springAotProcessingIsAvailableInDoProcess(@TempDir Path tempDir) {
40+
Settings settings = createTestSettings(tempDir);
41+
assertThat(new AbstractAotProcessor<String>(settings) {
42+
@Override
43+
protected String doProcess() {
44+
assertThat(System.getProperty("spring.aot.processing")).isEqualTo("true");
45+
return "Hello";
46+
}
47+
}.process()).isEqualTo("Hello");
48+
}
49+
3750
@Test
3851
void builderRejectsMissingSourceOutput() {
3952
assertThatIllegalArgumentException()
@@ -123,4 +136,14 @@ void builderAcceptsRequiredSettings(@TempDir Path tempDir) {
123136
assertThat(settings.getArtifactId()).isEqualTo("my-artifact");
124137
}
125138

139+
private static Settings createTestSettings(Path tempDir) {
140+
return Settings.builder()
141+
.sourceOutput(tempDir)
142+
.resourceOutput(tempDir)
143+
.classOutput(tempDir)
144+
.groupId("my-group")
145+
.artifactId("my-artifact")
146+
.build();
147+
}
148+
126149
}

Diff for: spring-test/src/main/java/org/springframework/test/context/aot/TestAotProcessor.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* @see TestContextAotGenerator
3737
* @see org.springframework.context.aot.ContextAotProcessor
3838
*/
39-
public abstract class TestAotProcessor extends AbstractAotProcessor {
39+
public abstract class TestAotProcessor extends AbstractAotProcessor<Void> {
4040

4141
private final Set<Path> classpathRoots;
4242

@@ -66,9 +66,11 @@ protected Set<Path> getClasspathRoots() {
6666
* {@linkplain #deleteExistingOutput() clearing output directories} first and
6767
* then {@linkplain #performAotProcessing() performing AOT processing}.
6868
*/
69-
public void process() {
69+
@Override
70+
protected Void doProcess() {
7071
deleteExistingOutput();
7172
performAotProcessing();
73+
return null;
7274
}
7375

7476
/**

0 commit comments

Comments
 (0)