@Override public void initialize(ConfigurableWebApplicationContext applicationContext) { applicationContext.setServletContext(this.servletContext); if (this.addApplicationContextAttribute) { this.servletContext.setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext); } }
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) { if (ObjectUtils.identityToString(wac).equals(wac.getId())) { // The application context id is still set to its original default value // -> assign a more useful id based on available information String idParam = sc.getInitParameter(CONTEXT_ID_PARAM); if (idParam != null) { wac.setId(idParam); } else { // Generate default id... wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(sc.getContextPath())); } } wac.setServletContext(sc); String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM); if (configLocationParam != null) { wac.setConfigLocation(configLocationParam); } // The wac environment's #initPropertySources will be called in any case when the context // is refreshed; do it eagerly here to ensure servlet property sources are in place for // use in any post-processing or initialization that occurs below prior to #refresh ConfigurableEnvironment env = wac.getEnvironment(); if (env instanceof ConfigurableWebEnvironment) { ((ConfigurableWebEnvironment) env).initPropertySources(sc, null); } customizeContext(sc, wac); wac.refresh(); }
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) { if (ObjectUtils.identityToString(wac).equals(wac.getId())) { // The application context id is still set to its original default value // -> assign a more useful id based on available information String idParam = sc.getInitParameter(CONTEXT_ID_PARAM); if (idParam != null) { wac.setId(idParam); } else { // Generate default id... wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(sc.getContextPath())); } } wac.setServletContext(sc); String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM); if (configLocationParam != null) { wac.setConfigLocation(configLocationParam); } // The wac environment's #initPropertySources will be called in any case when the context // is refreshed; do it eagerly here to ensure servlet property sources are in place for // use in any post-processing or initialization that occurs below prior to #refresh ConfigurableEnvironment env = wac.getEnvironment(); if (env instanceof ConfigurableWebEnvironment) { ((ConfigurableWebEnvironment) env).initPropertySources(sc, null); } customizeContext(sc, wac); wac.refresh(); }
@Before public void setUp() throws Exception { MockServletContext sc = new MockServletContext(""); wac = new XmlWebApplicationContext(); wac.setServletContext(sc); wac.setConfigLocations(new String[] {CONF}); wac.refresh(); }
@Before public void setUp() throws Exception { MockServletContext sc = new MockServletContext(""); wac = new XmlWebApplicationContext(); wac.setServletContext(sc); wac.setConfigLocations(new String[] {CONF}); wac.refresh(); hm = (HandlerMapping) wac.getBean("urlMapping"); }
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac) { if (ObjectUtils.identityToString(wac).equals(wac.getId())) { // The application context id is still set to its original default value // -> assign a more useful id based on available information if (this.contextId != null) { wac.setId(this.contextId); } else { // Generate default id... wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(getServletContext().getContextPath()) + '/' + getServletName()); } } wac.setServletContext(getServletContext()); wac.setServletConfig(getServletConfig()); wac.setNamespace(getNamespace()); wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener())); // The wac environment's #initPropertySources will be called in any case when the context // is refreshed; do it eagerly here to ensure servlet property sources are in place for // use in any post-processing or initialization that occurs below prior to #refresh ConfigurableEnvironment env = wac.getEnvironment(); if (env instanceof ConfigurableWebEnvironment) { ((ConfigurableWebEnvironment) env).initPropertySources(getServletContext(), getServletConfig()); } postProcessWebApplicationContext(wac); applyInitializers(wac); wac.refresh(); }
public void initializeContext(ConfigurableWebApplicationContext context, String yamlPath) { MockServletContext servletContext = new MockServletContext() { @Override public <Type extends EventListener> void addListener(Type t) { //no op } }; MockServletConfig servletConfig = new MockServletConfig(servletContext); servletConfig.addInitParameter("environmentConfigDefaults", yamlPath); context.setServletContext(servletContext); context.setServletConfig(servletConfig); new YamlServletProfileInitializer().initialize(context); } }
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac) { if (ObjectUtils.identityToString(wac).equals(wac.getId())) { // The application context id is still set to its original default value // -> assign a more useful id based on available information if (this.contextId != null) { wac.setId(this.contextId); } else { // Generate default id... wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(getServletContext().getContextPath()) + '/' + getServletName()); } } wac.setServletContext(getServletContext()); wac.setServletConfig(getServletConfig()); wac.setNamespace(getNamespace()); wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener())); // The wac environment's #initPropertySources will be called in any case when the context // is refreshed; do it eagerly here to ensure servlet property sources are in place for // use in any post-processing or initialization that occurs below prior to #refresh ConfigurableEnvironment env = wac.getEnvironment(); if (env instanceof ConfigurableWebEnvironment) { ((ConfigurableWebEnvironment) env).initPropertySources(getServletContext(), getServletConfig()); } postProcessWebApplicationContext(wac); applyInitializers(wac); wac.refresh(); }
root.getEnvironment().addActiveProfile("rootProfile1"); MockServletContext sc = new MockServletContext(""); root.setServletContext(sc); root.setConfigLocations(new String[] {"/org/springframework/web/context/WEB-INF/applicationContext.xml"}); root.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() {
public void autowire() { this.context.setServletContext(new MockServletContext()); this.context.setServletConfig(new MockServletConfig()); this.context.refresh(); if (this.context.containsBean(SPRING_SECURITY_FILTER_CHAIN)) { MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context) .apply(springSecurity()) .apply(new AddFilter()).build(); this.context.getBeanFactory() .registerResolvableDependency(MockMvc.class, mockMvc); } AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); bpp.setBeanFactory(this.context.getBeanFactory()); bpp.processInjection(this.test); }
@Test public void requestWhenUsingMvcMatchersAndServletPathThenAuthorizesRequestsAccordingly() throws Exception { this.spring.configLocations(this.xml("MvcMatchersServletPath")).autowire(); MockServletContext servletContext = mockServletContext("/spring"); ConfigurableWebApplicationContext context = (ConfigurableWebApplicationContext) this.spring.getContext(); context.setServletContext(servletContext); this.mvc.perform(get("/spring/path").servletPath("/spring")) .andExpect(status().isUnauthorized()); this.mvc.perform(get("/spring/path.html").servletPath("/spring")) .andExpect(status().isUnauthorized()); this.mvc.perform(get("/spring/path/").servletPath("/spring")) .andExpect(status().isUnauthorized()); }
applicationContext.getEnvironment().setActiveProfiles(springProfiles.toArray(new String[0])); applicationContext.setServletContext(servletContext);
@Override public synchronized void setServletContext(final ServletContext context) { this.appContext.setServletContext(context); }
public void setServletContext(ServletContext servletContext) { initialiseApplicationContext(); if (context instanceof ConfigurableWebApplicationContext) { ((ConfigurableWebApplicationContext)context).setServletContext(servletContext); } } }
@Override protected WebApplicationContext initWebApplicationContext() { ServletContext servletContext = new MockServletContext(this.webResourceBasePath, this.webResourceLoader) { // Required for DefaultServletHttpRequestHandler... public RequestDispatcher getNamedDispatcher(String path) { return (path.equals("default")) ? new MockRequestDispatcher(path) : super.getNamedDispatcher(path); } }; this.webAppContext.setServletContext(servletContext); this.webAppContext.refresh(); return this.webAppContext; }
/** * Decorate the specified {@code contextFactory} to set a {@link MockServletContext} * on each newly created {@link WebApplicationContext}. * @param contextFactory the context factory to decorate * @return an updated supplier that will set the {@link MockServletContext} */ public static Supplier<ConfigurableWebApplicationContext> withMockServletContext( Supplier<ConfigurableWebApplicationContext> contextFactory) { return (contextFactory != null) ? () -> { ConfigurableWebApplicationContext context = contextFactory.get(); context.setServletContext(new MockServletContext()); return context; } : null; }
public synchronized void reload() { ConfigurableWebApplicationContext newAppContext = null; try { newAppContext = BeanUtils.instantiateClass(XmlWebApplicationContext.class); } catch (BeanInstantiationException e) { throw new RuntimeException("Can't create Spring application context.", e); } newAppContext.setParent(this.appContext.getParent()); newAppContext.setServletContext(this.appContext.getServletContext()); this.appContext.close(); this.appContext = newAppContext; this.appContext.refresh(); }
@Override public void onStartup(final ServletContext servletContext) throws ServletException { final ApplicationContext parent = new ClassPathXmlApplicationContext(new String[ {"/com/mydomain/root.context.xml"}); XmlWebApplicationContext context = new XmlWebApplicationContext(); context.setConfigLocation("classpath:/com/mydomain/childContext.xml"); context.setParent(parent); ConfigurableWebApplicationContext webappContext = (ConfigurableWebApplicationContext) context; webappContext.setServletContext(servletContext); webappContext.refresh(); servletContext.addListener(new ContextLoaderListener(webappContext)); // ... register dispatcher servlets here ... }
public void start(ServletContext context) { parentContext.setServletContext(context); parentContext.addBeanFactoryPostProcessor(new BeanRegistrationProcessor(this)); parentContext.refresh(); parentContext.start(); }
/** * Get the child application context. * <p> * The child application context is created as a child of the default * application context obtained from {@link #getDefaultContext() }. * * @param contextConfigLocation the location of the child application * context. * @return the child application context. */ protected ConfigurableApplicationContext getChildContext(String contextConfigLocation) { final ConfigurableWebApplicationContext ctx = new XmlWebApplicationContext(); ctx.setParent(getDefaultContext()); ctx.setServletContext(getServletContext()); ctx.setConfigLocation(contextConfigLocation); ctx.refresh(); return ctx; } }