/** * Sets a parent context for child context based on a given key. * * @param parentContextKey * key for the parent context * @param appContextId * id of the child context */ public void setParentContext(String parentContextKey, String appContextId) { log.debug("Set parent context {} on {}", parentContextKey, appContextId); ApplicationContext parentContext = getContext(parentContextKey); if (parentContext != null) { XmlWebApplicationContext childContext = (XmlWebApplicationContext) getContext(appContextId); if (childContext != null) { childContext.setParent(parentContext); } else { log.debug("Child context not found"); } } else { log.debug("Parent context not found"); } }
@Test @SuppressWarnings("resource") public void withoutMessageSource() throws Exception { MockServletContext sc = new MockServletContext(""); XmlWebApplicationContext wac = new XmlWebApplicationContext(); wac.setParent(root); wac.setServletContext(sc); wac.setNamespace("testNamespace"); wac.setConfigLocations(new String[] {"/org/springframework/web/context/WEB-INF/test-servlet.xml"}); wac.refresh(); try { wac.getMessage("someMessage", null, Locale.getDefault()); fail("Should have thrown NoSuchMessageException"); } catch (NoSuchMessageException ex) { // expected; } String msg = wac.getMessage("someMessage", null, "default", Locale.getDefault()); assertTrue("Default message returned", "default".equals(msg)); }
@Test public void configureWhenUserDetailsServiceInParentContextThenLocatesSuccessfully() { assertThatCode(() -> this.spring.configLocations(this.xml("MissingUserDetailsService")).autowire()) .isInstanceOf(BeansException.class); try ( XmlWebApplicationContext parent = new XmlWebApplicationContext() ) { parent.setConfigLocations(this.xml("AutoConfig")); parent.refresh(); try ( XmlWebApplicationContext child = new XmlWebApplicationContext() ) { child.setParent(parent); child.setConfigLocation(this.xml("MissingUserDetailsService")); child.refresh(); } } }
@Test @SuppressWarnings("resource") public void handlerBeanNotFound() { MockServletContext sc = new MockServletContext(""); XmlWebApplicationContext root = new XmlWebApplicationContext(); root.setServletContext(sc); root.setConfigLocations("/org/springframework/web/servlet/handler/map1.xml"); root.refresh(); XmlWebApplicationContext wac = new XmlWebApplicationContext(); wac.setParent(root); wac.setServletContext(sc); wac.setNamespace("map2err"); wac.setConfigLocations("/org/springframework/web/servlet/handler/map2err.xml"); try { wac.refresh(); fail("Should have thrown NoSuchBeanDefinitionException"); } catch (FatalBeanException ex) { NoSuchBeanDefinitionException nestedEx = (NoSuchBeanDefinitionException) ex.getCause(); assertEquals("mainControlle", nestedEx.getBeanName()); } }
XmlWebApplicationContext wac = new XmlWebApplicationContext(); wac.getEnvironment().addActiveProfile("wacProfile1"); wac.setParent(root); wac.setServletContext(sc); wac.setNamespace("test-servlet");
ctx2.setParent(ctx); String[] names = ctx.getBeanNamesForType(Bus.class); if (names == null || names.length == 0) {
@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 ... }
@Override public void init(ServletConfig config) throws ServletException { applicationContext = WebApplicationContextUtils.getWebApplicationContext(config.getServletContext()); if (config.getInitParameter(CONTEXT_CONFIG_LOCATION_PARAMETER) != null) { XmlWebApplicationContext context = new XmlWebApplicationContext(); context.setParent(applicationContext); context.setConfigLocation(config.getInitParameter(CONTEXT_CONFIG_LOCATION_PARAMETER)); context.setServletConfig(config); context.setServletContext(config.getServletContext()); context.refresh(); applicationContext = context; } if (config.getInitParameter(SYSTEM_MESSAGES_BEAN_NAME_PARAMETER) != null) { systemMessagesBeanName = config.getInitParameter(SYSTEM_MESSAGES_BEAN_NAME_PARAMETER); logger.debug("found SYSTEM_MESSAGES_BEAN_NAME_PARAMETER: {}", systemMessagesBeanName); } if (SpringApplicationContext.getApplicationContext() == null) { SpringApplicationContext.setApplicationContext(applicationContext); } super.init(config); }
if (applicationContext.containsBean(defaultParentContextKey)) { parentAppCtx = (ApplicationContext) applicationContext.getBean(defaultParentContextKey); appctx.setParent(parentAppCtx); } else { log.warn("{} bean was not found in context: {}", defaultParentContextKey, applicationContext.getDisplayName()); ContextLoader contextLoader = (ContextLoader) applicationContext.getBean("context.loader"); parentAppCtx = contextLoader.getContext(defaultParentContextKey); appctx.setParent(parentAppCtx); } else { log.debug("Context loader was not found, trying JMX");
/** * Instantiate a WrappingWebApplicationContext for this loader, * which wraps the ApplicationContext maintained by Janvg SpringContextHelper. * @param servletContext current servlet context * @param parent parent ApplicationContext (will be ignored!) * @see org.springframework.web.context.ContextLoader */ protected WebApplicationContext createWebApplicationContext( ServletContext servletContext, ApplicationContext parent) { if (parentApplicationContext == null) { parentApplicationContext = (ConfigurableApplicationContext) SpringContextHelper.getContext(); } XmlWebApplicationContext wac = new XmlWebApplicationContext() { public String[] getConfigLocations() { return null; } }; wac.setParent(parentApplicationContext); wac.setServletContext(servletContext); wac.refresh(); return wac; }
@SpringBootApplication public class WebApp extends SpringBootServletInitializer { @Autowired private ApplicationContext context; @Bean public ServletRegistrationBean restApi() { XmlWebApplicationContext applicationContext = new XmlWebApplicationContext(); applicationContext.setParent(context); applicationContext.setConfigLocation("classpath:/META-INF/rest.xml"); DispatcherServlet dispatcherServlet = new DispatcherServlet(); dispatcherServlet.setApplicationContext(applicationContext); ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(dispatcherServlet, "/rest/*"); servletRegistrationBean.setName("restApi"); return servletRegistrationBean; } static public void main(String[] args) throws Exception { SpringApplication.run(WebApp.class,args); } }
ctx2.setParent(ctx); String[] names = ctx.getBeanNamesForType(Bus.class); if (names == null || names.length == 0) {
appctx.setParent((ApplicationContext) applicationContext.getBean(defaultApplicationContextId)); } else { log.warn("{} bean was not found in context: {}", defaultApplicationContextId, applicationContext.getDisplayName()); appctx.setParent(contextLoader.getContext(defaultApplicationContextId)); } else { log.debug("Context loader was not found, trying JMX");
ctx2.setParent(ctx); String names[] = ctx.getBeanNamesForType(Bus.class); if (names == null || names.length == 0) {
public void afterPropertiesSet() throws Exception { if (loadContext) { context = new XmlWebApplicationContext(); context.setParent(parent); context.setDisplayName(displayName); context.setConfigLocations(configLocations); context.setServletContext(servletContext); context.refresh(); } }
appctx.setParent((ApplicationContext) applicationContext.getBean(defaultApplicationContextId)); } else { log.warn("{} bean was not found in context: {}", defaultApplicationContextId, applicationContext.getDisplayName()); appctx.setParent(contextLoader.getContext(defaultApplicationContextId)); } else { log.debug("Context loader was not found, trying JMX");
ServletContext sc = ExecutionContext.getSession().getServletContext(); ctx.setDisplayName("Child XmlWebApplicationContext " + page); ctx.setParent(AppContextFinder.rootContext); ctx.setServletContext(sc);
/** * Initialize the app context. * * @param config Configuration data. * @param parent If creating a child context, this will be its parent. Otherwise, will be null. * @return The initialized app context. */ protected XmlWebApplicationContext initAppContext(MockConfig config, ApplicationContext parent) { XmlWebApplicationContext appContext = createApplicationContext(); appContext.setServletContext(servletContext); if (parent == null) { ClasspathMessageSource.getInstance().setResourceLoader(appContext); } else { appContext.setParent(parent); } if (config.configLocations != null) { appContext.setConfigLocations(config.configLocations); } if (config.profiles != null && config.profiles.length > 0) { ConfigurableEnvironment env = appContext.getEnvironment(); env.setDefaultProfiles(parent == null ? config.profiles[0] : "dummy"); env.setActiveProfiles(config.profiles); } return appContext; }