本文整理汇总了Java中org.springframework.web.context.support.GenericWebApplicationContext.setServletContext方法的典型用法代码示例。如果您正苦于以下问题:Java GenericWebApplicationContext.setServletContext方法的具体用法?Java GenericWebApplicationContext.setServletContext怎么用?Java GenericWebApplicationContext.setServletContext使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.web.context.support.GenericWebApplicationContext
的用法示例。
在下文中一共展示了GenericWebApplicationContext.setServletContext方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getApplicationContext
import org.springframework.web.context.support.GenericWebApplicationContext; //导入方法依赖的package包/类
/**
* 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;
}
示例2: configureExporterParametersWithEncodingFromPropertiesFile
import org.springframework.web.context.support.GenericWebApplicationContext; //导入方法依赖的package包/类
@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());
}
示例3: createWebApplicationContext
import org.springframework.web.context.support.GenericWebApplicationContext; //导入方法依赖的package包/类
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;
}
示例4: initFactory
import org.springframework.web.context.support.GenericWebApplicationContext; //导入方法依赖的package包/类
/**
* 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;
}
示例5: setUp
import org.springframework.web.context.support.GenericWebApplicationContext; //导入方法依赖的package包/类
@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);
}
示例6: registerServletParamPropertySources_GenericWebApplicationContext
import org.springframework.web.context.support.GenericWebApplicationContext; //导入方法依赖的package包/类
@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"));
}
示例7: parameterDispatchingController
import org.springframework.web.context.support.GenericWebApplicationContext; //导入方法依赖的package包/类
@Test
public void parameterDispatchingController() throws Exception {
final MockServletContext servletContext = new MockServletContext();
final MockServletConfig servletConfig = new MockServletConfig(servletContext);
@SuppressWarnings("serial") DispatcherServlet servlet = new DispatcherServlet() {
@Override
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
GenericWebApplicationContext wac = new GenericWebApplicationContext();
wac.setServletContext(servletContext);
RootBeanDefinition bd = new RootBeanDefinition(MyParameterDispatchingController.class);
//bd.setScope(WebApplicationContext.SCOPE_REQUEST);
wac.registerBeanDefinition("controller", bd);
AnnotationConfigUtils.registerAnnotationConfigProcessors(wac);
wac.getBeanFactory().registerResolvableDependency(ServletConfig.class, servletConfig);
wac.refresh();
return wac;
}
};
servlet.init(servletConfig);
MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
MockHttpServletResponse response = new MockHttpServletResponse();
HttpSession session = request.getSession();
servlet.service(request, response);
assertEquals("myView", response.getContentAsString());
assertSame(servletContext, request.getAttribute("servletContext"));
assertSame(servletConfig, request.getAttribute("servletConfig"));
assertSame(session.getId(), request.getAttribute("sessionId"));
assertSame(request.getRequestURI(), request.getAttribute("requestUri"));
assertSame(request.getLocale(), request.getAttribute("locale"));
request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
response = new MockHttpServletResponse();
session = request.getSession();
servlet.service(request, response);
assertEquals("myView", response.getContentAsString());
assertSame(servletContext, request.getAttribute("servletContext"));
assertSame(servletConfig, request.getAttribute("servletConfig"));
assertSame(session.getId(), request.getAttribute("sessionId"));
assertSame(request.getRequestURI(), request.getAttribute("requestUri"));
request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
request.addParameter("view", "other");
response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals("myOtherView", response.getContentAsString());
request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
request.addParameter("view", "my");
request.addParameter("lang", "de");
response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals("myLangView", response.getContentAsString());
request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
request.addParameter("surprise", "!");
response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals("mySurpriseView", response.getContentAsString());
MyParameterDispatchingController deserialized = (MyParameterDispatchingController) SerializationTestUtils
.serializeAndDeserialize(servlet.getWebApplicationContext().getBean("controller"));
assertNotNull(deserialized.request);
assertNotNull(deserialized.session);
}
示例8: constrainedParameterDispatchingController
import org.springframework.web.context.support.GenericWebApplicationContext; //导入方法依赖的package包/类
@Test
public void constrainedParameterDispatchingController() throws Exception {
final MockServletContext servletContext = new MockServletContext();
final MockServletConfig servletConfig = new MockServletConfig(servletContext);
@SuppressWarnings("serial") DispatcherServlet servlet = new DispatcherServlet() {
@Override
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
GenericWebApplicationContext wac = new GenericWebApplicationContext();
wac.setServletContext(servletContext);
RootBeanDefinition bd = new RootBeanDefinition(MyConstrainedParameterDispatchingController.class);
bd.setScope(WebApplicationContext.SCOPE_REQUEST);
wac.registerBeanDefinition("controller", bd);
wac.refresh();
return wac;
}
};
servlet.init(servletConfig);
MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
request.addParameter("view", "other");
MockHttpServletResponse response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals(400, response.getStatus());
request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
request.addParameter("active", "true");
request.addParameter("view", "other");
response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals("myOtherView", response.getContentAsString());
request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
request.addParameter("view", "my");
request.addParameter("lang", "de");
response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals(400, response.getStatus());
request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
request.addParameter("view", "my");
request.addParameter("lang", "de");
request.addParameter("active", "true");
response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals("myLangView", response.getContentAsString());
}