Search in sources :

Example 1 with XmlWebApplicationContext

use of org.springframework.web.context.support.XmlWebApplicationContext in project spring-framework by spring-projects.

the class SimpleUrlHandlerMappingTests method handlerBeanNotFound.

@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());
    }
}
Also used : XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) FatalBeanException(org.springframework.beans.FatalBeanException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 2 with XmlWebApplicationContext

use of org.springframework.web.context.support.XmlWebApplicationContext in project spring-framework by spring-projects.

the class EnvironmentSystemIntegrationTests method xmlWebApplicationContext.

@Test
public void xmlWebApplicationContext() {
    AbstractRefreshableWebApplicationContext ctx = new XmlWebApplicationContext();
    ctx.setConfigLocation("classpath:" + XML_PATH);
    ctx.setEnvironment(prodWebEnv);
    ctx.refresh();
    assertHasEnvironment(ctx, prodWebEnv);
    assertEnvironmentBeanRegistered(ctx);
    assertEnvironmentAwareInvoked(ctx, prodWebEnv);
    assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false));
    assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true));
}
Also used : XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) AbstractRefreshableWebApplicationContext(org.springframework.web.context.support.AbstractRefreshableWebApplicationContext) Test(org.junit.Test)

Example 3 with XmlWebApplicationContext

use of org.springframework.web.context.support.XmlWebApplicationContext in project simple-java by angryziber.

the class SpringAwareResource method autowire.

public static void autowire(ServletContext sc, Object target) {
    XmlWebApplicationContext context = (XmlWebApplicationContext) WebApplicationContextUtils.getWebApplicationContext(sc);
    context.getBeanFactory().autowireBean(target);
}
Also used : XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext)

Example 4 with XmlWebApplicationContext

use of org.springframework.web.context.support.XmlWebApplicationContext in project spring-security by spring-projects.

the class HttpInterceptUrlTests method loadConfig.

private void loadConfig(String... configLocations) {
    for (int i = 0; i < configLocations.length; i++) {
        configLocations[i] = getClass().getName().replaceAll("\\.", "/") + "-" + configLocations[i];
    }
    XmlWebApplicationContext context = new XmlWebApplicationContext();
    context.setConfigLocations(configLocations);
    context.setServletContext(new MockServletContext());
    context.refresh();
    this.context = context;
    context.getAutowireCapableBeanFactory().autowireBean(this);
    Filter springSecurityFilterChain = context.getBean("springSecurityFilterChain", Filter.class);
    mockMvc = MockMvcBuilders.standaloneSetup(new FooController()).addFilters(springSecurityFilterChain).build();
}
Also used : XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) Filter(javax.servlet.Filter) MockServletContext(org.springframework.mock.web.MockServletContext)

Example 5 with XmlWebApplicationContext

use of org.springframework.web.context.support.XmlWebApplicationContext in project spring-framework by spring-projects.

the class XmlWebApplicationContextTests method createContext.

@Override
protected ConfigurableApplicationContext createContext() throws Exception {
    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) {
            beanFactory.addBeanPostProcessor(new BeanPostProcessor() {

                @Override
                public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
                    if (bean instanceof TestBean) {
                        ((TestBean) bean).getFriends().add("myFriend");
                    }
                    return bean;
                }

                @Override
                public Object postProcessAfterInitialization(Object bean, String name) throws BeansException {
                    return bean;
                }
            });
        }
    });
    root.refresh();
    XmlWebApplicationContext wac = new XmlWebApplicationContext();
    wac.getEnvironment().addActiveProfile("wacProfile1");
    wac.setParent(root);
    wac.setServletContext(sc);
    wac.setNamespace("test-servlet");
    wac.setConfigLocations(new String[] { "/org/springframework/web/context/WEB-INF/test-servlet.xml" });
    wac.refresh();
    return wac;
}
Also used : XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) TestBean(org.springframework.tests.sample.beans.TestBean) BeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor) MockServletContext(org.springframework.mock.web.test.MockServletContext) BeanFactoryPostProcessor(org.springframework.beans.factory.config.BeanFactoryPostProcessor) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Aggregations

XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)12 MockServletContext (org.springframework.mock.web.test.MockServletContext)7 Test (org.junit.Test)4 Before (org.junit.Before)3 MockServletContext (org.springframework.mock.web.MockServletContext)2 Filter (javax.servlet.Filter)1 ServletContextEvent (javax.servlet.ServletContextEvent)1 ServletContextListener (javax.servlet.ServletContextListener)1 FatalBeanException (org.springframework.beans.FatalBeanException)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 BeanFactoryPostProcessor (org.springframework.beans.factory.config.BeanFactoryPostProcessor)1 BeanPostProcessor (org.springframework.beans.factory.config.BeanPostProcessor)1 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)1 NoSuchMessageException (org.springframework.context.NoSuchMessageException)1 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)1 LifecycleBean (org.springframework.tests.sample.beans.LifecycleBean)1 TestBean (org.springframework.tests.sample.beans.TestBean)1 AbstractRefreshableWebApplicationContext (org.springframework.web.context.support.AbstractRefreshableWebApplicationContext)1 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)1 HandlerMapping (org.springframework.web.servlet.HandlerMapping)1