Load ApplicationContextFailureProcessors from spring.factories
At the request of the Spring Boot team, ApplicationContextFailureProcessor implementations are now loaded via the spring.factories mechanism instead of supporting a single processor registered via subclasses of AbstractTestContextBootstrapper. This makes the retrieval and handling of processors internal to DefaultCacheAwareContextLoaderDelegate, while simultaneously supporting multiple processors that can be registered by anyone (i.e., not limited to projects that implement custom TestContextBootstrappers). See gh-28826 Closes gh-29387
This commit is contained in:
@@ -101,18 +101,4 @@ public interface CacheAwareContextLoaderDelegate {
|
||||
*/
|
||||
void closeContext(MergedContextConfiguration mergedContextConfiguration, @Nullable HierarchyMode hierarchyMode);
|
||||
|
||||
/**
|
||||
* Set the {@link ApplicationContextFailureProcessor} to use.
|
||||
* <p>The default implementation ignores the supplied processor.
|
||||
* <p>Concrete implementations should override this method to store a reference
|
||||
* to the supplied processor and use it to process {@link ContextLoadException
|
||||
* ContextLoadExceptions} thrown from context loaders in
|
||||
* {@link #loadContext(MergedContextConfiguration)}.
|
||||
* @param contextFailureProcessor the context failure processor to use
|
||||
* @since 6.0
|
||||
*/
|
||||
default void setContextFailureProcessor(ApplicationContextFailureProcessor contextFailureProcessor) {
|
||||
// no-op
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,6 +16,10 @@
|
||||
|
||||
package org.springframework.test.context.cache;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
@@ -23,6 +27,7 @@ import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.core.io.support.SpringFactoriesLoader;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.test.annotation.DirtiesContext.HierarchyMode;
|
||||
import org.springframework.test.context.ApplicationContextFailureProcessor;
|
||||
@@ -44,6 +49,11 @@ import org.springframework.util.Assert;
|
||||
* invoke the {@link #DefaultCacheAwareContextLoaderDelegate(ContextCache)}
|
||||
* and provide a custom {@link ContextCache} implementation.
|
||||
*
|
||||
* <p>As of Spring Framework 6.0, this class loads {@link ApplicationContextFailureProcessor}
|
||||
* implementations via the {@link SpringFactoriesLoader} mechanism and delegates to
|
||||
* them in {@link #loadContext(MergedContextConfiguration)} to process context
|
||||
* load failures.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 4.1
|
||||
*/
|
||||
@@ -51,6 +61,9 @@ public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContext
|
||||
|
||||
private static final Log logger = LogFactory.getLog(DefaultCacheAwareContextLoaderDelegate.class);
|
||||
|
||||
private static List<ApplicationContextFailureProcessor> contextFailureProcessors =
|
||||
getApplicationContextFailureProcessors();
|
||||
|
||||
/**
|
||||
* Default static cache of Spring application contexts.
|
||||
*/
|
||||
@@ -60,9 +73,6 @@ public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContext
|
||||
|
||||
private final ContextCache contextCache;
|
||||
|
||||
@Nullable
|
||||
private ApplicationContextFailureProcessor contextFailureProcessor;
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new {@code DefaultCacheAwareContextLoaderDelegate} using
|
||||
@@ -117,14 +127,14 @@ public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContext
|
||||
Throwable cause = ex;
|
||||
if (ex instanceof ContextLoadException cle) {
|
||||
cause = cle.getCause();
|
||||
if (this.contextFailureProcessor != null) {
|
||||
for (ApplicationContextFailureProcessor contextFailureProcessor : contextFailureProcessors) {
|
||||
try {
|
||||
this.contextFailureProcessor.processLoadFailure(cle.getApplicationContext(), cause);
|
||||
contextFailureProcessor.processLoadFailure(cle.getApplicationContext(), cause);
|
||||
}
|
||||
catch (Throwable throwable) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Ignoring exception thrown from ApplicationContextFailureProcessor [%s]: %s"
|
||||
.formatted(this.contextFailureProcessor, throwable));
|
||||
.formatted(contextFailureProcessor, throwable));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -153,12 +163,6 @@ public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContext
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setContextFailureProcessor(ApplicationContextFailureProcessor contextFailureProcessor) {
|
||||
this.contextFailureProcessor = contextFailureProcessor;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the {@link ContextCache} used by this context loader delegate.
|
||||
*/
|
||||
@@ -236,6 +240,7 @@ public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContext
|
||||
* unmodified.
|
||||
* <p>This allows for transparent {@link org.springframework.test.context.cache.ContextCache ContextCache}
|
||||
* support for AOT-optimized application contexts.
|
||||
* @since 6.0
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private MergedContextConfiguration replaceIfNecessary(MergedContextConfiguration mergedConfig) {
|
||||
@@ -248,4 +253,71 @@ public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContext
|
||||
return mergedConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the {@link ApplicationContextFailureProcessor} implementations to use,
|
||||
* loaded via the {@link SpringFactoriesLoader} mechanism.
|
||||
* @return the context failure processors to use
|
||||
* @since 6.0
|
||||
*/
|
||||
private static List<ApplicationContextFailureProcessor> getApplicationContextFailureProcessors() {
|
||||
SpringFactoriesLoader loader = SpringFactoriesLoader.forDefaultResourceLocation(
|
||||
DefaultCacheAwareContextLoaderDelegate.class.getClassLoader());
|
||||
List<ApplicationContextFailureProcessor> processors = loader.load(ApplicationContextFailureProcessor.class,
|
||||
DefaultCacheAwareContextLoaderDelegate::handleInstantiationFailure);
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Loaded default ApplicationContextFailureProcessor implementations from location [%s]: %s"
|
||||
.formatted(SpringFactoriesLoader.FACTORIES_RESOURCE_LOCATION, classNames(processors)));
|
||||
}
|
||||
else if (logger.isDebugEnabled()) {
|
||||
logger.debug("Loaded default ApplicationContextFailureProcessor implementations from location [%s]: %s"
|
||||
.formatted(SpringFactoriesLoader.FACTORIES_RESOURCE_LOCATION, classSimpleNames(processors)));
|
||||
}
|
||||
return processors;
|
||||
}
|
||||
|
||||
private static void handleInstantiationFailure(
|
||||
Class<?> factoryType, String factoryImplementationName, Throwable failure) {
|
||||
|
||||
Throwable ex = (failure instanceof InvocationTargetException ite ?
|
||||
ite.getTargetException() : failure);
|
||||
if (ex instanceof ClassNotFoundException || ex instanceof NoClassDefFoundError) {
|
||||
logSkippedComponent(factoryType, factoryImplementationName, ex);
|
||||
}
|
||||
else if (ex instanceof LinkageError) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("""
|
||||
Could not load %1$s [%2$s]. Specify custom %1$s classes or make the default %1$s classes \
|
||||
available.""".formatted(factoryType.getSimpleName(), factoryImplementationName), ex);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (ex instanceof RuntimeException runtimeException) {
|
||||
throw runtimeException;
|
||||
}
|
||||
if (ex instanceof Error error) {
|
||||
throw error;
|
||||
}
|
||||
throw new IllegalStateException(
|
||||
"Failed to load %s [%s].".formatted(factoryType.getSimpleName(), factoryImplementationName), ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static void logSkippedComponent(Class<?> factoryType, String factoryImplementationName, Throwable ex) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("""
|
||||
Skipping candidate %1$s [%2$s] due to a missing dependency. \
|
||||
Specify custom %1$s classes or make the default %1$s classes \
|
||||
and their required dependencies available. Offending class: [%3$s]"""
|
||||
.formatted(factoryType.getSimpleName(), factoryImplementationName, ex.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
private static List<String> classNames(Collection<?> components) {
|
||||
return components.stream().map(Object::getClass).map(Class::getName).toList();
|
||||
}
|
||||
|
||||
private static List<String> classSimpleNames(Collection<?> components) {
|
||||
return components.stream().map(Object::getClass).map(Class::getSimpleName).toList();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -35,7 +35,6 @@ import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||
import org.springframework.core.io.support.SpringFactoriesLoader;
|
||||
import org.springframework.core.io.support.SpringFactoriesLoader.FailureHandler;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.test.context.ApplicationContextFailureProcessor;
|
||||
import org.springframework.test.context.BootstrapContext;
|
||||
import org.springframework.test.context.CacheAwareContextLoaderDelegate;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
@@ -523,29 +522,7 @@ public abstract class AbstractTestContextBootstrapper implements TestContextBoot
|
||||
* @see #getApplicationContextFailureProcessor()
|
||||
*/
|
||||
protected CacheAwareContextLoaderDelegate getCacheAwareContextLoaderDelegate() {
|
||||
CacheAwareContextLoaderDelegate delegate = getBootstrapContext().getCacheAwareContextLoaderDelegate();
|
||||
ApplicationContextFailureProcessor contextFailureProcessor = getApplicationContextFailureProcessor();
|
||||
if (contextFailureProcessor != null) {
|
||||
delegate.setContextFailureProcessor(contextFailureProcessor);
|
||||
}
|
||||
return delegate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the {@link ApplicationContextFailureProcessor} to use.
|
||||
* <p>The default implementation returns {@code null}.
|
||||
* <p>Concrete subclasses may choose to override this method to provide an
|
||||
* {@code ApplicationContextFailureProcessor} that will be supplied to the
|
||||
* configured {@code CacheAwareContextLoaderDelegate} in
|
||||
* {@link #getCacheAwareContextLoaderDelegate()}.
|
||||
* @return the context failure processor to use, or {@code null} if no processor
|
||||
* should be used
|
||||
* @since 6.0
|
||||
* @see #getCacheAwareContextLoaderDelegate()
|
||||
*/
|
||||
@Nullable
|
||||
protected ApplicationContextFailureProcessor getApplicationContextFailureProcessor() {
|
||||
return null;
|
||||
return getBootstrapContext().getCacheAwareContextLoaderDelegate();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user