Skip to content

Tag: servlet-filters

How can I get a Spring bean in a servlet filter?

I have defined a javax.servlet.Filter and I have Java class with Spring annotations. I want to get the bean UsersConnectionRepository in my Filter, so I tried the following: But it always returns null. How can I get a Spring bean in a Filter? Answer Try: Where usersConnectionRepository is a name/id of your bean in the application context. Or even better:

What is chain.doFilter doing in Filter.doFilter method?

In a Filter.doFilter method I made this call chain.doFilter. What is doFilter doing inside a doFilter? Isn’t it a recursive call? Answer Servlet Filters are an implementation of the Chain of responsibility design pattern. All filters are chained (in the order of their definition in web.xml). The chain.doFilter() is proceeding to the next element in the chain. The last element