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

The following examples show how to use org.springframework.web.context.support.GenericWebApplicationContext#setServletContext() . 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: WebApplicationContextLoader.java    From alfresco-data-model with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Provides a static, single instance of the application context.  This method can be
 * called repeatedly.
 * <p/>
 * If the configuration requested differs from one used previously, then the previously-created
 * context is shut down.
 * 
 * @return Returns an application context for the given configuration
 */
public synchronized static ConfigurableApplicationContext getApplicationContext(ServletContext servletContext, String[] configLocations)
{
	AbstractApplicationContext ctx = (AbstractApplicationContext)BaseApplicationContextHelper.getApplicationContext(configLocations);
	
	CmisServiceFactory factory = (CmisServiceFactory)ctx.getBean("CMISServiceFactory");
	
	DefaultListableBeanFactory dlbf = new DefaultListableBeanFactory(ctx.getBeanFactory());
	GenericWebApplicationContext gwac = new GenericWebApplicationContext(dlbf);
	servletContext.setAttribute(GenericWebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, gwac);
       servletContext.setAttribute(CmisRepositoryContextListener.SERVICES_FACTORY, factory);
	gwac.setServletContext(servletContext);
	gwac.refresh();

	return gwac;
}
 
Example 2
Source File: JasperReportsHtmlViewTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("deprecation")
public void configureExporterParametersWithEncodingFromPropertiesFile() throws Exception {
	GenericWebApplicationContext ac = new GenericWebApplicationContext();
	ac.setServletContext(new MockServletContext());
	BeanDefinitionReader reader = new PropertiesBeanDefinitionReader(ac);
	reader.loadBeanDefinitions(new ClassPathResource("view.properties", getClass()));
	ac.refresh();

	AbstractJasperReportsView view = (AbstractJasperReportsView) ac.getBean("report");
	String encoding = (String) view.getConvertedExporterParameters().get(
		net.sf.jasperreports.engine.export.JRHtmlExporterParameter.CHARACTER_ENCODING);
	assertEquals("UTF-8", encoding);

	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, ac);
	view.render(getModel(), request, response);
	assertEquals("Response content type is incorrect", "text/html;charset=UTF-8", response.getContentType());
}
 
Example 3
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 4
Source File: EnvironmentSystemIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void registerServletParamPropertySources_GenericWebApplicationContext() {
	MockServletContext servletContext = new MockServletContext();
	servletContext.addInitParameter("pCommon", "pCommonContextValue");
	servletContext.addInitParameter("pContext1", "pContext1Value");

	GenericWebApplicationContext ctx = new GenericWebApplicationContext();
	ctx.setServletContext(servletContext);
	ctx.refresh();

	ConfigurableEnvironment environment = ctx.getEnvironment();
	assertThat(environment, instanceOf(StandardServletEnvironment.class));
	MutablePropertySources propertySources = environment.getPropertySources();
	assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true));

	// ServletContext params are available
	assertThat(environment.getProperty("pCommon"), is("pCommonContextValue"));
	assertThat(environment.getProperty("pContext1"), is("pContext1Value"));

	// Servlet* PropertySources have precedence over System* PropertySources
	assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)),
			lessThan(propertySources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME))));

	// Replace system properties with a mock property source for convenience
	MockPropertySource mockSystemProperties = new MockPropertySource(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME);
	mockSystemProperties.setProperty("pCommon", "pCommonSysPropsValue");
	mockSystemProperties.setProperty("pSysProps1", "pSysProps1Value");
	propertySources.replace(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, mockSystemProperties);

	// assert that servletcontext init params resolve with higher precedence than sysprops
	assertThat(environment.getProperty("pCommon"), is("pCommonContextValue"));
	assertThat(environment.getProperty("pSysProps1"), is("pSysProps1Value"));
}
 
Example 5
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 6
Source File: EnvironmentSystemIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void registerServletParamPropertySources_GenericWebApplicationContext() {
	MockServletContext servletContext = new MockServletContext();
	servletContext.addInitParameter("pCommon", "pCommonContextValue");
	servletContext.addInitParameter("pContext1", "pContext1Value");

	GenericWebApplicationContext ctx = new GenericWebApplicationContext();
	ctx.setServletContext(servletContext);
	ctx.refresh();

	ConfigurableEnvironment environment = ctx.getEnvironment();
	assertThat(environment, instanceOf(StandardServletEnvironment.class));
	MutablePropertySources propertySources = environment.getPropertySources();
	assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true));

	// ServletContext params are available
	assertThat(environment.getProperty("pCommon"), is("pCommonContextValue"));
	assertThat(environment.getProperty("pContext1"), is("pContext1Value"));

	// Servlet* PropertySources have precedence over System* PropertySources
	assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)),
			lessThan(propertySources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME))));

	// Replace system properties with a mock property source for convenience
	MockPropertySource mockSystemProperties = new MockPropertySource(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME);
	mockSystemProperties.setProperty("pCommon", "pCommonSysPropsValue");
	mockSystemProperties.setProperty("pSysProps1", "pSysProps1Value");
	propertySources.replace(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, mockSystemProperties);

	// assert that servletcontext init params resolve with higher precedence than sysprops
	assertThat(environment.getProperty("pCommon"), is("pCommonContextValue"));
	assertThat(environment.getProperty("pSysProps1"), is("pSysProps1Value"));
}
 
Example 7
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 8
Source File: WebApplicationContextLoader.java    From alfresco-data-model with GNU Lesser General Public License v3.0 5 votes vote down vote up
public synchronized static ConfigurableApplicationContext getApplicationContext(ServletContext servletContext, final String[] configLocations,
		final String[] classLocations) throws IOException
{
	final AbstractApplicationContext ctx = (AbstractApplicationContext)BaseApplicationContextHelper.getApplicationContext(configLocations, classLocations);
	DefaultListableBeanFactory dlbf = new DefaultListableBeanFactory(ctx.getBeanFactory());
	GenericWebApplicationContext gwac = new GenericWebApplicationContext(dlbf);
	CmisServiceFactory factory = (CmisServiceFactory)ctx.getBean("CMISServiceFactory");

	servletContext.setAttribute(GenericWebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, gwac);
       servletContext.setAttribute(CmisRepositoryContextListener.SERVICES_FACTORY, factory);
	gwac.setServletContext(servletContext);
	gwac.refresh();

	return gwac;
}
 
Example 9
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 10
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 11
Source File: MvcNamespaceTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
	TestMockServletContext servletContext = new TestMockServletContext();
	appContext = new GenericWebApplicationContext();
	appContext.setServletContext(servletContext);
	LocaleContextHolder.setLocale(Locale.US);

	String attributeName = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
	appContext.getServletContext().setAttribute(attributeName, appContext);

	handler = new TestController();
	Method method = TestController.class.getMethod("testBind", Date.class, Double.class, TestBean.class, BindingResult.class);
	handlerMethod = new InvocableHandlerMethod(handler, method);
}
 
Example 12
Source File: EnvironmentSystemIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void registerServletParamPropertySources_GenericWebApplicationContext() {
	MockServletContext servletContext = new MockServletContext();
	servletContext.addInitParameter("pCommon", "pCommonContextValue");
	servletContext.addInitParameter("pContext1", "pContext1Value");

	GenericWebApplicationContext ctx = new GenericWebApplicationContext();
	ctx.setServletContext(servletContext);
	ctx.refresh();

	ConfigurableEnvironment environment = ctx.getEnvironment();
	assertThat(environment, instanceOf(StandardServletEnvironment.class));
	MutablePropertySources propertySources = environment.getPropertySources();
	assertThat(propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME), is(true));

	// ServletContext params are available
	assertThat(environment.getProperty("pCommon"), is("pCommonContextValue"));
	assertThat(environment.getProperty("pContext1"), is("pContext1Value"));

	// Servlet* PropertySources have precedence over System* PropertySources
	assertThat(propertySources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)),
			lessThan(propertySources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME))));

	// Replace system properties with a mock property source for convenience
	MockPropertySource mockSystemProperties = new MockPropertySource(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME);
	mockSystemProperties.setProperty("pCommon", "pCommonSysPropsValue");
	mockSystemProperties.setProperty("pSysProps1", "pSysProps1Value");
	propertySources.replace(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, mockSystemProperties);

	// assert that servletcontext init params resolve with higher precedence than sysprops
	assertThat(environment.getProperty("pCommon"), is("pCommonContextValue"));
	assertThat(environment.getProperty("pSysProps1"), is("pSysProps1Value"));
}
 
Example 13
Source File: GenericWebContextLoader.java    From maven-framework-project with MIT License 4 votes vote down vote up
protected void prepareContext(GenericWebApplicationContext context) {
	this.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
	context.setServletContext(this.servletContext);
}
 
Example 14
Source File: GenericWebContextLoader.java    From maven-framework-project with MIT License 4 votes vote down vote up
protected void prepareContext(GenericWebApplicationContext context) {
	this.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
	context.setServletContext(this.servletContext);
}
 
Example 15
Source File: GenericWebContextLoader.java    From maven-framework-project with MIT License 4 votes vote down vote up
protected void prepareContext(GenericWebApplicationContext context) {
	this.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
	context.setServletContext(this.servletContext);
}