aryanRaj_kary
aryanRaj_kary

Reputation: 503

Access to spring beans from OncePerRequestFilter

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"

Upvotes: 4

Views: 12564

Answers (3)

Satyendra Kumar
Satyendra Kumar

Reputation: 367

I don't know why @Autowire is not working, but I got it working by using setter injection.

For details:

Include this in your applicationContext.xml file

    <bean name="AuthCheckFilter" class="x.y.AuthCheckFilter">
       <property name="authCheckService" ref="authCheckService"/>
    </bean>

    <bean name="authCheckService" class="x.y.AuthCheckService"/>

Remember to provide a setter in AuthCheckFilter class for AuthCheckService.

Include this in your web.xml:

 <filter>
    <filter-name>AuthCheckFilter</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>

That's it. Now if you hit the url, you will get the non-null authCheckService.

Upvotes: 2

ekem chitsiga
ekem chitsiga

Reputation: 5753

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

Upvotes: 4

kuhajeyan
kuhajeyan

Reputation: 11017

add this in your init() OncePerRequestFilter, so spring can wire your autowired beans

public void init(FilterConfig filterConfig) throws ServletException {
    SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
            filterConfig.getServletContext());
}

Upvotes: 1

Related Questions