类 InfraApplicationContextLoader
- 所有已实现的接口:
ContextLoader,SmartContextLoader
ContextLoader that can be used to test Infra applications (those that
normally startup using Application). Although this loader can be used
directly, most test will instead want to use it with
@InfraTest.
The loader supports both standard MergedContextConfiguration as well as
WebMergedContextConfiguration. If WebMergedContextConfiguration is used
the context will either use a mock servlet environment, or start the full embedded web
server.
If @ActiveProfiles are provided in the test class they will be used to create
the application context.
- 从以下版本开始:
- 4.0
- 作者:
- Dave Syer, Phillip Webb, Andy Wilkinson, Stephane Nicoll, Madhura Bhave, Scott Frederick, Harry Yang
- 另请参阅:
-
嵌套类概要
嵌套类修饰符和类型类说明private static classAdapts aContextCustomizerto aApplicationContextInitializerso that it can be triggered viaApplication.private static classApplicationContextInitializerused to set the parent context.private classApplicationListenerused to prepare the application created environment.private static classInner class to configureWebMergedContextConfiguration. -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明protected Class<?>[]detectDefaultConfigurationClasses(Class<?> declaringClass) Detect the default configuration classes for the supplied test class.protected cn.taketoday.framework.ApplicationBuilds newApplicationinstance.protected cn.taketoday.core.env.ConfigurableEnvironmentReturns theConfigurableEnvironmentinstance that should be applied toApplicationornullto use the default.protected List<cn.taketoday.context.ApplicationContextInitializer>getInitializers(MergedContextConfiguration config, cn.taketoday.framework.Application application) Return theinitializersthat will be applied to the context.protected String[]protected StringGet the suffix to append toApplicationContextresource locations when detecting default locations.protected String[]Get the suffixes to append toApplicationContextresource locations when detecting default locations.private booleancn.taketoday.context.ApplicationContextLoad a newApplicationContextbased on the suppliedMergedContextConfiguration, configure the context, and return the context in a fully refreshed state.cn.taketoday.context.ApplicationContextloadContext(String... locations) SmartContextLoaderdoes not support deprecatedContextLoadermethods.private voidprepareEnvironment(MergedContextConfiguration config, cn.taketoday.framework.Application application, cn.taketoday.core.env.ConfigurableEnvironment environment, boolean applicationEnvironment) voidprocessContextConfiguration(ContextConfigurationAttributes configAttributes) The default implementation processes locations analogous toAbstractContextLoader.processLocations(Class, String...), using thedeclaring classas the test class and theresource locationsretrieved from the suppliedconfiguration attributesas the locations to process.private voidsetActiveProfiles(cn.taketoday.core.env.ConfigurableEnvironment environment, String[] profiles, boolean applicationEnvironment) 从类继承的方法 cn.taketoday.test.context.support.AbstractContextLoader
customizeContext, generateDefaultLocations, isGenerateDefaultLocations, modifyLocations, prepareContext, processLocations
-
构造器详细资料
-
InfraApplicationContextLoader
public InfraApplicationContextLoader()
-
-
方法详细资料
-
loadContext
public cn.taketoday.context.ApplicationContext loadContext(MergedContextConfiguration config) throws Exception 从接口复制的说明:SmartContextLoaderLoad a newApplicationContextbased on the suppliedMergedContextConfiguration, configure the context, and return the context in a fully refreshed state.Concrete implementations should register annotation configuration processors with bean factories of
application contextsloaded by thisSmartContextLoader. Beans will therefore automatically be candidates for annotation-based dependency injection using@Autowired,@Resource, and@Inject. In addition, concrete implementations should perform the following actions.- Set the parent
ApplicationContextif appropriate (seeMergedContextConfiguration.getParent()). - Set the active bean definition profiles in the context's
Environment(seeMergedContextConfiguration.getActiveProfiles()). - Add test
PropertySourcesto theEnvironment(seeMergedContextConfiguration.getPropertySourceLocations(),MergedContextConfiguration.getPropertySourceProperties(), andTestPropertySourceUtils). - Invoke
ApplicationContextInitializers(seeMergedContextConfiguration.getContextInitializerClasses()). - Invoke
ContextCustomizers(seeMergedContextConfiguration.getContextCustomizers()). - Register a JVM shutdown hook for the
ApplicationContext. Unless the context gets closed early, all context instances will be automatically closed on JVM shutdown. This allows for freeing of external resources held by beans within the context — for example, temporary files.
any exception thrown while attempting to load an
ApplicationContextshould be wrapped in aContextLoadException. Concrete implementations should therefore contain a try-catch block similar to the following.ApplicationContext context = // create context try { // configure and refresh context } catch (Exception ex) { throw new ContextLoadException(context, ex); }- 参数:
config- the merged context configuration to use to load the application context- 返回:
- a new application context
- 抛出:
ContextLoadException- if context loading failedException- 另请参阅:
-
SmartContextLoader.processContextConfiguration(ContextConfigurationAttributes)AnnotationConfigUtils.registerAnnotationConfigProcessors(cn.taketoday.beans.factory.support.BeanDefinitionRegistry)ConfigurableApplicationContext.getEnvironment()
- Set the parent
-
prepareEnvironment
private void prepareEnvironment(MergedContextConfiguration config, cn.taketoday.framework.Application application, cn.taketoday.core.env.ConfigurableEnvironment environment, boolean applicationEnvironment) -
setActiveProfiles
private void setActiveProfiles(cn.taketoday.core.env.ConfigurableEnvironment environment, String[] profiles, boolean applicationEnvironment) -
getApplication
protected cn.taketoday.framework.Application getApplication()Builds newApplicationinstance. You can override this method to add custom behavior- 返回:
Applicationinstance
-
getEnvironment
protected cn.taketoday.core.env.ConfigurableEnvironment getEnvironment()Returns theConfigurableEnvironmentinstance that should be applied toApplicationornullto use the default. You can override this method if you need a custom environment.- 返回:
- a
ConfigurableEnvironmentinstance
-
getInlinedProperties
-
getInitializers
protected List<cn.taketoday.context.ApplicationContextInitializer> getInitializers(MergedContextConfiguration config, cn.taketoday.framework.Application application) Return theinitializersthat will be applied to the context. By default this method will adaptcontext customizers, addapplication initializersand addinitializers specified on the test.- 参数:
config- the source context configurationapplication- the application instance- 返回:
- the initializers to apply
-
isEmbeddedWebEnvironment
-
processContextConfiguration
从类复制的说明:AbstractContextLoaderThe default implementation processes locations analogous toAbstractContextLoader.processLocations(Class, String...), using thedeclaring classas the test class and theresource locationsretrieved from the suppliedconfiguration attributesas the locations to process. The processed locations are thensetin the supplied configuration attributes.Can be overridden in subclasses — for example, to process annotated classes instead of resource locations.
- 指定者:
processContextConfiguration在接口中SmartContextLoader- 覆盖:
processContextConfiguration在类中AbstractContextLoader- 参数:
configAttributes- the context configuration attributes to process- 另请参阅:
-
detectDefaultConfigurationClasses
Detect the default configuration classes for the supplied test class. By default simply delegates toAnnotationConfigContextLoaderUtils.detectDefaultConfigurationClasses(java.lang.Class<?>).- 参数:
declaringClass- the test class that declared@ContextConfiguration- 返回:
- an array of default configuration classes, potentially empty but never
null - 另请参阅:
-
loadContext
从接口复制的说明:SmartContextLoaderSmartContextLoaderdoes not support deprecatedContextLoadermethods.Call
SmartContextLoader.loadContext(MergedContextConfiguration)instead.- 参数:
locations- the resource locations to use to load the application context- 返回:
- a new application context
- 抛出:
Exception- if context loading failed
-
getResourceSuffixes
从类复制的说明:AbstractContextLoaderGet the suffixes to append toApplicationContextresource locations when detecting default locations.The default implementation simply wraps the value returned by
AbstractContextLoader.getResourceSuffix()in a single-element array, but this can be overridden by subclasses in order to support multiple suffixes.- 覆盖:
getResourceSuffixes在类中AbstractContextLoader- 返回:
- the resource suffixes; never
nullor empty - 另请参阅:
-
getResourceSuffix
从类复制的说明:AbstractContextLoaderGet the suffix to append toApplicationContextresource locations when detecting default locations.Subclasses must provide an implementation of this method that returns a single suffix. Alternatively subclasses may provide a no-op implementation of this method and override
AbstractContextLoader.getResourceSuffixes()in order to provide multiple custom suffixes.- 指定者:
getResourceSuffix在类中AbstractContextLoader- 返回:
- the resource suffix; never
nullor empty - 另请参阅:
-