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(); }
(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass); wac.setEnvironment(getEnvironment()); wac.setParent(parent); String configLocation = getContextConfigLocation(); if (configLocation != null) { wac.setConfigLocation(configLocation);
/** * Close Spring's web application context for the given servlet context. * <p>If overriding {@link #loadParentContext(ServletContext)}, you may have * to override this method as well. * @param servletContext the ServletContext that the WebApplicationContext runs in */ public void closeWebApplicationContext(ServletContext servletContext) { servletContext.log("Closing Spring root WebApplicationContext"); try { if (this.context instanceof ConfigurableWebApplicationContext) { ((ConfigurableWebApplicationContext) this.context).close(); } } finally { ClassLoader ccl = Thread.currentThread().getContextClassLoader(); if (ccl == ContextLoader.class.getClassLoader()) { currentContext = null; } else if (ccl != null) { currentContextPerThread.remove(ccl); } servletContext.removeAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); } }
@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"); }
wac.setParent(parent); wac.setServletContext(getServletContext()); wac.setNamespace(getNamespace()); if (getContextConfigLocation() != null) { wac.setConfigLocations( StringUtils.tokenizeToStringArray( getContextConfigLocation(), ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS)); wac.addBeanFactoryPostProcessor( new BeanFactoryPostProcessor() { public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { wac.refresh(); return wac;
/** * 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; } }
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(); }
/** * Instantiate the root WebApplicationContext for this loader, either the * default context class or a custom context class if specified. * <p>This implementation expects custom contexts to implement * ConfigurableWebApplicationContext. Can be overridden in subclasses. * @param servletContext current servlet context * @param parent the parent ApplicationContext to use, or <code>null</code> if none * @return the root WebApplicationContext * @throws BeansException if the context couldn't be initialized * @see ConfigurableWebApplicationContext */ protected WebApplicationContext createWebApplicationContext( ServletContext servletContext, ApplicationContext parent) throws BeansException { Class contextClass = determineContextClass(servletContext); if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) { throw new ApplicationContextException("Custom context class [" + contextClass.getName() + "] is not of type ConfigurableWebApplicationContext"); } ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass); wac.setParent(parent); wac.setServletContext(servletContext); String configLocation = servletContext.getInitParameter(CONFIG_LOCATION_PARAM); if (configLocation != null) { wac.setConfigLocations(StringUtils.tokenizeToStringArray(configLocation, ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS)); } wac.refresh(); return wac; }
appctx.setConfigLocations(new String[] { contextConfigLocation }); appctx.setServletContext(servletContext); appctx.setParent(parentContext); } else { appctx.setParent(applicationContext); log.trace("Classloader prior to refresh: {}", appctx.getClassLoader()); appctx.refresh(); if (log.isDebugEnabled()) { log.debug("Red5 app is active: {} running: {}", appctx.isActive(), appctx.isRunning()); if (appctx.containsBean("realm")) { log.debug("Realm specified in context configuration"); Realm contextRealm = (Realm) appctx.getBean("realm"); if (contextRealm != null) { log.debug("Realm class: {}", contextRealm.getClass().getName()); appctx.start(); } catch (Throwable e) { throw new RuntimeException("Failed to load webapplication context class", e);
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(); }
InitAndIB.constructed = false; root = new XmlWebApplicationContext(); 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() { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { root.refresh(); XmlWebApplicationContext wac = new XmlWebApplicationContext(); wac.getEnvironment().addActiveProfile("wacProfile1");
@Before public void setUp() throws Exception { MockServletContext sc = new MockServletContext(""); wac = new XmlWebApplicationContext(); wac.setServletContext(sc); wac.setConfigLocations(new String[] {CONF}); wac.refresh(); }
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); }
@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; }
public void registerSubContext(String webAppKey) { // get the sub contexts - servlet context ServletContext ctx = servletContext.getContext(webAppKey); if (ctx == null) { ctx = servletContext; } ContextLoader loader = new ContextLoader(); ConfigurableWebApplicationContext appCtx = (ConfigurableWebApplicationContext) loader.initWebApplicationContext(ctx); appCtx.setParent(applicationContext); appCtx.refresh(); ctx.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appCtx); ConfigurableBeanFactory appFactory = appCtx.getBeanFactory(); logger.debug("About to grab Webcontext bean for {}", webAppKey); Context webContext = (Context) appCtx.getBean("web.context"); webContext.setCoreBeanFactory(parentFactory); webContext.setClientRegistry(clientRegistry); webContext.setServiceInvoker(globalInvoker); webContext.setScopeResolver(globalResolver); webContext.setMappingStrategy(globalStrategy); WebScope scope = (WebScope) appFactory.getBean("web.scope"); scope.setServer(server); scope.setParent(global); scope.register(); scope.start(); // register the context so we dont try to reinitialize it registeredContexts.add(ctx); }
public void start(ServletContext context) { parentContext.setServletContext(context); parentContext.addBeanFactoryPostProcessor(new BeanRegistrationProcessor(this)); parentContext.refresh(); parentContext.start(); }
Timer.start("SPRING_INITIALIZER_ONSTARTUP"); if (springProfiles != null) { applicationContext.getEnvironment().setActiveProfiles(springProfiles.toArray(new String[0])); applicationContext.setServletContext(servletContext); applicationContext.setServletConfig(dispatcherConfig); applicationContext.addApplicationListener((ApplicationListener<ServletRequestHandledEvent>) servletRequestHandledEvent -> { try { if (currentResponse != null) {
protected WebApplicationContext createWebApplicationContext(ApplicationContext parent) { Class<?> contextClass = getContextClass(); if (this.logger.isDebugEnabled()) { this.logger.debug("Servlet with name '" + getServletName() + "' will try to create custom WebApplicationContext context of class '" + contextClass.getName() + "'" + ", using parent context [" + parent + "]"); } if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) { throw new ApplicationContextException( "Fatal initialization error in servlet with name '" + getServletName() + "': custom WebApplicationContext class [" + contextClass.getName() + "] is not of type ConfigurableWebApplicationContext"); } ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass); wac.setParent(parent); if (wac.getParent() == null) { ApplicationContext rootContext = (ApplicationContext) getServletContext().getAttribute("JetStreamRoot"); wac.setParent(rootContext); } wac.setConfigLocation(getContextConfigLocation()); configureAndRefreshWebApplicationContext(wac); return wac; } }