Java Code Examples for org.springframework.web.context.support.GenericWebApplicationContext#setParent()

The following examples show how to use org.springframework.web.context.support.GenericWebApplicationContext#setParent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XmlViewResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Initialize the view bean factory from the XML file.
 * Synchronized because of access by parallel threads.
 * @throws BeansException in case of initialization errors
 */
protected synchronized BeanFactory initFactory() throws BeansException {
	if (this.cachedFactory != null) {
		return this.cachedFactory;
	}

	ApplicationContext applicationContext = obtainApplicationContext();

	Resource actualLocation = this.location;
	if (actualLocation == null) {
		actualLocation = applicationContext.getResource(DEFAULT_LOCATION);
	}

	// Create child ApplicationContext for views.
	GenericWebApplicationContext factory = new GenericWebApplicationContext();
	factory.setParent(applicationContext);
	factory.setServletContext(getServletContext());

	// Load XML resource with context-aware entity resolver.
	XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
	reader.setEnvironment(applicationContext.getEnvironment());
	reader.setEntityResolver(new ResourceEntityResolver(applicationContext));
	reader.loadBeanDefinitions(actualLocation);

	factory.refresh();

	if (isCache()) {
		this.cachedFactory = factory;
	}
	return factory;
}
 
Example 2
Source File: XmlViewResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Initialize the view bean factory from the XML file.
 * Synchronized because of access by parallel threads.
 * @throws BeansException in case of initialization errors
 */
protected synchronized BeanFactory initFactory() throws BeansException {
	if (this.cachedFactory != null) {
		return this.cachedFactory;
	}

	ApplicationContext applicationContext = obtainApplicationContext();

	Resource actualLocation = this.location;
	if (actualLocation == null) {
		actualLocation = applicationContext.getResource(DEFAULT_LOCATION);
	}

	// Create child ApplicationContext for views.
	GenericWebApplicationContext factory = new GenericWebApplicationContext();
	factory.setParent(applicationContext);
	factory.setServletContext(getServletContext());

	// Load XML resource with context-aware entity resolver.
	XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
	reader.setEnvironment(applicationContext.getEnvironment());
	reader.setEntityResolver(new ResourceEntityResolver(applicationContext));
	reader.loadBeanDefinitions(actualLocation);

	factory.refresh();

	if (isCache()) {
		this.cachedFactory = factory;
	}
	return factory;
}
 
Example 3
Source File: XmlViewResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Initialize the view bean factory from the XML file.
 * Synchronized because of access by parallel threads.
 * @throws BeansException in case of initialization errors
 */
protected synchronized BeanFactory initFactory() throws BeansException {
	if (this.cachedFactory != null) {
		return this.cachedFactory;
	}

	Resource actualLocation = this.location;
	if (actualLocation == null) {
		actualLocation = getApplicationContext().getResource(DEFAULT_LOCATION);
	}

	// Create child ApplicationContext for views.
	GenericWebApplicationContext factory = new GenericWebApplicationContext();
	factory.setParent(getApplicationContext());
	factory.setServletContext(getServletContext());

	// Load XML resource with context-aware entity resolver.
	XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
	reader.setEnvironment(getApplicationContext().getEnvironment());
	reader.setEntityResolver(new ResourceEntityResolver(getApplicationContext()));
	reader.loadBeanDefinitions(actualLocation);

	factory.refresh();

	if (isCache()) {
		this.cachedFactory = factory;
	}
	return factory;
}
 
Example 4
Source File: AbstractJettyComponent.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected WebApplicationContext createWebApplicationContext(ServletContext sc, ApplicationContext parent)
{
	GenericWebApplicationContext wac = (GenericWebApplicationContext) BeanUtils.instantiateClass(GenericWebApplicationContext.class);

	// Assign the best possible id value.
	wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + contextPath);

	wac.setParent(parent);
	wac.setServletContext(sc);
	wac.refresh();

	return wac;
}
 
Example 5
Source File: XmlViewResolver.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize the view bean factory from the XML file.
 * Synchronized because of access by parallel threads.
 * @throws BeansException in case of initialization errors
 */
protected synchronized BeanFactory initFactory() throws BeansException {
	if (this.cachedFactory != null) {
		return this.cachedFactory;
	}

	Resource actualLocation = this.location;
	if (actualLocation == null) {
		actualLocation = getApplicationContext().getResource(DEFAULT_LOCATION);
	}

	// Create child ApplicationContext for views.
	GenericWebApplicationContext factory = new GenericWebApplicationContext();
	factory.setParent(getApplicationContext());
	factory.setServletContext(getServletContext());

	// Load XML resource with context-aware entity resolver.
	XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
	reader.setEnvironment(getApplicationContext().getEnvironment());
	reader.setEntityResolver(new ResourceEntityResolver(getApplicationContext()));
	reader.loadBeanDefinitions(actualLocation);

	factory.refresh();

	if (isCache()) {
		this.cachedFactory = factory;
	}
	return factory;
}
 
Example 6
Source File: AbstractGenericWebContextLoader.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * Load a Spring {@link WebApplicationContext} from the supplied
 * {@link MergedContextConfiguration}.
 * <p>Implementation details:
 * <ul>
 * <li>Calls {@link #validateMergedContextConfiguration(WebMergedContextConfiguration)}
 * to allow subclasses to validate the supplied configuration before proceeding.</li>
 * <li>Creates a {@link GenericWebApplicationContext} instance.</li>
 * <li>If the supplied {@code MergedContextConfiguration} references a
 * {@linkplain MergedContextConfiguration#getParent() parent configuration},
 * the corresponding {@link MergedContextConfiguration#getParentApplicationContext()
 * ApplicationContext} will be retrieved and
 * {@linkplain GenericWebApplicationContext#setParent(ApplicationContext) set as the parent}
 * for the context created by this method.</li>
 * <li>Delegates to {@link #configureWebResources} to create the
 * {@link MockServletContext} and set it in the {@code WebApplicationContext}.</li>
 * <li>Calls {@link #prepareContext} to allow for customizing the context
 * before bean definitions are loaded.</li>
 * <li>Calls {@link #customizeBeanFactory} to allow for customizing the
 * context's {@code DefaultListableBeanFactory}.</li>
 * <li>Delegates to {@link #loadBeanDefinitions} to populate the context
 * from the locations or classes in the supplied {@code MergedContextConfiguration}.</li>
 * <li>Delegates to {@link AnnotationConfigUtils} for
 * {@linkplain AnnotationConfigUtils#registerAnnotationConfigProcessors registering}
 * annotation configuration processors.</li>
 * <li>Calls {@link #customizeContext} to allow for customizing the context
 * before it is refreshed.</li>
 * <li>{@link ConfigurableApplicationContext#refresh Refreshes} the
 * context and registers a JVM shutdown hook for it.</li>
 * </ul>
 * @return a new web application context
 * @see org.springframework.test.context.SmartContextLoader#loadContext(MergedContextConfiguration)
 * @see GenericWebApplicationContext
 */
@Override
public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception {
	Assert.isTrue(mergedConfig instanceof WebMergedContextConfiguration,
			() -> String.format("Cannot load WebApplicationContext from non-web merged context configuration %s. " +
					"Consider annotating your test class with @WebAppConfiguration.", mergedConfig));

	WebMergedContextConfiguration webMergedConfig = (WebMergedContextConfiguration) mergedConfig;

	if (logger.isDebugEnabled()) {
		logger.debug(String.format("Loading WebApplicationContext for merged context configuration %s.",
			webMergedConfig));
	}

	validateMergedContextConfiguration(webMergedConfig);

	GenericWebApplicationContext context = new GenericWebApplicationContext();

	ApplicationContext parent = mergedConfig.getParentApplicationContext();
	if (parent != null) {
		context.setParent(parent);
	}
	configureWebResources(context, webMergedConfig);
	prepareContext(context, webMergedConfig);
	customizeBeanFactory(context.getDefaultListableBeanFactory(), webMergedConfig);
	loadBeanDefinitions(context, webMergedConfig);
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	customizeContext(context, webMergedConfig);
	context.refresh();
	context.registerShutdownHook();
	return context;
}
 
Example 7
Source File: AbstractGenericWebContextLoader.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * Load a Spring {@link WebApplicationContext} from the supplied
 * {@link MergedContextConfiguration}.
 * <p>Implementation details:
 * <ul>
 * <li>Calls {@link #validateMergedContextConfiguration(WebMergedContextConfiguration)}
 * to allow subclasses to validate the supplied configuration before proceeding.</li>
 * <li>Creates a {@link GenericWebApplicationContext} instance.</li>
 * <li>If the supplied {@code MergedContextConfiguration} references a
 * {@linkplain MergedContextConfiguration#getParent() parent configuration},
 * the corresponding {@link MergedContextConfiguration#getParentApplicationContext()
 * ApplicationContext} will be retrieved and
 * {@linkplain GenericWebApplicationContext#setParent(ApplicationContext) set as the parent}
 * for the context created by this method.</li>
 * <li>Delegates to {@link #configureWebResources} to create the
 * {@link MockServletContext} and set it in the {@code WebApplicationContext}.</li>
 * <li>Calls {@link #prepareContext} to allow for customizing the context
 * before bean definitions are loaded.</li>
 * <li>Calls {@link #customizeBeanFactory} to allow for customizing the
 * context's {@code DefaultListableBeanFactory}.</li>
 * <li>Delegates to {@link #loadBeanDefinitions} to populate the context
 * from the locations or classes in the supplied {@code MergedContextConfiguration}.</li>
 * <li>Delegates to {@link AnnotationConfigUtils} for
 * {@linkplain AnnotationConfigUtils#registerAnnotationConfigProcessors registering}
 * annotation configuration processors.</li>
 * <li>Calls {@link #customizeContext} to allow for customizing the context
 * before it is refreshed.</li>
 * <li>{@link ConfigurableApplicationContext#refresh Refreshes} the
 * context and registers a JVM shutdown hook for it.</li>
 * </ul>
 * @return a new web application context
 * @see org.springframework.test.context.SmartContextLoader#loadContext(MergedContextConfiguration)
 * @see GenericWebApplicationContext
 */
@Override
public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception {
	Assert.isTrue(mergedConfig instanceof WebMergedContextConfiguration,
			() -> String.format("Cannot load WebApplicationContext from non-web merged context configuration %s. " +
					"Consider annotating your test class with @WebAppConfiguration.", mergedConfig));

	WebMergedContextConfiguration webMergedConfig = (WebMergedContextConfiguration) mergedConfig;

	if (logger.isDebugEnabled()) {
		logger.debug(String.format("Loading WebApplicationContext for merged context configuration %s.",
			webMergedConfig));
	}

	validateMergedContextConfiguration(webMergedConfig);

	GenericWebApplicationContext context = new GenericWebApplicationContext();

	ApplicationContext parent = mergedConfig.getParentApplicationContext();
	if (parent != null) {
		context.setParent(parent);
	}
	configureWebResources(context, webMergedConfig);
	prepareContext(context, webMergedConfig);
	customizeBeanFactory(context.getDefaultListableBeanFactory(), webMergedConfig);
	loadBeanDefinitions(context, webMergedConfig);
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	customizeContext(context, webMergedConfig);
	context.refresh();
	context.registerShutdownHook();
	return context;
}
 
Example 8
Source File: AbstractGenericWebContextLoader.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * Load a Spring {@link WebApplicationContext} from the supplied
 * {@link MergedContextConfiguration}.
 *
 * <p>Implementation details:
 *
 * <ul>
 * <li>Calls {@link #validateMergedContextConfiguration(WebMergedContextConfiguration)}
 * to allow subclasses to validate the supplied configuration before proceeding.</li>
 * <li>Creates a {@link GenericWebApplicationContext} instance.</li>
 * <li>If the supplied {@code MergedContextConfiguration} references a
 * {@linkplain MergedContextConfiguration#getParent() parent configuration},
 * the corresponding {@link MergedContextConfiguration#getParentApplicationContext()
 * ApplicationContext} will be retrieved and
 * {@linkplain GenericWebApplicationContext#setParent(ApplicationContext) set as the parent}
 * for the context created by this method.</li>
 * <li>Delegates to {@link #configureWebResources} to create the
 * {@link MockServletContext} and set it in the {@code WebApplicationContext}.</li>
 * <li>Calls {@link #prepareContext} to allow for customizing the context
 * before bean definitions are loaded.</li>
 * <li>Calls {@link #customizeBeanFactory} to allow for customizing the
 * context's {@code DefaultListableBeanFactory}.</li>
 * <li>Delegates to {@link #loadBeanDefinitions} to populate the context
 * from the locations or classes in the supplied {@code MergedContextConfiguration}.</li>
 * <li>Delegates to {@link AnnotationConfigUtils} for
 * {@linkplain AnnotationConfigUtils#registerAnnotationConfigProcessors registering}
 * annotation configuration processors.</li>
 * <li>Calls {@link #customizeContext} to allow for customizing the context
 * before it is refreshed.</li>
 * <li>{@link ConfigurableApplicationContext#refresh Refreshes} the
 * context and registers a JVM shutdown hook for it.</li>
 * </ul>
 *
 * @return a new web application context
 * @see org.springframework.test.context.SmartContextLoader#loadContext(MergedContextConfiguration)
 * @see GenericWebApplicationContext
 */
@Override
public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception {

	if (!(mergedConfig instanceof WebMergedContextConfiguration)) {
		throw new IllegalArgumentException(String.format(
			"Cannot load WebApplicationContext from non-web merged context configuration %s. "
					+ "Consider annotating your test class with @WebAppConfiguration.", mergedConfig));
	}
	WebMergedContextConfiguration webMergedConfig = (WebMergedContextConfiguration) mergedConfig;

	if (logger.isDebugEnabled()) {
		logger.debug(String.format("Loading WebApplicationContext for merged context configuration %s.",
			webMergedConfig));
	}

	validateMergedContextConfiguration(webMergedConfig);

	GenericWebApplicationContext context = new GenericWebApplicationContext();

	ApplicationContext parent = mergedConfig.getParentApplicationContext();
	if (parent != null) {
		context.setParent(parent);
	}
	configureWebResources(context, webMergedConfig);
	prepareContext(context, webMergedConfig);
	customizeBeanFactory(context.getDefaultListableBeanFactory(), webMergedConfig);
	loadBeanDefinitions(context, webMergedConfig);
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	customizeContext(context, webMergedConfig);
	context.refresh();
	context.registerShutdownHook();
	return context;
}