ServletContext servletContext = new MockServletContext(resourceBasePath, resourceLoader); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context); context.setServletContext(servletContext); context.setServletContext(servletContext);
factory.setServletContext(getServletContext());
private void loadBeanDefinitions(String fileName) { XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.appContext); ClassPathResource resource = new ClassPathResource(fileName, MessageBrokerBeanDefinitionParserTests.class); reader.loadBeanDefinitions(resource); this.appContext.setServletContext(new MockServletContext()); this.appContext.refresh(); }
factory.setServletContext(getServletContext());
/** * 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; }
/** * 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; }
@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")); }
@Before public void setUp() { ServletContext servletContext = new MockServletContext(); MockHttpServletRequest mockRequest = new MockHttpServletRequest(servletContext); mockRequest.setAttribute(FROM_CUSTOM_MOCK, FROM_CUSTOM_MOCK); RequestContextHolder.setRequestAttributes(new ServletWebRequest(mockRequest, new MockHttpServletResponse())); this.wac.setServletContext(servletContext); new AnnotatedBeanDefinitionReader(this.wac).register(WebConfig.class); this.wac.refresh(); this.mockMvc = webAppContextSetup(this.wac) .defaultRequest(get("/").requestAttr(FROM_MVC_TEST_DEFAULT, FROM_MVC_TEST_DEFAULT)) .alwaysExpect(status().isOk()) .build(); }
wac.setServletContext(servletContext); AnnotationConfigUtils.registerAnnotationConfigProcessors(wac); wac.getBeanFactory().registerResolvableDependency(ServletConfig.class, servletConfig);
@Override public void setServletContext(ServletContext servletContext) { super.setServletContext(servletContext); // prepareWebApplicationContext(servletContext); }
@Override public void setServletContext(ServletContext servletContext) { super.setServletContext(servletContext); // prepareWebApplicationContext(servletContext); }
@Override public void initialize(GenericWebApplicationContext applicationContext) { applicationContext.setServletContext(servletContext); } });
MockServletContext servletContext = new MockServletContextPatched(); MockServletConfig config = new MockServletConfig( servletContext ); ImageServlet servlet = new ImageServlet(); ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext( "spring-data-app-context.xml" ); DefaultListableBeanFactory dlbf = new DefaultListableBeanFactory(appContext.getBeanFactory()); GenericWebApplicationContext gwac = new GenericWebApplicationContext(dlbf); servletContext.setAttribute(GenericWebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, gwac); gwac.setServletContext(servletContext); gwac.refresh(); servlet.init( config );
public class XmlWebApplicationContextLoader extends AbstractContextLoader { public final ConfigurableApplicationContext loadContext(final String... locations) throws Exception { ServletContext servletContext = new MockServletContext("war", new FileSystemResourceLoader()); GenericWebApplicationContext webContext = new GenericWebApplicationContext(); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webContext); webContext.setServletContext(servletContext); new XmlBeanDefinitionReader(webContext).loadBeanDefinitions(locations); AnnotationConfigUtils.registerAnnotationConfigProcessors(webContext); webContext.refresh(); webContext.registerShutdownHook(); return webContext; } protected String getResourceSuffix() { return ""; }
/** * Performs initialization of the web context binding it to Jetty. * * @throws Exception * in case of an error while starting the Jetty context */ @PostConstruct public void postConstruct() throws Exception { ServletContext servletContext = jettyContext.getServletContext(); GenericWebApplicationContext webCtx = new GenericWebApplicationContext(); webCtx.setServletContext(servletContext); webCtx.setParent(ctx); webCtx.refresh(); servletContext.setAttribute(contextAttribute, webCtx); jettyContext.start(); if (log.isInfoEnabled()) { log.info("| Jetty Web Application Context started!"); } }
private GenericApplicationContext createFlowApplicationContext(Resource[] resources) { ApplicationContext parent = getContext().getApplicationContext(); GenericApplicationContext flowContext; if (parent instanceof WebApplicationContext) { GenericWebApplicationContext webContext = new GenericWebApplicationContext(); webContext.setServletContext(((WebApplicationContext) parent).getServletContext()); flowContext = webContext; } else { flowContext = new GenericApplicationContext(); } flowContext.setDisplayName("Flow ApplicationContext [" + getContext().getFlowId() + "]"); flowContext.setParent(parent); flowContext.getBeanFactory().registerScope("request", new RequestScope()); flowContext.getBeanFactory().registerScope("flash", new FlashScope()); flowContext.getBeanFactory().registerScope("view", new ViewScope()); flowContext.getBeanFactory().registerScope("flow", new FlowScope()); flowContext.getBeanFactory().registerScope("conversation", new ConversationScope()); // Ensure the current ClassLoader is used, or otherwise setting the ResourceLoader would suppress it ClassLoader classLoaderToUse = flowContext.getClassLoader(); flowContext.setClassLoader(classLoaderToUse); Resource flowResource = flowModelHolder.getFlowModelResource(); flowContext.setResourceLoader(new FlowRelativeResourceLoader(flowResource)); AnnotationConfigUtils.registerAnnotationConfigProcessors(flowContext); new XmlBeanDefinitionReader(flowContext).loadBeanDefinitions(resources); registerFlowBeans(flowContext.getBeanFactory()); registerMessageSource(flowContext, flowResource); flowContext.refresh(); return flowContext; }
protected void prepareContext(GenericWebApplicationContext context) { MockServletContext servletContext = new MockServletContext(); context.setServletContext(servletContext); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context); } }
@Override public ApplicationContext loadContext(final String... locations) throws Exception { this.locations = locations; this.servletContext = new MockServletContext(this.contextPath, new FileSystemResourceLoader()); this.webContext = new GenericWebApplicationContext(); this.servletContext.setAttribute(ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.webContext); this.webContext.setServletContext(this.servletContext); new XmlBeanDefinitionReader(this.webContext).loadBeanDefinitions(locations); AnnotationConfigUtils.registerAnnotationConfigProcessors(this.webContext); this.webContext.refresh(); this.webContext.registerShutdownHook(); return this.webContext; }
public void setup(ApplicationContext applicationContext) throws ServletException { MockServletContext servletContext = new MockServletContext(); GenericWebApplicationContext testWebContext = new GenericWebApplicationContext(); testWebContext.setParent(applicationContext); testWebContext.setServletContext(servletContext); servletContext.setAttribute(CONTEXT_NAME, testWebContext); servletContext.setContextPath(CONTEXT_PATH); config = new MockServletConfig(servletContext, "services"); Assert.assertNotNull(config); d.setPublishEvents(false); d.setContextAttribute(CONTEXT_NAME); d.init(config); }
protected void setupSpringContext() { GenericWebApplicationContext context = new GenericWebApplicationContext(); new AnnotatedBeanDefinitionReader(context).register(GeoGigWebAPISpringConfig.class); MockServletContext msc = new MockServletContext(""); context.setServletContext(msc); context.refresh(); wac = context; } /**