Search code examples
javaspringapplicationcontextservlet-listeners

How to get spring ApplicationContext in ServletContextListener?


In servet class it's possible to get spring ApplicationContext this way

ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());

How to access ApplicationContext in ServletContextListener since getServletContext() method is not accessible there?


Solution

  • In contextInitialized :

    public void contextInitialized(final ServletContextEvent event)
    {
        ApplicationContext appCtx = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
    }