Skip to content

Example : Spring delagating Filter

johnmcclean-aol edited this page Dec 10, 2015 · 1 revision

Filters as Spring beans

By using Spring's DelegatingFilterProxy we can inject Spring beans inside Filter. With Microserver's FilterConfiguration interface, Filter implmenations can define their own configuration.

@Component
public class AutodiscoveredFilter  implements Filter, FilterConfiguration {

	@Autowired
	Bean bean;
	@Getter
	@Setter
	private static final AtomicInteger = new AtomicInteger(0);
	
	@Getter
	private static boolean beanSet = false;
	
	@Override
	public String[] getMapping() {
		return new String[] { "/*" };
	}
	public Class<? extends Filter> getFilter(){

		return org.springframework.web.filter.DelegatingFilterProxy.class;

	}
	public String getName(){
		return "autodiscoveredFilter";
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		
		called.incrementAndGet();
		if(bean!=null)
			beanSet =true;
		chain.doFilter(request, response);
	}

	@Override
	public void destroy() {
		
		
	}

	

}