This commit is contained in:
Phillip Webb 2022-05-10 19:03:57 -07:00
parent 693941c2c2
commit c7590277a9
2 changed files with 7 additions and 26 deletions

View File

@ -409,9 +409,10 @@ public class SpringApplication {
private void addAotGeneratedInitializerIfNecessary(List<ApplicationContextInitializer<?>> initializers) { private void addAotGeneratedInitializerIfNecessary(List<ApplicationContextInitializer<?>> initializers) {
if (NativeDetector.inNativeImage()) { if (NativeDetector.inNativeImage()) {
try { try {
Class<?> aClass = Class.forName(this.mainApplicationClass.getName() + "__ApplicationContextInitializer", Class<?> initializerClass = Class.forName(
true, getClassLoader()); this.mainApplicationClass.getName() + "__ApplicationContextInitializer", true,
ApplicationContextInitializer<?> initializer = (ApplicationContextInitializer<?>) aClass getClassLoader());
ApplicationContextInitializer<?> initializer = (ApplicationContextInitializer<?>) initializerClass
.getDeclaredConstructor().newInstance(); .getDeclaredConstructor().newInstance();
initializers.add(0, initializer); initializers.add(0, initializer);
} }

View File

@ -20,6 +20,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.function.ThrowingSupplier;
/** /**
* Low-level hooks that can observe a {@link SpringApplication} and modify its behavior. * Low-level hooks that can observe a {@link SpringApplication} and modify its behavior.
@ -33,7 +34,6 @@ final class SpringApplicationHooks {
private static final ThreadLocal<Hooks> hooks = ThreadLocal.withInitial(Hooks::new); private static final ThreadLocal<Hooks> hooks = ThreadLocal.withInitial(Hooks::new);
private SpringApplicationHooks() { private SpringApplicationHooks() {
} }
/** /**
@ -44,10 +44,10 @@ final class SpringApplicationHooks {
* @return the result of the action * @return the result of the action
* @throws Exception if a failure occurs while performing the action * @throws Exception if a failure occurs while performing the action
*/ */
static <T> T withHook(Hook hook, Action<T> action) throws Exception { static <T> T withHook(Hook hook, ThrowingSupplier<T> action) throws Exception {
hooks.get().add(hook); hooks.get().add(hook);
try { try {
return action.perform(); return action.getWithException();
} }
finally { finally {
hooks.get().remove(hook); hooks.get().remove(hook);
@ -84,7 +84,6 @@ final class SpringApplicationHooks {
* @param application the application that is being run * @param application the application that is being run
*/ */
default void preRun(SpringApplication application) { default void preRun(SpringApplication application) {
} }
/** /**
@ -95,7 +94,6 @@ final class SpringApplicationHooks {
* @param context the application's context * @param context the application's context
*/ */
default void postRun(SpringApplication application, ConfigurableApplicationContext context) { default void postRun(SpringApplication application, ConfigurableApplicationContext context) {
} }
/** /**
@ -110,24 +108,6 @@ final class SpringApplicationHooks {
} }
/**
* An action that can be performed with a hook attached.
* <p>
* <strong>For internal use only.</strong>
*
* @param <T> the type of the action's result
*/
interface Action<T> {
/**
* Perform the action.
* @return the result of the action
* @throws Exception if a failure occurs
*/
T perform() throws Exception;
}
static final class Hooks implements Hook { static final class Hooks implements Hook {
private final List<Hook> delegates = new ArrayList<>(); private final List<Hook> delegates = new ArrayList<>();