Announcement Announcement Module
Collapse
No announcement yet.
Dependency Injection/Inversion of Control for Servlet Listener Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dependency Injection/Inversion of Control for Servlet Listener

    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) {
    	}	
    }
Working...
X