[SPR-6184] AnnotationConfigContextLoader now extends AbstractGenericContextLoader; added new extension points to AbstractGenericContextLoader.

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@4141 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
Sam Brannen 2011-03-30 23:50:19 +00:00
parent 8c4a19caf8
commit d20150b0d5
3 changed files with 93 additions and 107 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2008 the original author or authors. * Copyright 2002-2011 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,7 +18,6 @@ package org.springframework.test.context.support;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.support.BeanDefinitionReader; import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
@ -48,37 +47,35 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
* Loads a Spring ApplicationContext from the supplied <code>locations</code>. * Loads a Spring ApplicationContext from the supplied <code>locations</code>.
* <p>Implementation details: * <p>Implementation details:
* <ul> * <ul>
* <li>Creates a standard {@link GenericApplicationContext} instance.</li> * <li>Delegates to {@link #createGenericApplicationContext()} to
* <li>Populates it from the specified config locations through a * create a {@link GenericApplicationContext} instance.</li>
* {@link #createBeanDefinitionReader(GenericApplicationContext) BeanDefinitionReader}.</li> * <li>Calls {@link #prepareContext(GenericApplicationContext)} to
* prepare the context.</li>
* <li>Calls {@link #customizeBeanFactory(DefaultListableBeanFactory)} to * <li>Calls {@link #customizeBeanFactory(DefaultListableBeanFactory)} to
* allow for customizing the context's DefaultListableBeanFactory.</li> * allow for customizing the context's <code>DefaultListableBeanFactory</code>.</li>
* <li>Delegates to {@link #loadBeanDefinitions(GenericApplicationContext, String...)}
* to populate the context from the specified config locations.</li>
* <li>Delegates to {@link AnnotationConfigUtils} for * <li>Delegates to {@link AnnotationConfigUtils} for
* {@link AnnotationConfigUtils#registerAnnotationConfigProcessors(org.springframework.beans.factory.support.BeanDefinitionRegistry) registering} * {@link AnnotationConfigUtils#registerAnnotationConfigProcessors registering}
* annotation configuration processors.</li> * annotation configuration processors.</li>
* <li>Calls {@link #customizeContext(GenericApplicationContext)} to allow * <li>Calls {@link #customizeContext(GenericApplicationContext)} to allow
* for customizing the context before it is refreshed.</li> * for customizing the context before it is refreshed.</li>
* <li>{@link ConfigurableApplicationContext#refresh() Refreshes} the * <li>{@link ConfigurableApplicationContext#refresh() Refreshes} the
* context and registers a JVM shutdown hook for it.</li> * context and registers a JVM shutdown hook for it.</li>
* </ul> * </ul>
* <p>Subclasses must provide an appropriate implementation of
* {@link #createBeanDefinitionReader(GenericApplicationContext)}.
* @return a new application context * @return a new application context
* @see org.springframework.test.context.ContextLoader#loadContext * @see org.springframework.test.context.ContextLoader#loadContext
* @see GenericApplicationContext * @see GenericApplicationContext
* @see #customizeBeanFactory(DefaultListableBeanFactory)
* @see #createBeanDefinitionReader(GenericApplicationContext)
* @see BeanDefinitionReader
*/ */
public final ConfigurableApplicationContext loadContext(String... locations) throws Exception { public final ConfigurableApplicationContext loadContext(String... locations) throws Exception {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("Loading ApplicationContext for locations [" + logger.debug(String.format("Loading ApplicationContext for locations [%s].",
StringUtils.arrayToCommaDelimitedString(locations) + "]."); StringUtils.arrayToCommaDelimitedString(locations)));
} }
GenericApplicationContext context = new GenericApplicationContext(); GenericApplicationContext context = createGenericApplicationContext();
prepareContext(context); prepareContext(context);
customizeBeanFactory(context.getDefaultListableBeanFactory()); customizeBeanFactory(context.getDefaultListableBeanFactory());
createBeanDefinitionReader(context).loadBeanDefinitions(locations); loadBeanDefinitions(context, locations);
AnnotationConfigUtils.registerAnnotationConfigProcessors(context); AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
customizeContext(context); customizeContext(context);
context.refresh(); context.refresh();
@ -87,7 +84,21 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
} }
/** /**
* Prepare the {@link GenericApplicationContext} created by this ContextLoader. * Factory method for creating a new {@link GenericApplicationContext}
* to be used by this <code>ContextLoader</code>.
* <p>The default implementation returns an instance of
* {@link GenericApplicationContext}. Can be overridden in subclasses
* to return a specific subclass of <code>GenericApplicationContext</code>.
* @return a new <code>GenericApplicationContext</code>
* @since 3.1
* @see #loadContext
*/
protected GenericApplicationContext createGenericApplicationContext() {
return new GenericApplicationContext();
}
/**
* Prepare the {@link GenericApplicationContext} created by this <code>ContextLoader</code>.
* Called <i>before</> bean definitions are read. * Called <i>before</> bean definitions are read.
* <p>The default implementation is empty. Can be overridden in subclasses to * <p>The default implementation is empty. Can be overridden in subclasses to
* customize GenericApplicationContext's standard settings. * customize GenericApplicationContext's standard settings.
@ -100,11 +111,10 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
} }
/** /**
* Customize the internal bean factory of the ApplicationContext created by * Customize the internal bean factory of the ApplicationContext created by this <code>ContextLoader</code>.
* this ContextLoader.
* <p>The default implementation is empty but can be overridden in subclasses * <p>The default implementation is empty but can be overridden in subclasses
* to customize DefaultListableBeanFactory's standard settings. * to customize DefaultListableBeanFactory's standard settings.
* @param beanFactory the bean factory created by this ContextLoader * @param beanFactory the bean factory created by this <code>ContextLoader</code>
* @see #loadContext * @see #loadContext
* @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowBeanDefinitionOverriding(boolean) * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowBeanDefinitionOverriding(boolean)
* @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowEagerClassLoading(boolean) * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowEagerClassLoading(boolean)
@ -115,24 +125,45 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
} }
/** /**
* Factory method for creating new {@link BeanDefinitionReader}s for * Load bean definitions into the supplied {@link GenericApplicationContext context}
* from the specified resource locations.
* <p>The default implementation delegates to the {@link BeanDefinitionReader}
* returned by {@link #createBeanDefinitionReader} to
* {@link BeanDefinitionReader#loadBeanDefinitions(String) load} the
* bean definitions.
* <p>Subclasses must provide an appropriate implementation of
* {@link #createBeanDefinitionReader}.
* Alternatively subclasses may provide a <em>no-op</em> implementation
* of {@link #createBeanDefinitionReader} 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 locations the resource locations from which to load the bean definitions
* @since 3.1
* @see #loadContext
*/
protected void loadBeanDefinitions(GenericApplicationContext context, String... locations) {
createBeanDefinitionReader(context).loadBeanDefinitions(locations);
}
/**
* Factory method for creating a new {@link BeanDefinitionReader} for
* loading bean definitions into the supplied * loading bean definitions into the supplied
* {@link GenericApplicationContext context}. * {@link GenericApplicationContext context}.
* @param context the context for which the BeanDefinitionReader should be created * @param context the context for which the BeanDefinitionReader should be created
* @return a BeanDefinitionReader for the supplied context * @return a BeanDefinitionReader for the supplied context
* @see #loadContext * @see #loadBeanDefinitions
* @see BeanDefinitionReader * @see BeanDefinitionReader
*/ */
protected abstract BeanDefinitionReader createBeanDefinitionReader(GenericApplicationContext context); protected abstract BeanDefinitionReader createBeanDefinitionReader(GenericApplicationContext context);
/** /**
* Customize the {@link GenericApplicationContext} created by this ContextLoader * Customize the {@link GenericApplicationContext} created by this
* <i>after</i> bean definitions have been loaded into the context but * <code>ContextLoader</code> <i>after</i> bean definitions have been
* before the context is refreshed. * loaded into the context but before the context is refreshed.
* <p>The default implementation is empty but can be overridden in subclasses * <p>The default implementation is empty but can be overridden in subclasses
* to customize the application context. * to customize the application context.
* @param context the newly created application context * @param context the newly created application context
* @see #loadContext(String...) * @see #loadContext
*/ */
protected void customizeContext(GenericApplicationContext context) { protected void customizeContext(GenericApplicationContext context) {
} }

View File

@ -21,12 +21,10 @@ import java.util.List;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.context.support.GenericApplicationContext; import org.springframework.context.support.GenericApplicationContext;
import org.springframework.util.ObjectUtils; import org.springframework.util.Assert;
/** /**
* TODO Document AnnotationConfigContextLoader. * TODO Document AnnotationConfigContextLoader.
@ -34,99 +32,61 @@ import org.springframework.util.ObjectUtils;
* @author Sam Brannen * @author Sam Brannen
* @since 3.1 * @since 3.1
*/ */
public class AnnotationConfigContextLoader extends AbstractContextLoader { public class AnnotationConfigContextLoader extends AbstractGenericContextLoader {
private static final Log logger = LogFactory.getLog(AnnotationConfigContextLoader.class); private static final Log logger = LogFactory.getLog(AnnotationConfigContextLoader.class);
/** /**
* TODO Document loadContext(). * Creates a new {@link AnnotationConfigApplicationContext}.
*
* @see org.springframework.test.context.ContextLoader#loadContext(java.lang.String[])
*/ */
public ApplicationContext loadContext(String... locations) throws Exception { @Override
if (logger.isDebugEnabled()) { protected GenericApplicationContext createGenericApplicationContext() {
logger.debug("Creating an AnnotationConfigApplicationContext for " return new AnnotationConfigApplicationContext();
+ ObjectUtils.nullSafeToString(locations));
} }
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); /**
prepareContext(context); * TODO Document overridden loadBeanDefinitions().
customizeBeanFactory(context.getDefaultListableBeanFactory()); */
@Override
protected void loadBeanDefinitions(GenericApplicationContext context, String... locations) {
Assert.isInstanceOf(AnnotationConfigApplicationContext.class, context,
"context must be an instance of AnnotationConfigApplicationContext");
AnnotationConfigApplicationContext annotationConfigApplicationContext = (AnnotationConfigApplicationContext) context;
List<Class<?>> configClasses = new ArrayList<Class<?>>(); List<Class<?>> configClasses = new ArrayList<Class<?>>();
for (String location : locations) { for (String location : locations) {
final Class<?> clazz = getClass().getClassLoader().loadClass(location); try {
Class<?> clazz = getClass().getClassLoader().loadClass(location);
configClasses.add(clazz); configClasses.add(clazz);
} }
catch (ClassNotFoundException e) {
throw new IllegalArgumentException(String.format(
"The supplied resource location [%s] does not represent a class.", location), e);
}
}
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("Loading AnnotationConfigApplicationContext from config classes: " + configClasses); logger.debug("Registering configuration classes: " + configClasses);
} }
for (Class<?> configClass : configClasses) { for (Class<?> configClass : configClasses) {
context.register(configClass); annotationConfigApplicationContext.register(configClass);
} }
AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
customizeContext(context);
context.refresh();
context.registerShutdownHook();
return context;
} }
/** /**
* Prepare the {@link GenericApplicationContext} created by this * Returns <code>null</code>; intended as a <em>no-op</em> operation.
* ContextLoader. Called <i>before</> bean definitions are read.
* <p>
* The default implementation is empty. Can be overridden in subclasses to
* customize GenericApplicationContext's standard settings.
*
* @param context the context for which the BeanDefinitionReader should be
* created
* @see #loadContext
* @see org.springframework.context.support.GenericApplicationContext#setResourceLoader
* @see org.springframework.context.support.GenericApplicationContext#setId
*/ */
protected void prepareContext(GenericApplicationContext context) { @Override
} protected BeanDefinitionReader createBeanDefinitionReader(GenericApplicationContext context) {
return null;
/**
* Customize the internal bean factory of the ApplicationContext created by
* this ContextLoader.
* <p>
* The default implementation is empty but can be overridden in subclasses
* to customize DefaultListableBeanFactory's standard settings.
*
* @param beanFactory the bean factory created by this ContextLoader
* @see #loadContext
* @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowBeanDefinitionOverriding(boolean)
* @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowEagerClassLoading(boolean)
* @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowCircularReferences(boolean)
* @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowRawInjectionDespiteWrapping(boolean)
*/
protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
}
/**
* Customize the {@link GenericApplicationContext} created by this
* ContextLoader <i>after</i> bean definitions have been loaded into the
* context but before the context is refreshed.
* <p>
* The default implementation is empty but can be overridden in subclasses
* to customize the application context.
*
* @param context the newly created application context
* @see #loadContext(String...)
*/
protected void customizeContext(GenericApplicationContext context) {
} }
/** /**
* TODO Document overridden generateDefaultLocations(). * TODO Document overridden generateDefaultLocations().
*
* @see org.springframework.test.context.support.AbstractContextLoader#generateDefaultLocations(java.lang.Class)
*/ */
@Override @Override
protected String[] generateDefaultLocations(Class<?> clazz) { protected String[] generateDefaultLocations(Class<?> clazz) {
@ -135,21 +95,15 @@ public class AnnotationConfigContextLoader extends AbstractContextLoader {
} }
/** /**
* TODO Document modifyLocations(). * Returns the supplied <code>locations</code> unmodified.
*
* @see org.springframework.test.context.support.AbstractContextLoader#modifyLocations(java.lang.Class,
* java.lang.String[])
*/ */
@Override @Override
protected String[] modifyLocations(Class<?> clazz, String... locations) { protected String[] modifyLocations(Class<?> clazz, String... locations) {
// TODO Implement modifyLocations() (?).
return locations; return locations;
} }
/** /**
* TODO Document getResourceSuffix(). * Returns &quot;Config</code>&quot;.
*
* @see org.springframework.test.context.support.AbstractContextLoader#getResourceSuffix()
*/ */
@Override @Override
protected String getResourceSuffix() { protected String getResourceSuffix() {

View File

@ -48,6 +48,7 @@ import org.springframework.test.context.junit4.orm.HibernateSessionFlushingTests
StandardJUnit4FeaturesTests.class,// StandardJUnit4FeaturesTests.class,//
StandardJUnit4FeaturesSpringRunnerTests.class,// StandardJUnit4FeaturesSpringRunnerTests.class,//
SpringJUnit47ClassRunnerRuleTests.class,// SpringJUnit47ClassRunnerRuleTests.class,//
AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests.class,//
ExpectedExceptionSpringRunnerTests.class,// ExpectedExceptionSpringRunnerTests.class,//
TimedSpringRunnerTests.class,// TimedSpringRunnerTests.class,//
RepeatedSpringRunnerTests.class,// RepeatedSpringRunnerTests.class,//