[SPR-8386] Polishing SmartContextLoader SPI; AnnotationConfigContextLoader now only considers static inner classes annotated with @Configuration as configuration classes.
This commit is contained in:
@@ -34,14 +34,14 @@ public class ContextConfigurationAttributes {
|
||||
|
||||
private final Class<?> declaringClass;
|
||||
|
||||
private String[] locations;
|
||||
|
||||
private Class<?>[] classes;
|
||||
|
||||
private final boolean inheritLocations;
|
||||
|
||||
private final Class<? extends ContextLoader> contextLoaderClass;
|
||||
|
||||
private String[] locations;
|
||||
|
||||
private Class<?>[] classes;
|
||||
|
||||
|
||||
/**
|
||||
* Resolves resource locations from the {@link ContextConfiguration#locations() locations}
|
||||
@@ -97,6 +97,20 @@ public class ContextConfigurationAttributes {
|
||||
return this.declaringClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Document isInheritLocations().
|
||||
*/
|
||||
public boolean isInheritLocations() {
|
||||
return this.inheritLocations;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Document getContextLoaderClass().
|
||||
*/
|
||||
public Class<? extends ContextLoader> getContextLoaderClass() {
|
||||
return this.contextLoaderClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Document getLocations().
|
||||
*/
|
||||
@@ -125,20 +139,6 @@ public class ContextConfigurationAttributes {
|
||||
this.classes = classes;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Document isInheritLocations().
|
||||
*/
|
||||
public boolean isInheritLocations() {
|
||||
return this.inheritLocations;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Document getContextLoaderClass().
|
||||
*/
|
||||
public Class<? extends ContextLoader> getContextLoaderClass() {
|
||||
return this.contextLoaderClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Document overridden toString().
|
||||
*/
|
||||
|
||||
@@ -310,7 +310,7 @@ abstract class ContextLoaderUtils {
|
||||
if (contextLoader instanceof SmartContextLoader) {
|
||||
SmartContextLoader smartContextLoader = (SmartContextLoader) contextLoader;
|
||||
// TODO Decide on mutability of locations and classes properties
|
||||
smartContextLoader.processContextConfigurationAttributes(configAttributes);
|
||||
smartContextLoader.processContextConfiguration(configAttributes);
|
||||
locationsList.addAll(Arrays.asList(configAttributes.getLocations()));
|
||||
classesList.addAll(Arrays.asList(configAttributes.getClasses()));
|
||||
}
|
||||
|
||||
@@ -27,13 +27,13 @@ import org.springframework.context.ApplicationContext;
|
||||
public interface SmartContextLoader extends ContextLoader {
|
||||
|
||||
/**
|
||||
* TODO Document processContextConfigurationAttributes().
|
||||
* TODO Document processContextConfiguration().
|
||||
*/
|
||||
void processContextConfigurationAttributes(ContextConfigurationAttributes configAttributes);
|
||||
void processContextConfiguration(ContextConfigurationAttributes configAttributes);
|
||||
|
||||
/**
|
||||
* TODO Document loadContext().
|
||||
*/
|
||||
ApplicationContext loadContext(MergedContextConfiguration mergedContextConfiguration) throws Exception;
|
||||
ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception;
|
||||
|
||||
}
|
||||
|
||||
@@ -42,9 +42,9 @@ import org.springframework.util.StringUtils;
|
||||
public abstract class AbstractContextLoader implements SmartContextLoader {
|
||||
|
||||
/**
|
||||
* TODO Document processContextConfigurationAttributes().
|
||||
* TODO Document processContextConfiguration().
|
||||
*/
|
||||
public void processContextConfigurationAttributes(ContextConfigurationAttributes configAttributes) {
|
||||
public void processContextConfiguration(ContextConfigurationAttributes configAttributes) {
|
||||
String[] processedLocations = processLocations(configAttributes.getDeclaringClass(),
|
||||
configAttributes.getLocations());
|
||||
|
||||
|
||||
@@ -52,17 +52,16 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
|
||||
*
|
||||
* @see SmartContextLoader#loadContext(MergedContextConfiguration)
|
||||
*/
|
||||
public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedContextConfiguration)
|
||||
throws Exception {
|
||||
public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(String.format("Loading ApplicationContext for merged context configuration [%s].",
|
||||
mergedContextConfiguration));
|
||||
mergedConfig));
|
||||
}
|
||||
GenericApplicationContext context = new GenericApplicationContext();
|
||||
context.getEnvironment().setActiveProfiles(mergedContextConfiguration.getActiveProfiles());
|
||||
context.getEnvironment().setActiveProfiles(mergedConfig.getActiveProfiles());
|
||||
prepareContext(context);
|
||||
customizeBeanFactory(context.getDefaultListableBeanFactory());
|
||||
loadBeanDefinitions(context, mergedContextConfiguration);
|
||||
loadBeanDefinitions(context, mergedConfig);
|
||||
AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
|
||||
customizeContext(context);
|
||||
context.refresh();
|
||||
@@ -149,13 +148,12 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
|
||||
* and override this method to provide a custom strategy for loading or
|
||||
* registering bean definitions.
|
||||
* @param context the context into which the bean definitions should be loaded
|
||||
* @param mergedContextConfiguration TODO Document parameters.
|
||||
* @param mergedConfig TODO Document parameters.
|
||||
* @since 3.1
|
||||
* @see #loadContext
|
||||
*/
|
||||
protected void loadBeanDefinitions(GenericApplicationContext context,
|
||||
MergedContextConfiguration mergedContextConfiguration) {
|
||||
createBeanDefinitionReader(context).loadBeanDefinitions(mergedContextConfiguration.getLocations());
|
||||
protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) {
|
||||
createBeanDefinitionReader(context).loadBeanDefinitions(mergedConfig.getLocations());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -23,6 +23,7 @@ import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionReader;
|
||||
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.test.context.ContextConfigurationAttributes;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
@@ -43,14 +44,12 @@ public class AnnotationConfigContextLoader extends AbstractGenericContextLoader
|
||||
|
||||
|
||||
/**
|
||||
* TODO Document overridden processContextConfigurationAttributes().
|
||||
*
|
||||
* @see org.springframework.test.context.SmartContextLoader#processContextConfigurationAttributes
|
||||
* TODO Document overridden processContextConfiguration().
|
||||
*/
|
||||
public void processContextConfigurationAttributes(ContextConfigurationAttributes configAttributes) {
|
||||
public void processContextConfiguration(ContextConfigurationAttributes configAttributes) {
|
||||
if (ObjectUtils.isEmpty(configAttributes.getClasses()) && isGenerateDefaultClasses()) {
|
||||
Class<?>[] defaultConfigurationClasses = generateDefaultConfigurationClasses(configAttributes.getDeclaringClass());
|
||||
configAttributes.setClasses(defaultConfigurationClasses);
|
||||
Class<?>[] defaultConfigClasses = generateDefaultConfigurationClasses(configAttributes.getDeclaringClass());
|
||||
configAttributes.setClasses(defaultConfigClasses);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,6 +86,8 @@ public class AnnotationConfigContextLoader extends AbstractGenericContextLoader
|
||||
|
||||
/**
|
||||
* TODO Document isGenerateDefaultClasses().
|
||||
* <p>
|
||||
* TODO Consider renaming to a generic boolean generatesDefaults() method and moving to SmartContextLoader.
|
||||
*/
|
||||
protected boolean isGenerateDefaultClasses() {
|
||||
return true;
|
||||
@@ -101,7 +102,7 @@ public class AnnotationConfigContextLoader extends AbstractGenericContextLoader
|
||||
* class name refers to a nested <code>static</code> class within the
|
||||
* test class.
|
||||
*
|
||||
* @see #generateDefaultLocations(Class)
|
||||
* @see #generateDefaultConfigurationClasses(Class)
|
||||
*/
|
||||
protected String getConfigurationClassNameSuffix() {
|
||||
return "$ContextConfiguration";
|
||||
@@ -118,10 +119,18 @@ public class AnnotationConfigContextLoader extends AbstractGenericContextLoader
|
||||
|
||||
List<Class<?>> configClasses = new ArrayList<Class<?>>();
|
||||
try {
|
||||
configClasses.add((Class<?>) getClass().getClassLoader().loadClass(className));
|
||||
Class<?> configClass = (Class<?>) getClass().getClassLoader().loadClass(className);
|
||||
if (configClass.isAnnotationPresent(Configuration.class)) {
|
||||
configClasses.add(configClass);
|
||||
}
|
||||
else {
|
||||
logger.warn(String.format(
|
||||
"Found candidate configuration class [%s], but it is not annotated with @Configuration.", className));
|
||||
}
|
||||
}
|
||||
catch (ClassNotFoundException e) {
|
||||
logger.warn(String.format("Cannot load @Configuration class with generated class name [%s].", className), e);
|
||||
logger.warn(String.format(
|
||||
"Cannot load @Configuration class with generated class name [%s]: class not found", className));
|
||||
}
|
||||
|
||||
return configClasses.toArray(new Class<?>[configClasses.size()]);
|
||||
|
||||
Reference in New Issue
Block a user