Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Example Filter that binds spring-managed beans into the request
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;
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