From 87c39defa333f894deb6566bfeee0886045975a6 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Fri, 28 Feb 2025 16:51:36 +0100 Subject: [PATCH] Introduce RuntimeHints support in AotContextLoader This commit introduces a new loadContextForAotProcessing(...) variant in AotContextLoader which accepts a RuntimeHints argument. This new method is an interface default method which delegates to the existing loadContextForAotProcessing(MergedContextConfiguration) variant. Note, however, that the framework now only invokes the new loadContextForAotProcessing(...) variant within TestContextAotGenerator. Closes gh-34513 --- .../test/context/aot/AotContextLoader.java | 37 +++++++++++++++++-- .../context/aot/TestContextAotGenerator.java | 4 +- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/aot/AotContextLoader.java b/spring-test/src/main/java/org/springframework/test/context/aot/AotContextLoader.java index 25d649807321..3b5251d7c538 100644 --- a/spring-test/src/main/java/org/springframework/test/context/aot/AotContextLoader.java +++ b/spring-test/src/main/java/org/springframework/test/context/aot/AotContextLoader.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package org.springframework.test.context.aot; +import org.springframework.aot.hint.RuntimeHints; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; @@ -25,9 +26,9 @@ /** * Strategy interface for loading an {@link ApplicationContext} for build-time - * {@linkplain #loadContextForAotProcessing AOT processing} as well as run-time - * {@linkplain #loadContextForAotRuntime AOT execution} for an integration test - * managed by the Spring TestContext Framework. + * {@linkplain #loadContextForAotProcessing(MergedContextConfiguration, RuntimeHints) + * AOT processing} as well as run-time {@linkplain #loadContextForAotRuntime + * AOT execution} for an integration test managed by the Spring TestContext Framework. * *

{@code AotContextLoader} is an extension of the {@link SmartContextLoader} * SPI that allows a context loader to optionally provide ahead-of-time (AOT) @@ -69,10 +70,38 @@ public interface AotContextLoader extends SmartContextLoader { * application context * @return a new {@code GenericApplicationContext} * @throws ContextLoadException if context loading failed + * @see #loadContextForAotProcessing(MergedContextConfiguration, RuntimeHints) * @see #loadContextForAotRuntime(MergedContextConfiguration, ApplicationContextInitializer) */ ApplicationContext loadContextForAotProcessing(MergedContextConfiguration mergedConfig) throws Exception; + /** + * Load a new {@link ApplicationContext} for AOT build-time processing based + * on the supplied {@link MergedContextConfiguration}, configure the context, + * and return the context. + *

This variant of {@code loadContextForAotProcessing(...)} accepts a + * {@link RuntimeHints} argument. See the documentation for + * {@link #loadContextForAotProcessing(MergedContextConfiguration)} for details + * regarding the contract of this method. + *

The default implementation of this method delegates to + * {@code loadContextForAotProcessing(MergedContextConfiguration)}. Note, + * however, that the framework only invokes this method as of Spring Framework + * 6.2.4. + * @param mergedConfig the merged context configuration to use to load the + * application context + * @param runtimeHints the runtime hints + * @return a new {@code GenericApplicationContext} + * @throws ContextLoadException if context loading failed + * @see #loadContextForAotProcessing(MergedContextConfiguration) + * @see #loadContextForAotRuntime(MergedContextConfiguration, ApplicationContextInitializer) + * @since 6.2.4 + */ + default ApplicationContext loadContextForAotProcessing(MergedContextConfiguration mergedConfig, + RuntimeHints runtimeHints) throws Exception { + + return loadContextForAotProcessing(mergedConfig); + } + /** * Load a new {@link ApplicationContext} for AOT run-time execution based on * the supplied {@link MergedContextConfiguration} and diff --git a/spring-test/src/main/java/org/springframework/test/context/aot/TestContextAotGenerator.java b/spring-test/src/main/java/org/springframework/test/context/aot/TestContextAotGenerator.java index 7d81e97629d1..d3ec9eb444a3 100644 --- a/spring-test/src/main/java/org/springframework/test/context/aot/TestContextAotGenerator.java +++ b/spring-test/src/main/java/org/springframework/test/context/aot/TestContextAotGenerator.java @@ -331,7 +331,7 @@ ClassName processAheadOfTime(MergedContextConfiguration mergedConfig, * create {@link GenericApplicationContext GenericApplicationContexts}. * @throws TestContextAotException if an error occurs while loading the application * context or if one of the prerequisites is not met - * @see AotContextLoader#loadContextForAotProcessing(MergedContextConfiguration) + * @see AotContextLoader#loadContextForAotProcessing(MergedContextConfiguration, RuntimeHints) */ private GenericApplicationContext loadContextForAotProcessing( MergedContextConfiguration mergedConfig) throws TestContextAotException { @@ -345,7 +345,7 @@ private GenericApplicationContext loadContextForAotProcessing( if (contextLoader instanceof AotContextLoader aotContextLoader) { try { - ApplicationContext context = aotContextLoader.loadContextForAotProcessing(mergedConfig); + ApplicationContext context = aotContextLoader.loadContextForAotProcessing(mergedConfig, this.runtimeHints); if (context instanceof GenericApplicationContext gac) { return gac; }