From 69d87f16eeddfe5974d650f91e936b82cd72fe00 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sat, 20 Aug 2022 13:54:28 +0200 Subject: [PATCH 1/2] Reorganize internals of DefaultCacheAwareContextLoaderDelegate --- ...efaultCacheAwareContextLoaderDelegate.java | 66 +++++++++---------- 1 file changed, 31 insertions(+), 35 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/cache/DefaultCacheAwareContextLoaderDelegate.java b/spring-test/src/main/java/org/springframework/test/context/cache/DefaultCacheAwareContextLoaderDelegate.java index 133373dc7f..e469b7d610 100644 --- a/spring-test/src/main/java/org/springframework/test/context/cache/DefaultCacheAwareContextLoaderDelegate.java +++ b/spring-test/src/main/java/org/springframework/test/context/cache/DefaultCacheAwareContextLoaderDelegate.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 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. @@ -73,40 +73,6 @@ public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContext this.contextCache = contextCache; } - /** - * Get the {@link ContextCache} used by this context loader delegate. - */ - protected ContextCache getContextCache() { - return this.contextCache; - } - - /** - * Load the {@code ApplicationContext} for the supplied merged context configuration. - *

Supports both the {@link SmartContextLoader} and {@link ContextLoader} SPIs. - * @throws Exception if an error occurs while loading the application context - */ - protected ApplicationContext loadContextInternal(MergedContextConfiguration mergedContextConfiguration) - throws Exception { - - ContextLoader contextLoader = mergedContextConfiguration.getContextLoader(); - Assert.notNull(contextLoader, "Cannot load an ApplicationContext with a NULL 'contextLoader'. " + - "Consider annotating your test class with @ContextConfiguration or @ContextHierarchy."); - - ApplicationContext applicationContext; - - if (contextLoader instanceof SmartContextLoader) { - SmartContextLoader smartContextLoader = (SmartContextLoader) contextLoader; - applicationContext = smartContextLoader.loadContext(mergedContextConfiguration); - } - else { - String[] locations = mergedContextConfiguration.getLocations(); - Assert.notNull(locations, "Cannot load an ApplicationContext with a NULL 'locations' array. " + - "Consider annotating your test class with @ContextConfiguration or @ContextHierarchy."); - applicationContext = contextLoader.loadContext(locations); - } - - return applicationContext; - } @Override public boolean isContextLoaded(MergedContextConfiguration mergedContextConfiguration) { @@ -152,4 +118,34 @@ public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContext } } + /** + * Get the {@link ContextCache} used by this context loader delegate. + */ + protected ContextCache getContextCache() { + return this.contextCache; + } + + /** + * Load the {@code ApplicationContext} for the supplied merged context configuration. + *

Supports both the {@link SmartContextLoader} and {@link ContextLoader} SPIs. + * @throws Exception if an error occurs while loading the application context + */ + protected ApplicationContext loadContextInternal(MergedContextConfiguration mergedContextConfiguration) + throws Exception { + + ContextLoader contextLoader = mergedContextConfiguration.getContextLoader(); + Assert.notNull(contextLoader, "Cannot load an ApplicationContext with a NULL 'contextLoader'. " + + "Consider annotating your test class with @ContextConfiguration or @ContextHierarchy."); + + if (contextLoader instanceof SmartContextLoader) { + return ((SmartContextLoader) contextLoader).loadContext(mergedContextConfiguration); + } + else { + String[] locations = mergedContextConfiguration.getLocations(); + Assert.notNull(locations, "Cannot load an ApplicationContext with a NULL 'locations' array. " + + "Consider annotating your test class with @ContextConfiguration or @ContextHierarchy."); + return contextLoader.loadContext(locations); + } + } + } From 711820ec708c866d72c92142ba17e6ffe1fa7f44 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Thu, 18 Aug 2022 17:37:55 +0200 Subject: [PATCH 2/2] Introduce createContext() factory method in AbstractWebGenericContextLoader Prior to this commit it was possible to configure the DefaultListableBeanFactory used by the GenericWebApplicationContext created by AbstractWebGenericContextLoader, but it was not possible to completely replace the bean factory. This commit introduces a new createContext() factory method in AbstractWebGenericContextLoader which indirectly allows subclasses to supply a custom DefaultListableBeanFactory implementation to the GenericWebApplicationContext. See gh-25600 Closes gh-28983 --- .../support/AbstractGenericContextLoader.java | 7 ++++--- .../web/AbstractGenericWebContextLoader.java | 19 +++++++++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/support/AbstractGenericContextLoader.java b/spring-test/src/main/java/org/springframework/test/context/support/AbstractGenericContextLoader.java index 45ae716f6f..6b94938bb9 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/AbstractGenericContextLoader.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/AbstractGenericContextLoader.java @@ -200,9 +200,10 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader * Factory method for creating the {@link GenericApplicationContext} used by * this {@code ContextLoader}. *

The default implementation creates a {@code GenericApplicationContext} - * using the default constructor. This method may get overridden e.g. to use - * a custom context subclass or to create a {@code GenericApplicationContext} - * with a custom {@link DefaultListableBeanFactory} implementation. + * using the default constructor. This method may be overridden — for + * example, to use a custom context subclass or to create a + * {@code GenericApplicationContext} with a custom + * {@link DefaultListableBeanFactory} implementation. * @return a newly instantiated {@code GenericApplicationContext} * @since 5.2.9 */ diff --git a/spring-test/src/main/java/org/springframework/test/context/web/AbstractGenericWebContextLoader.java b/spring-test/src/main/java/org/springframework/test/context/web/AbstractGenericWebContextLoader.java index e48cfecc8a..43f1539ff1 100644 --- a/spring-test/src/main/java/org/springframework/test/context/web/AbstractGenericWebContextLoader.java +++ b/spring-test/src/main/java/org/springframework/test/context/web/AbstractGenericWebContextLoader.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 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. @@ -114,7 +114,7 @@ public abstract class AbstractGenericWebContextLoader extends AbstractContextLoa validateMergedContextConfiguration(webMergedConfig); - GenericWebApplicationContext context = new GenericWebApplicationContext(); + GenericWebApplicationContext context = createContext(); ApplicationContext parent = mergedConfig.getParentApplicationContext(); if (parent != null) { @@ -145,6 +145,21 @@ public abstract class AbstractGenericWebContextLoader extends AbstractContextLoa // no-op } + /** + * Factory method for creating the {@link GenericWebApplicationContext} used + * by this {@code ContextLoader}. + *

The default implementation creates a {@code GenericWebApplicationContext} + * using the default constructor. This method may be overridden — for + * example, to use a custom context subclass or to create a + * {@code GenericWebApplicationContext} with a custom + * {@link DefaultListableBeanFactory} implementation. + * @return a newly instantiated {@code GenericWebApplicationContext} + * @since 5.2.23 + */ + protected GenericWebApplicationContext createContext() { + return new GenericWebApplicationContext(); + } + /** * Configures web resources for the supplied web application context (WAC). *

Implementation Details