|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2014 the original author or authors. |
|
|
|
* Copyright 2002-2020 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. |
|
|
|
@ -64,9 +64,7 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Load a Spring ApplicationContext from the supplied {@link MergedContextConfiguration}. |
|
|
|
* Load a Spring ApplicationContext from the supplied {@link MergedContextConfiguration}. |
|
|
|
* |
|
|
|
|
|
|
|
* <p>Implementation details: |
|
|
|
* <p>Implementation details: |
|
|
|
* |
|
|
|
|
|
|
|
* <ul> |
|
|
|
* <ul> |
|
|
|
* <li>Calls {@link #validateMergedContextConfiguration(MergedContextConfiguration)} |
|
|
|
* <li>Calls {@link #validateMergedContextConfiguration(MergedContextConfiguration)} |
|
|
|
* to allow subclasses to validate the supplied configuration before proceeding.</li> |
|
|
|
* to allow subclasses to validate the supplied configuration before proceeding.</li> |
|
|
|
@ -97,7 +95,6 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader |
|
|
|
* <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> |
|
|
|
* |
|
|
|
|
|
|
|
* @return a new application context |
|
|
|
* @return a new application context |
|
|
|
* @see org.springframework.test.context.SmartContextLoader#loadContext(MergedContextConfiguration) |
|
|
|
* @see org.springframework.test.context.SmartContextLoader#loadContext(MergedContextConfiguration) |
|
|
|
* @see GenericApplicationContext |
|
|
|
* @see GenericApplicationContext |
|
|
|
@ -107,7 +104,7 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader |
|
|
|
public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception { |
|
|
|
public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception { |
|
|
|
if (logger.isDebugEnabled()) { |
|
|
|
if (logger.isDebugEnabled()) { |
|
|
|
logger.debug(String.format("Loading ApplicationContext for merged context configuration [%s].", |
|
|
|
logger.debug(String.format("Loading ApplicationContext for merged context configuration [%s].", |
|
|
|
mergedConfig)); |
|
|
|
mergedConfig)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
validateMergedContextConfiguration(mergedConfig); |
|
|
|
validateMergedContextConfiguration(mergedConfig); |
|
|
|
@ -118,6 +115,7 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader |
|
|
|
if (parent != null) { |
|
|
|
if (parent != null) { |
|
|
|
context.setParent(parent); |
|
|
|
context.setParent(parent); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
prepareContext(context); |
|
|
|
prepareContext(context); |
|
|
|
prepareContext(context, mergedConfig); |
|
|
|
prepareContext(context, mergedConfig); |
|
|
|
customizeBeanFactory(context.getDefaultListableBeanFactory()); |
|
|
|
customizeBeanFactory(context.getDefaultListableBeanFactory()); |
|
|
|
@ -125,8 +123,10 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader |
|
|
|
AnnotationConfigUtils.registerAnnotationConfigProcessors(context); |
|
|
|
AnnotationConfigUtils.registerAnnotationConfigProcessors(context); |
|
|
|
customizeContext(context); |
|
|
|
customizeContext(context); |
|
|
|
customizeContext(context, mergedConfig); |
|
|
|
customizeContext(context, mergedConfig); |
|
|
|
|
|
|
|
|
|
|
|
context.refresh(); |
|
|
|
context.refresh(); |
|
|
|
context.registerShutdownHook(); |
|
|
|
context.registerShutdownHook(); |
|
|
|
|
|
|
|
|
|
|
|
return context; |
|
|
|
return context; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -146,9 +146,7 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Load a Spring ApplicationContext from the supplied {@code locations}. |
|
|
|
* Load a Spring ApplicationContext from the supplied {@code locations}. |
|
|
|
* |
|
|
|
|
|
|
|
* <p>Implementation details: |
|
|
|
* <p>Implementation details: |
|
|
|
* |
|
|
|
|
|
|
|
* <ul> |
|
|
|
* <ul> |
|
|
|
* <li>Creates a {@link GenericApplicationContext} instance.</li> |
|
|
|
* <li>Creates a {@link GenericApplicationContext} instance.</li> |
|
|
|
* <li>Calls {@link #prepareContext(GenericApplicationContext)} to allow for customizing the context |
|
|
|
* <li>Calls {@link #prepareContext(GenericApplicationContext)} to allow for customizing the context |
|
|
|
@ -166,12 +164,10 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader |
|
|
|
* <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><b>Note</b>: this method does not provide a means to set active bean definition |
|
|
|
* <p><b>Note</b>: this method does not provide a means to set active bean definition |
|
|
|
* profiles for the loaded context. See {@link #loadContext(MergedContextConfiguration)} |
|
|
|
* profiles for the loaded context. See {@link #loadContext(MergedContextConfiguration)} |
|
|
|
* and {@link AbstractContextLoader#prepareContext(ConfigurableApplicationContext, MergedContextConfiguration)} |
|
|
|
* and {@link AbstractContextLoader#prepareContext(ConfigurableApplicationContext, MergedContextConfiguration)} |
|
|
|
* for an alternative. |
|
|
|
* for an alternative. |
|
|
|
* |
|
|
|
|
|
|
|
* @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 |
|
|
|
@ -182,26 +178,28 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader |
|
|
|
public final ConfigurableApplicationContext loadContext(String... locations) throws Exception { |
|
|
|
public final ConfigurableApplicationContext loadContext(String... locations) throws Exception { |
|
|
|
if (logger.isDebugEnabled()) { |
|
|
|
if (logger.isDebugEnabled()) { |
|
|
|
logger.debug(String.format("Loading ApplicationContext for locations [%s].", |
|
|
|
logger.debug(String.format("Loading ApplicationContext for locations [%s].", |
|
|
|
StringUtils.arrayToCommaDelimitedString(locations))); |
|
|
|
StringUtils.arrayToCommaDelimitedString(locations))); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
GenericApplicationContext context = new GenericApplicationContext(); |
|
|
|
GenericApplicationContext context = new GenericApplicationContext(); |
|
|
|
|
|
|
|
|
|
|
|
prepareContext(context); |
|
|
|
prepareContext(context); |
|
|
|
customizeBeanFactory(context.getDefaultListableBeanFactory()); |
|
|
|
customizeBeanFactory(context.getDefaultListableBeanFactory()); |
|
|
|
createBeanDefinitionReader(context).loadBeanDefinitions(locations); |
|
|
|
createBeanDefinitionReader(context).loadBeanDefinitions(locations); |
|
|
|
AnnotationConfigUtils.registerAnnotationConfigProcessors(context); |
|
|
|
AnnotationConfigUtils.registerAnnotationConfigProcessors(context); |
|
|
|
customizeContext(context); |
|
|
|
customizeContext(context); |
|
|
|
|
|
|
|
|
|
|
|
context.refresh(); |
|
|
|
context.refresh(); |
|
|
|
context.registerShutdownHook(); |
|
|
|
context.registerShutdownHook(); |
|
|
|
|
|
|
|
|
|
|
|
return context; |
|
|
|
return context; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Prepare the {@link GenericApplicationContext} created by this {@code ContextLoader}. |
|
|
|
* Prepare the {@link GenericApplicationContext} created by this {@code ContextLoader}. |
|
|
|
* Called <i>before</i> bean definitions are read. |
|
|
|
* Called <i>before</i> 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 {@code GenericApplicationContext}'s standard settings. |
|
|
|
* customize {@code GenericApplicationContext}'s standard settings. |
|
|
|
* |
|
|
|
|
|
|
|
* @param context the context that should be prepared |
|
|
|
* @param context the context that should be prepared |
|
|
|
* @see #loadContext(MergedContextConfiguration) |
|
|
|
* @see #loadContext(MergedContextConfiguration) |
|
|
|
* @see #loadContext(String...) |
|
|
|
* @see #loadContext(String...) |
|
|
|
@ -217,10 +215,8 @@ 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}. |
|
|
|
* this {@code 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 {@code DefaultListableBeanFactory}'s standard settings. |
|
|
|
* to customize {@code DefaultListableBeanFactory}'s standard settings. |
|
|
|
* |
|
|
|
|
|
|
|
* @param beanFactory the bean factory created by this {@code ContextLoader} |
|
|
|
* @param beanFactory the bean factory created by this {@code ContextLoader} |
|
|
|
* @see #loadContext(MergedContextConfiguration) |
|
|
|
* @see #loadContext(MergedContextConfiguration) |
|
|
|
* @see #loadContext(String...) |
|
|
|
* @see #loadContext(String...) |
|
|
|
@ -236,18 +232,15 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Load bean definitions into the supplied {@link GenericApplicationContext context} |
|
|
|
* Load bean definitions into the supplied {@link GenericApplicationContext context} |
|
|
|
* from the locations or classes in the supplied {@code MergedContextConfiguration}. |
|
|
|
* from the locations or classes in the supplied {@code MergedContextConfiguration}. |
|
|
|
* |
|
|
|
|
|
|
|
* <p>The default implementation delegates to the {@link BeanDefinitionReader} |
|
|
|
* <p>The default implementation delegates to the {@link BeanDefinitionReader} |
|
|
|
* returned by {@link #createBeanDefinitionReader(GenericApplicationContext)} to |
|
|
|
* returned by {@link #createBeanDefinitionReader(GenericApplicationContext)} to |
|
|
|
* {@link BeanDefinitionReader#loadBeanDefinitions(String) load} the |
|
|
|
* {@link BeanDefinitionReader#loadBeanDefinitions(String) load} the |
|
|
|
* bean definitions. |
|
|
|
* bean definitions. |
|
|
|
* |
|
|
|
|
|
|
|
* <p>Subclasses must provide an appropriate implementation of |
|
|
|
* <p>Subclasses must provide an appropriate implementation of |
|
|
|
* {@link #createBeanDefinitionReader(GenericApplicationContext)}. Alternatively subclasses |
|
|
|
* {@link #createBeanDefinitionReader(GenericApplicationContext)}. Alternatively subclasses |
|
|
|
* may provide a <em>no-op</em> implementation of {@code createBeanDefinitionReader()} |
|
|
|
* may provide a <em>no-op</em> implementation of {@code createBeanDefinitionReader()} |
|
|
|
* and override this method to provide a custom strategy for loading or |
|
|
|
* and override this method to provide a custom strategy for loading or |
|
|
|
* registering bean definitions. |
|
|
|
* registering bean definitions. |
|
|
|
* |
|
|
|
|
|
|
|
* @param context the context into which the bean definitions should be loaded |
|
|
|
* @param context the context into which the bean definitions should be loaded |
|
|
|
* @param mergedConfig the merged context configuration |
|
|
|
* @param mergedConfig the merged context configuration |
|
|
|
* @see #loadContext(MergedContextConfiguration) |
|
|
|
* @see #loadContext(MergedContextConfiguration) |
|
|
|
@ -260,7 +253,6 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Factory method for creating a new {@link BeanDefinitionReader} for loading |
|
|
|
* Factory method for creating a new {@link BeanDefinitionReader} for loading |
|
|
|
* bean definitions into the supplied {@link GenericApplicationContext context}. |
|
|
|
* bean definitions into the supplied {@link GenericApplicationContext context}. |
|
|
|
* |
|
|
|
|
|
|
|
* @param context the context for which the {@code BeanDefinitionReader} |
|
|
|
* @param context the context for which the {@code BeanDefinitionReader} |
|
|
|
* should be created |
|
|
|
* should be created |
|
|
|
* @return a {@code BeanDefinitionReader} for the supplied context |
|
|
|
* @return a {@code BeanDefinitionReader} for the supplied context |
|
|
|
@ -275,10 +267,8 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader |
|
|
|
* Customize the {@link GenericApplicationContext} created by this |
|
|
|
* Customize the {@link GenericApplicationContext} created by this |
|
|
|
* {@code ContextLoader} <i>after</i> bean definitions have been |
|
|
|
* {@code ContextLoader} <i>after</i> bean definitions have been |
|
|
|
* loaded into the context but <i>before</i> the context is refreshed. |
|
|
|
* loaded into the context but <i>before</i> 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(MergedContextConfiguration) |
|
|
|
* @see #loadContext(MergedContextConfiguration) |
|
|
|
* @see #loadContext(String...) |
|
|
|
* @see #loadContext(String...) |
|
|
|
|