Is this a reasonable way to get the WebApplicationContext from a ServletListener (since listeners can't be injected out of the box)? Are there any ramifications in regards to listener registration order, specifically does it matter if this goes before or after ContextLoaderListener? I copied the technique from FrameworkServlet and it works, but I am unaware of any Spring internals or invariants I might be goofing. Please advise... I am up to a better way of doing this, but so far, this seems clean enough
Code:public class ProductImageCleanupListener implements ServletContextListener { private static final Log logger = LogFactory.getLog(ProductImageCleanupListener.class); public void contextDestroyed(ServletContextEvent event) { logger.debug("server shutting down. deleting orphaned product images"); WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext()); ProductService productService = (ProductService) wac.getBean("productService"); productService.deleteOrphanedProductImages(); } public void contextInitialized(ServletContextEvent event) { } }


Reply With Quote