Java Code Examples for org.springframework.web.context.support.XmlWebApplicationContext#setParent()

The following examples show how to use org.springframework.web.context.support.XmlWebApplicationContext#setParent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SimpleUrlHandlerMappingTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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());
	}
}
 
Example 2
Source File: XmlWebApplicationContextTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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));
}
 
Example 3
Source File: SimpleUrlHandlerMappingTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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());
	}
}
 
Example 4
Source File: XmlWebApplicationContextTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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));
}
 
Example 5
Source File: SimpleUrlHandlerMappingTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void handlerBeanNotFound() throws Exception {
	MockServletContext sc = new MockServletContext("");
	XmlWebApplicationContext root = new XmlWebApplicationContext();
	root.setServletContext(sc);
	root.setConfigLocations(new String[] {"/org/springframework/web/servlet/handler/map1.xml"});
	root.refresh();
	XmlWebApplicationContext wac = new XmlWebApplicationContext();
	wac.setParent(root);
	wac.setServletContext(sc);
	wac.setNamespace("map2err");
	wac.setConfigLocations(new String[] {"/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());
	}
}
 
Example 6
Source File: XmlWebApplicationContextTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example 7
Source File: SpringResourceLoader.java    From rice with Educational Community License v2.0 6 votes vote down vote up
@Override
public void start() throws Exception {
	if(!isStarted()){
		LOG.info("Creating Spring context " + StringUtils.join(this.fileLocs, ","));
		if (parentSpringResourceLoader != null && parentContext != null) {
			throw new ConfigurationException("Both a parentSpringResourceLoader and parentContext were defined.  Only one can be defined!");
		}
		if (parentSpringResourceLoader != null) {
			parentContext = parentSpringResourceLoader.getContext();
		}
		
		if (servletContextcontext != null) {
			XmlWebApplicationContext lContext = new XmlWebApplicationContext();
			lContext.setServletContext(servletContextcontext);
			lContext.setParent(parentContext);
			lContext.setConfigLocations(this.fileLocs.toArray(new String[] {}));
			lContext.refresh();
			context = lContext;
		} else {
			this.context = new ClassPathXmlApplicationContext(this.fileLocs.toArray(new String[] {}), parentContext);
		}
          
		super.start();
	}
}
 
Example 8
Source File: ContextLoader.java    From red5-server-common with Apache License 2.0 5 votes vote down vote up
/**
 * 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");
    }
}