[SPR-8387] Introduced hasResources() in ContextConfigurationAttributes; plus minor polishing.
This commit is contained in:
parent
d2e6f82aa3
commit
b8624b470c
|
|
@ -55,7 +55,7 @@ public class ContextConfigurationAttributes {
|
||||||
*
|
*
|
||||||
* @throws IllegalStateException if both the locations and value attributes have been declared
|
* @throws IllegalStateException if both the locations and value attributes have been declared
|
||||||
*/
|
*/
|
||||||
static String[] resolveLocations(Class<?> declaringClass, ContextConfiguration contextConfiguration) {
|
private static String[] resolveLocations(Class<?> declaringClass, ContextConfiguration contextConfiguration) {
|
||||||
Assert.notNull(declaringClass, "declaringClass must not be null");
|
Assert.notNull(declaringClass, "declaringClass must not be null");
|
||||||
|
|
||||||
String[] locations = contextConfiguration.locations();
|
String[] locations = contextConfiguration.locations();
|
||||||
|
|
@ -165,6 +165,18 @@ public class ContextConfigurationAttributes {
|
||||||
this.classes = classes;
|
this.classes = classes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if this {@code ContextConfigurationAttributes} instance has
|
||||||
|
* either path-based resource locations or class-based resources.
|
||||||
|
* @return <code>true</code> if neither the {@link #getLocations() locations}
|
||||||
|
* nor the {@link #getClasses() classes} array is empty
|
||||||
|
* @see #getLocations()
|
||||||
|
* @see #getClasses()
|
||||||
|
*/
|
||||||
|
public boolean hasResources() {
|
||||||
|
return !ObjectUtils.isEmpty(getLocations()) && !ObjectUtils.isEmpty(getClasses());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the <code>inheritLocations</code> flag that was declared via
|
* Get the <code>inheritLocations</code> flag that was declared via
|
||||||
* {@link ContextConfiguration @ContextConfiguration}.
|
* {@link ContextConfiguration @ContextConfiguration}.
|
||||||
|
|
|
||||||
|
|
@ -138,16 +138,16 @@ public class AnnotationConfigContextLoader extends AbstractGenericContextLoader
|
||||||
|
|
||||||
List<Class<?>> configClasses = new ArrayList<Class<?>>();
|
List<Class<?>> configClasses = new ArrayList<Class<?>>();
|
||||||
|
|
||||||
for (Class<?> configClass : declaringClass.getDeclaredClasses()) {
|
for (Class<?> candidate : declaringClass.getDeclaredClasses()) {
|
||||||
if (isDefaultConfigurationClassCandidate(configClass)) {
|
if (isDefaultConfigurationClassCandidate(candidate)) {
|
||||||
configClasses.add(configClass);
|
configClasses.add(candidate);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug(String.format(
|
logger.debug(String.format(
|
||||||
"Ignoring class [%s]; it must be static, non-private, non-final, and annotated "
|
"Ignoring class [%s]; it must be static, non-private, non-final, and annotated "
|
||||||
+ "with @Configuration to be considered a default configuration class.",
|
+ "with @Configuration to be considered a default configuration class.",
|
||||||
configClass.getName()));
|
candidate.getName()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,6 @@ import org.springframework.test.context.ContextLoader;
|
||||||
import org.springframework.test.context.MergedContextConfiguration;
|
import org.springframework.test.context.MergedContextConfiguration;
|
||||||
import org.springframework.test.context.SmartContextLoader;
|
import org.springframework.test.context.SmartContextLoader;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.ObjectUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO Document DelegatingSmartContextLoader.
|
* TODO Document DelegatingSmartContextLoader.
|
||||||
|
|
@ -60,38 +59,30 @@ public class DelegatingSmartContextLoader implements SmartContextLoader {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* TODO Document emptyResources().
|
|
||||||
*/
|
|
||||||
private boolean emptyResources(ContextConfigurationAttributes configAttributes) {
|
|
||||||
return ObjectUtils.isEmpty(configAttributes.getLocations())
|
|
||||||
&& ObjectUtils.isEmpty(configAttributes.getClasses());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO Document processContextConfiguration() implementation.
|
* TODO Document processContextConfiguration() implementation.
|
||||||
*/
|
*/
|
||||||
public void processContextConfiguration(ContextConfigurationAttributes configAttributes) {
|
public void processContextConfiguration(ContextConfigurationAttributes configAttributes) {
|
||||||
|
|
||||||
final boolean emptyResources = emptyResources(configAttributes);
|
final boolean hasResources = configAttributes.hasResources();
|
||||||
|
|
||||||
for (SmartContextLoader loader : candidates) {
|
for (SmartContextLoader loader : candidates) {
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug(String.format("Delegating to %s to process context configuration [%s].",
|
logger.debug(String.format("Potentially delegating to %s to process context configuration [%s].",
|
||||||
loader.getClass().getName(), configAttributes));
|
loader.getClass().getName(), configAttributes));
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the original locations and classes were not empty, there's no
|
// If the original locations and classes were not empty, there's no
|
||||||
// need to bother with default generation checks; just let each
|
// need to bother with default generation checks; just let each
|
||||||
// loader process the configuration.
|
// loader process the configuration.
|
||||||
if (!emptyResources) {
|
if (hasResources) {
|
||||||
loader.processContextConfiguration(configAttributes);
|
loader.processContextConfiguration(configAttributes);
|
||||||
}
|
}
|
||||||
// Otherwise, if the loader claims to generate defaults, let it
|
// Otherwise, if the loader claims to generate defaults, let it
|
||||||
// process the configuration.
|
// process the configuration.
|
||||||
else if (loader.generatesDefaults()) {
|
else if (loader.generatesDefaults()) {
|
||||||
loader.processContextConfiguration(configAttributes);
|
loader.processContextConfiguration(configAttributes);
|
||||||
if (!emptyResources(configAttributes) && logger.isInfoEnabled()) {
|
if (configAttributes.hasResources() && logger.isInfoEnabled()) {
|
||||||
logger.info(String.format("SmartContextLoader candidate %s "
|
logger.info(String.format("SmartContextLoader candidate %s "
|
||||||
+ "generated defaults for context configuration [%s].", loader, configAttributes));
|
+ "generated defaults for context configuration [%s].", loader, configAttributes));
|
||||||
}
|
}
|
||||||
|
|
@ -100,7 +91,7 @@ public class DelegatingSmartContextLoader implements SmartContextLoader {
|
||||||
|
|
||||||
// If any loader claims to generate defaults but none actually did,
|
// If any loader claims to generate defaults but none actually did,
|
||||||
// throw an exception.
|
// throw an exception.
|
||||||
if (generatesDefaults() && emptyResources(configAttributes)) {
|
if (generatesDefaults() && !configAttributes.hasResources()) {
|
||||||
throw new IllegalStateException(String.format("None of the SmartContextLoader candidates %s "
|
throw new IllegalStateException(String.format("None of the SmartContextLoader candidates %s "
|
||||||
+ "was able to generate defaults for context configuration [%s].", candidates, configAttributes));
|
+ "was able to generate defaults for context configuration [%s].", candidates, configAttributes));
|
||||||
}
|
}
|
||||||
|
|
@ -128,10 +119,8 @@ public class DelegatingSmartContextLoader implements SmartContextLoader {
|
||||||
Assert.notNull(mergedConfig, "mergedConfig must not be null");
|
Assert.notNull(mergedConfig, "mergedConfig must not be null");
|
||||||
|
|
||||||
for (SmartContextLoader loader : candidates) {
|
for (SmartContextLoader loader : candidates) {
|
||||||
|
|
||||||
// Ask each loader if it can load a context from the mergedConfig.
|
// Ask each loader if it can load a context from the mergedConfig.
|
||||||
// If a loader can, let it; otherwise, continue iterating over all
|
// If it can, let it; otherwise, keep iterating.
|
||||||
// remaining candidates.
|
|
||||||
if (loader.supports(mergedConfig)) {
|
if (loader.supports(mergedConfig)) {
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug(String.format("Delegating to %s to load context from [%s].",
|
logger.debug(String.format("Delegating to %s to load context from [%s].",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue