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

The following examples show how to use org.springframework.web.context.support.XmlWebApplicationContext#setConfigLocations() . 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 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 3
Source File: MockTdsContextLoader.java    From tds with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public ApplicationContext loadContext(String... locations) throws Exception {
  final MockServletContext servletContext = new MockTdsServletContext();
  final MockServletConfig servletConfig = new MockServletConfig(servletContext);
  final XmlWebApplicationContext webApplicationContext = new XmlWebApplicationContext();

  servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webApplicationContext);
  webApplicationContext.setServletConfig(servletConfig);
  webApplicationContext.setConfigLocations(locations);
  webApplicationContext.refresh();

  TdsContext tdsContext = webApplicationContext.getBean(TdsContext.class);
  checkContentRootPath(webApplicationContext, tdsContext);

  webApplicationContext.registerShutdownHook();

  return webApplicationContext;
}
 
Example 4
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 5
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 6
Source File: WiringTests.java    From cas4.0.x-server-wechat with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
    applicationContext = new XmlWebApplicationContext();
    applicationContext.setConfigLocations(new String[]{
            "file:src/main/webapp/WEB-INF/cas-management-servlet.xml",
            "file:src/main/webapp/WEB-INF/managementConfigContext.xml",
    "file:src/main/webapp/WEB-INF/spring-configuration/*.xml"});
    applicationContext.setServletContext(new MockServletContext(new ResourceLoader() {
        @Override
        public Resource getResource(final String location) {
            return new FileSystemResource("src/main/webapp" + location);
        }

        @Override
        public ClassLoader getClassLoader() {
            return getClassLoader();
        }
    }));
    applicationContext.refresh();
}
 
Example 7
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 8
Source File: WiringTests.java    From cas4.0.x-server-wechat with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
    applicationContext = new XmlWebApplicationContext();
    applicationContext.setConfigLocations(new String[]{
            "file:src/main/webapp/WEB-INF/cas-servlet.xml",
            "file:src/main/webapp/WEB-INF/deployerConfigContext.xml",
    "file:src/main/webapp/WEB-INF/spring-configuration/*.xml"});
    applicationContext.setServletContext(new MockServletContext(new ResourceLoader() {
        @Override
        public Resource getResource(final String location) {
            return new FileSystemResource("src/main/webapp" + location);
        }

        @Override
        public ClassLoader getClassLoader() {
            return getClassLoader();
        }
    }));
    applicationContext.refresh();
}
 
Example 9
Source File: WiringTests.java    From springboot-shiro-cas-mybatis with MIT License 6 votes vote down vote up
@Before
public void setUp() {
    applicationContext = new XmlWebApplicationContext();
    applicationContext.setConfigLocations(
            "file:src/main/webapp/WEB-INF/cas-management-servlet.xml",
            "file:src/main/webapp/WEB-INF/managementConfigContext.xml",
    "file:src/main/webapp/WEB-INF/spring-configuration/*.xml");
    applicationContext.setServletContext(new MockServletContext(new ResourceLoader() {
        @Override
        public Resource getResource(final String location) {
            return new FileSystemResource("src/main/webapp" + location);
        }

        @Override
        public ClassLoader getClassLoader() {
            return getClassLoader();
        }
    }));
    applicationContext.refresh();
}
 
Example 10
Source File: AopNamespaceHandlerScopeIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
	XmlWebApplicationContext wac = new XmlWebApplicationContext();
	wac.setConfigLocations(new String[] {CONTEXT});
	wac.refresh();
	this.context = wac;
}
 
Example 11
Source File: PathMatchingUrlHandlerMappingTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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");
}
 
Example 12
Source File: BeanNameUrlHandlerMappingTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
	MockServletContext sc = new MockServletContext("");
	wac = new XmlWebApplicationContext();
	wac.setServletContext(sc);
	wac.setConfigLocations(new String[] {CONF});
	wac.refresh();
}
 
Example 13
Source File: PetclinicInitializer.java    From DevOps-for-Web-Development with MIT License 5 votes vote down vote up
@Override
protected WebApplicationContext createRootApplicationContext() {
    XmlWebApplicationContext rootAppContext = new XmlWebApplicationContext();
    rootAppContext.setConfigLocations("classpath:spring/business-config.xml", "classpath:spring/tools-config.xml");
    rootAppContext.getEnvironment().setActiveProfiles(SPRING_PROFILE);
    return rootAppContext;
}
 
Example 14
Source File: AopNamespaceHandlerScopeIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setUp() {
	XmlWebApplicationContext wac = new XmlWebApplicationContext();
	wac.setConfigLocations(CONTEXT);
	wac.refresh();
	this.context = wac;
}
 
Example 15
Source File: BeanNameUrlHandlerMappingTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
	MockServletContext sc = new MockServletContext("");
	wac = new XmlWebApplicationContext();
	wac.setServletContext(sc);
	wac.setConfigLocations(new String[] {CONF});
	wac.refresh();
}
 
Example 16
Source File: PathMatchingUrlHandlerMappingTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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");
}
 
Example 17
Source File: AopNamespaceHandlerScopeIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setUp() {
	XmlWebApplicationContext wac = new XmlWebApplicationContext();
	wac.setConfigLocations(CONTEXT);
	wac.refresh();
	this.context = wac;
}
 
Example 18
Source File: ConfigTestUtils.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Crea e restituisce il Contesto dell'Applicazione.
 *
 * @param srvCtx Il Contesto della Servlet.
 * @return Il Contesto dell'Applicazione.
 */
public ApplicationContext createApplicationContext(ServletContext srvCtx) {
    this.createNamingContext();
    XmlWebApplicationContext applicationContext = new XmlWebApplicationContext();
    applicationContext.setConfigLocations(this.getSpringConfigFilePaths());
    applicationContext.setServletContext(srvCtx);
    ContextLoader contextLoader = new ContextLoader(applicationContext);
    contextLoader.initWebApplicationContext(srvCtx);
    applicationContext.refresh();
    return applicationContext;
}
 
Example 19
Source File: MockServletContextWebContextLoader.java    From olat with Apache License 2.0 4 votes vote down vote up
@Override
public final XmlWebApplicationContext loadContext(final String... locations) throws Exception {

    System.out.println("Loading ApplicationContext for locations [" + StringUtils.arrayToCommaDelimitedString(locations) + "].");

    final XmlWebApplicationContext appContext = new XmlWebApplicationContext();
    final MockServletContext servletContext = new MockServletContext();

    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appContext);

    appContext.setServletContext(servletContext);
    appContext.setConfigLocations(locations);
    appContext.refresh();
    appContext.registerShutdownHook();

    return appContext;

}
 
Example 20
Source File: MockServletContextWebContextLoader.java    From olat with Apache License 2.0 4 votes vote down vote up
@Override
public final XmlWebApplicationContext loadContext(final String... locations) throws Exception {

    System.out.println("Loading ApplicationContext for locations [" + StringUtils.arrayToCommaDelimitedString(locations) + "].");

    final XmlWebApplicationContext appContext = new XmlWebApplicationContext();
    final MockServletContext servletContext = new MockServletContext();

    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appContext);

    appContext.setServletContext(servletContext);
    appContext.setConfigLocations(locations);
    appContext.refresh();
    appContext.registerShutdownHook();

    return appContext;

}