Springified Servlets, Listeners and Filters

Here are some short, but fully functional, templates for when you have a need for HttpServlet, ContextListener or Filter implementations, that also need spring bean dependencies.

You use them by subclassing them and calling the getBean method when a dependency is needed, like this:

// ..blah blah..inside subclass
MyService myService = getBean("myService");

Okay, here they come..

SpringHttpServlet

package net.techper.server.web;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

public abstract class SpringHttpServlet extends HttpServlet {

  private ApplicationContext context;

  @Override
  public void init(ServletConfig config) throws ServletException {
    super.init(config);
    context = WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext());
  }

  @Override
  public void destroy() {
    context = null;
    super.destroy();
  }

  protected <T> T getBean(String beanName) {
    return (T) context.getBean(beanName);
  }
}

SpringContextListener

package net.techper.server.web;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public abstract class SpringContextListener implements ServletContextListener {

  private ApplicationContext context;

  @Override
  public void contextInitialized(ServletContextEvent sce) {
    context = WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext());
  }

  @Override
  public void contextDestroyed(ServletContextEvent sce) {
    context = null;
  }

  protected <T> T getBean(String beanName) {
    return (T) context.getBean(beanName);
  }
}

SpringFilter

package net.techper.server.web;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

public abstract class SpringFilter implements Filter {

  private ApplicationContext context;

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {
    context = WebApplicationContextUtils.getRequiredWebApplicationContext(filterConfig.getServletContext());
  }

  @Override
  public void destroy() {
    context = null;
  }

  protected <T> T getBean(String beanName) {
    return (T) context.getBean(beanName);
  }
}

March 5, 2011 В· polesen В· 2 Comments
Tags: , , , ,  В· Posted in: Programming

2 Responses

  1. Josh Long - March 7, 2011

    Hiya, nice post! I just wanted to also point out that you can use Spring’s DelegatingFilterProxy for filters, and Spring’s HttpRequestHandlerServlet for servlet-like functionality. Both of these support delegating to a Spring bean with the same bean name as the respective entity in web.xml. I’m not aware of a similar thing for ServletContextListeners, so your implementation is quite useful there. I’d advise a little care since that case poses something of a chicken-and-egg problem, since the root application context is usually loaded by a servlet context listener, too. Peace, and keep up the cool blogs! ;-)

  2. polesen - March 7, 2011

    Hey cool. Didn’t know about those. Thanks Josh.