Access to spring beans from OncePerRequestFilter

filter, spring, spring-mvc

Solution

You filter is being configured outside the Spring container. Hence your @Autowired dependency is not injected.

To have your Filter bean managed by Spring without also tightly coupling it to Spring infrastructure through the use of SpringBeanAutowiringSupport suggested , you can use the DelegatingFilterProxy abstraction

Define AuthCheckFilter filter as a bean in your application context e.g

@Bean
public Filter authCheckFilter(){
     AuthCheckFilter filter = new AuthCheckFilter();
     //supply dependencies
     return filter;
}

Then in your web.xml specify your filter with filter-class as org.springframework.web.filter.DelegatingFilterProxy and the filter name must match the authCheckFilter bean name in the context

At runtime DelegatingFilterProxy filter will delegate to a fully configured bean with the name authCheckFilter in the context (which must be a Filter)

<filter>
  <filter-name>authFilterCheck</filter-name>
  <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>    
<filter-mapping>
  <filter-name>authCheckFilter</filter-name>
  <url-pattern>/test/api/*</url-pattern>
</filter-mapping>

With this setup you wont have to worry about the lifecyles of your filter , root context or servlet

Problem

I want to access a spring component from OncePerRequestFilter class, but I am getting null pointer when I access service, and I think the reason for that is the configuration. I think the filter is getting called before the spring dispatcher servlet due to the configuration. Any good way to get this done, suggestions please. ``` <servlet> <servlet-name>SpringDispatcherServer</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/springConfig.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringDispatcherServer</servlet-name> <url-pattern>/test/api/*</url-pattern> </servlet-mapping> <filter> <filter-name>AuthCheck</filter-name> <filter-class>org.test.util.AuthCheckFilter</filter-class> </filter> <filter-mapping> <filter-name>AuthCheck</filter-name> <url-pattern>/test/api/*</url-pattern> </filter-mapping>` public class AuthCheckFilter extends OncePerRequestFilter { @Autowired private AuthCheckService authCheckService; @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { log.info("authCheckService"+authCheckService); ``` and the logger prints null for "authCheckService"

Original source

Related problems