Search in sources :

Example 1 with ContextLoaderListener

use of org.springframework.web.context.ContextLoaderListener in project JessMA by ldcsaa.

the class SpringInjectFilter method init.

@Override
public void init() {
    servletContext = HttpHelper.getServletContext();
    springMap = new HashMap<CoupleKey<Class<?>, Method>, SpringAttr[]>();
    context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    if (context == null) {
        listener = new ContextLoaderListener();
        listener.contextInitialized(new ServletContextEvent(servletContext));
        context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
    }
}
Also used : ContextLoaderListener(org.springframework.web.context.ContextLoaderListener) CoupleKey(org.jessma.util.CoupleKey) ServletContextEvent(javax.servlet.ServletContextEvent)

Example 2 with ContextLoaderListener

use of org.springframework.web.context.ContextLoaderListener in project spring-boot by spring-projects.

the class SpringBootServletInitializer method onStartup.

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    // Logger initialization is deferred in case a ordered
    // LogServletContextInitializer is being used
    this.logger = LogFactory.getLog(getClass());
    WebApplicationContext rootAppContext = createRootApplicationContext(servletContext);
    if (rootAppContext != null) {
        servletContext.addListener(new ContextLoaderListener(rootAppContext) {

            @Override
            public void contextInitialized(ServletContextEvent event) {
            // no-op because the application context is already initialized
            }
        });
    } else {
        this.logger.debug("No ContextLoaderListener registered, as " + "createRootApplicationContext() did not " + "return an application context");
    }
}
Also used : ContextLoaderListener(org.springframework.web.context.ContextLoaderListener) ServletContextEvent(javax.servlet.ServletContextEvent) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Example 3 with ContextLoaderListener

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

the class Spr8510Tests method abstractRefreshableWAC_respectsProgrammaticConfigLocations.

@Test
public void abstractRefreshableWAC_respectsProgrammaticConfigLocations() {
    XmlWebApplicationContext ctx = new XmlWebApplicationContext();
    ctx.setConfigLocation("programmatic.xml");
    ContextLoaderListener cll = new ContextLoaderListener(ctx);
    MockServletContext sc = new MockServletContext();
    try {
        cll.contextInitialized(new ServletContextEvent(sc));
        fail("expected exception");
    } catch (Throwable t) {
        // assert that an attempt was made to load the correct XML
        assertTrue(t.getMessage(), t.getMessage().endsWith("Could not open ServletContext resource [/programmatic.xml]"));
    }
}
Also used : ContextLoaderListener(org.springframework.web.context.ContextLoaderListener) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContextEvent(javax.servlet.ServletContextEvent) Test(org.junit.Test)

Example 4 with ContextLoaderListener

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

the class AbstractSecurityWebApplicationInitializer method onStartup.

/*
	 * (non-Javadoc)
	 *
	 * @see org.springframework.web.WebApplicationInitializer#onStartup(javax.servlet.
	 * ServletContext)
	 */
public final void onStartup(ServletContext servletContext) throws ServletException {
    beforeSpringSecurityFilterChain(servletContext);
    if (this.configurationClasses != null) {
        AnnotationConfigWebApplicationContext rootAppContext = new AnnotationConfigWebApplicationContext();
        rootAppContext.register(this.configurationClasses);
        servletContext.addListener(new ContextLoaderListener(rootAppContext));
    }
    if (enableHttpSessionEventPublisher()) {
        servletContext.addListener("org.springframework.security.web.session.HttpSessionEventPublisher");
    }
    servletContext.setSessionTrackingModes(getSessionTrackingModes());
    insertSpringSecurityFilterChain(servletContext);
    afterSpringSecurityFilterChain(servletContext);
}
Also used : ContextLoaderListener(org.springframework.web.context.ContextLoaderListener) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext)

Example 5 with ContextLoaderListener

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

the class Spr8510Tests method abstractRefreshableWAC_fallsBackToConventionBasedNaming.

/**
	 * If context config locations have been specified neither against the application
	 * context nor the context loader listener, then fall back to default values.
	 */
@Test
public void abstractRefreshableWAC_fallsBackToConventionBasedNaming() {
    XmlWebApplicationContext ctx = new XmlWebApplicationContext();
    //ctx.setConfigLocation("programmatic.xml"); // nothing set programmatically
    ContextLoaderListener cll = new ContextLoaderListener(ctx);
    MockServletContext sc = new MockServletContext();
    try {
        cll.contextInitialized(new ServletContextEvent(sc));
        fail("expected exception");
    } catch (Throwable t) {
        // assert that an attempt was made to load the correct XML
        System.out.println(t.getMessage());
        assertTrue(t.getMessage().endsWith("Could not open ServletContext resource [/WEB-INF/applicationContext.xml]"));
    }
}
Also used : ContextLoaderListener(org.springframework.web.context.ContextLoaderListener) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContextEvent(javax.servlet.ServletContextEvent) Test(org.junit.Test)

Aggregations

ContextLoaderListener (org.springframework.web.context.ContextLoaderListener)11 ServletContextEvent (javax.servlet.ServletContextEvent)10 Test (org.junit.Test)7 MockServletContext (org.springframework.mock.web.test.MockServletContext)7 ServletException (javax.servlet.ServletException)1 CoupleKey (org.jessma.util.CoupleKey)1 MockDatabase (org.opennms.core.test.db.MockDatabase)1 DaoTestConfigBean (org.opennms.test.DaoTestConfigBean)1 ClassPathBeanDefinitionScanner (org.springframework.context.annotation.ClassPathBeanDefinitionScanner)1 MockFilterConfig (org.springframework.mock.web.MockFilterConfig)1 MockServletConfig (org.springframework.mock.web.MockServletConfig)1 MockServletContext (org.springframework.mock.web.MockServletContext)1 OpenSessionInViewFilter (org.springframework.orm.hibernate3.support.OpenSessionInViewFilter)1 WebApplicationContext (org.springframework.web.context.WebApplicationContext)1 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)1