Skip to content

Instantly share code, notes, and snippets.

@twasink
Created November 2, 2011 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twasink/1333306 to your computer and use it in GitHub Desktop.
Save twasink/1333306 to your computer and use it in GitHub Desktop.
Example Filter that binds spring-managed beans into the request
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;
public class SpringMappingFilter implements Filter {
private ServletContext context;
public void destroy() { }
public void init(FilterConfig filterConfig) {
this.context = filterConfig.getServletContext();
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filter)
throws IOException, ServletException {
// the Spring Context would have been configured by the SpringContextListener
ApplicationContext springContext = this.context.getAttribute(XmlWebApplicationContext.DEFAULT_CONFIG_LOCATION);
request.setAttribute("myBean", springContext.getBean("myBean");
filterChain.doFilter(request, response);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment