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

The following examples show how to use org.springframework.web.context.support.XmlWebApplicationContext#refresh() . 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: 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 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: 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 4
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 5
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 6
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 7
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-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 8
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 9
Source File: DispatcherServletChannelInitializer.java    From nettyholdspringmvc with Apache License 2.0 6 votes vote down vote up
public DispatcherServletChannelInitializer() throws ServletException {

    	MockServletContext servletContext = new MockServletContext();
    	MockServletConfig servletConfig = new MockServletConfig(servletContext);
        servletConfig.addInitParameter("contextConfigLocation","classpath:/META-INF/spring/root-context.xml");
        servletContext.addInitParameter("contextConfigLocation","classpath:/META-INF/spring/root-context.xml");

    	//AnnotationConfigWebApplicationContext wac = new AnnotationConfigWebApplicationContext();
        XmlWebApplicationContext wac = new XmlWebApplicationContext();

        //ClassPathXmlApplicationContext wac = new ClassPathXmlApplicationContext();
		wac.setServletContext(servletContext);
		wac.setServletConfig(servletConfig);
        wac.setConfigLocation("classpath:/servlet-context.xml");
    	//wac.register(WebConfig.class);
    	wac.refresh();

    	this.dispatcherServlet = new DispatcherServlet(wac);
    	this.dispatcherServlet.init(servletConfig);
	}
 
Example 10
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 11
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 12
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 13
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 14
Source File: StreamingReceiver.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
private void startHttpServer() throws Exception {
    KylinConfig kylinConfig = KylinConfig.getInstanceFromEnv();
    createAndConfigHttpServer(kylinConfig);

    ContextHandlerCollection contexts = new ContextHandlerCollection();

    ServletContextHandler context = new ServletContextHandler();
    context.setContextPath("/kylin");

    XmlWebApplicationContext ctx = new XmlWebApplicationContext();
    ctx.setConfigLocation("classpath:applicationContext.xml");
    ctx.refresh();
    DispatcherServlet dispatcher = new DispatcherServlet(ctx);
    context.addServlet(new ServletHolder(dispatcher), "/api/*");

    ContextHandler logContext = new ContextHandler("/kylin/logs");
    String logDir = getLogDir(kylinConfig);
    ResourceHandler logHandler = new ResourceHandler();
    logHandler.setResourceBase(logDir);
    logHandler.setDirectoriesListed(true);
    logContext.setHandler(logHandler);

    contexts.setHandlers(new Handler[] { context, logContext });
    httpServer.setHandler(contexts);
    httpServer.start();
    httpServer.join();
}
 
Example 15
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 16
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 17
Source File: TestContextLoader.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public ApplicationContext loadContext (String... locations) throws Exception
{
   XmlWebApplicationContext ctx = new XmlWebApplicationContext ();
   ctx.setConfigLocations (locations);
   ctx.getEnvironment ().setActiveProfiles ("test");
   ctx.refresh ();
   AnnotationConfigUtils.registerAnnotationConfigProcessors (
      (BeanDefinitionRegistry) ctx.getBeanFactory ());
   ctx.registerShutdownHook ();
   return ctx;
}
 
Example 18
Source File: KualiInitializeListener.java    From rice with Educational Community License v2.0 4 votes vote down vote up
/**
 * ServletContextListener interface implementation that schedules the start of the lifecycle
 */
@Override
public void contextInitialized(ServletContextEvent sce) {
    long startInit = System.currentTimeMillis();
    LOG.info("Initializing Kuali Rice Application...");

    // Stop Quartz from "phoning home" on every startup
    System.setProperty("org.terracotta.quartz.skipUpdateCheck", "true");

    List<String> configLocations = new ArrayList<String>();
    String additionalConfigLocations = System.getProperty(KewApiConstants.ADDITIONAL_CONFIG_LOCATIONS_PARAM);
    if (!StringUtils.isBlank(additionalConfigLocations)) {
        String[] additionalConfigLocationArray = additionalConfigLocations.split(",");
        for (String additionalConfigLocation : additionalConfigLocationArray) {
            configLocations.add(additionalConfigLocation);
        }
    }

    String bootstrapSpringBeans = "";
    if (!StringUtils.isBlank(System.getProperty(WEB_BOOTSTRAP_SPRING_FILE))) {
        bootstrapSpringBeans = System.getProperty(WEB_BOOTSTRAP_SPRING_FILE);
    } else if (!StringUtils.isBlank(sce.getServletContext().getInitParameter(WEB_BOOTSTRAP_SPRING_FILE))) {
        String bootstrapSpringInitParam = sce.getServletContext().getInitParameter(WEB_BOOTSTRAP_SPRING_FILE);
        // if the value comes through as ${bootstrap.spring.beans}, we ignore it
        if (!DEFAULT_SPRING_BEANS_REPLACEMENT_VALUE.equals(bootstrapSpringInitParam)) {
            bootstrapSpringBeans = bootstrapSpringInitParam;
            LOG.info("Found bootstrap Spring Beans file defined in servlet context: " + bootstrapSpringBeans);
        }
    }

    Properties baseProps = new Properties();
    baseProps.putAll(getContextParameters(sce.getServletContext()));
    baseProps.putAll(System.getProperties());
    JAXBConfigImpl config = new JAXBConfigImpl(baseProps);
    ConfigContext.init(config);

    context = new XmlWebApplicationContext();
    if (!StringUtils.isEmpty(bootstrapSpringBeans)) {
        context.setConfigLocation(bootstrapSpringBeans);
    }
    context.setServletContext(sce.getServletContext());

    // Provide an optional method for bootstrapping a Spring property source
    Optional<PropertySource<?>> ps = PropertySources.getPropertySource(sce, "web.bootstrap.spring.psc");
    if (ps.isPresent()) {
        PropertySources.addFirst(context, ps.get());
    }

    try {
        context.refresh();
    } catch (RuntimeException e) {
        LOG.error("problem during context.refresh()", e);

        throw e;
    }

    context.start();
    long endInit = System.currentTimeMillis();
    LOG.info("...Kuali Rice Application successfully initialized, startup took " + (endInit - startInit) + " ms.");
}
 
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;

}