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
This commit is contained in:
Stephane Nicoll
2022-10-18 16:57:47 +02:00
parent 004875670b
commit 82a0154bd1
4 changed files with 59 additions and 9 deletions

View File

@@ -30,20 +30,28 @@ import org.springframework.util.FileSystemUtils;
/**
* Abstract base class for filesystem-based ahead-of-time (AOT) processing.
*
* <p>Concrete implementations are typically used to kick off optimization of an
* application or test suite in a build tool.
* <p>Concrete implementations should override {@link #doProcess()} that kicks
* off the optimization of the target, usually an application.
*
* @author Stephane Nicoll
* @author Andy Wilkinson
* @author Phillip Webb
* @author Sam Brannen
* @since 6.0
* @param <T> the type of the processing result
* @see FileSystemGeneratedFiles
* @see FileNativeConfigurationWriter
* @see org.springframework.context.aot.ContextAotProcessor
* @see org.springframework.test.context.aot.TestAotProcessor
*/
public abstract class AbstractAotProcessor {
public abstract class AbstractAotProcessor<T> {
/**
* The name of a system property that is made available when the processor
* runs.
* @see #doProcess()
*/
private static final String AOT_PROCESSING = "spring.aot.processing";
private final Settings settings;
@@ -64,6 +72,22 @@ public abstract class AbstractAotProcessor {
return this.settings;
}
/**
* Run AOT processing.
* @return the result of the processing.
*/
public final T process() {
try {
System.setProperty(AOT_PROCESSING, "true");
return doProcess();
}
finally {
System.clearProperty(AOT_PROCESSING);
}
}
protected abstract T doProcess();
/**
* Delete the source, resource, and class output directories.
*/

View File

@@ -46,7 +46,7 @@ import org.springframework.util.CollectionUtils;
* @since 6.0
* @see org.springframework.test.context.aot.TestAotProcessor
*/
public abstract class ContextAotProcessor extends AbstractAotProcessor {
public abstract class ContextAotProcessor extends AbstractAotProcessor<ClassName> {
private final Class<?> applicationClass;
@@ -64,7 +64,7 @@ public abstract class ContextAotProcessor extends AbstractAotProcessor {
/**
* Get the the application entry point (typically a class with a {@code main()} method).
* Get the application entry point (typically a class with a {@code main()} method).
*/
protected Class<?> getApplicationClass() {
return this.applicationClass;
@@ -77,7 +77,8 @@ public abstract class ContextAotProcessor extends AbstractAotProcessor {
* @return the {@code ClassName} of the {@code ApplicationContextInitializer}
* entry point
*/
public ClassName process() {
@Override
protected ClassName doProcess() {
deleteExistingOutput();
GenericApplicationContext applicationContext = prepareApplicationContext(getApplicationClass());
return performAotProcessing(applicationContext);