title: Spring interceptor dependency injection
body: <p>I have springmvc and angularjs app up and running.
  In Springmvc i have a bean named userSessionBean.
  Now i am adding an interceptor to spring mvc and in its pre handel method i am trying to access userSessionBean.</p>
  
  <p>My question is "Can i inject userSessionBean inside interceptor "</p>
  
  <pre><code>/**
  * 
  */
  package com.loginLite.remote.authentication.interceptors;
  
  import java.util.Random;
  
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  
  
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.web.servlet.HandlerInterceptor;
  import org.springframework.web.servlet.ModelAndView;
  
  import com.remote.authentication.model.UserSessionBean;
  
  /**
   * @author jamju02
  *
  */
  public class AuthenticationInteceptor implements HandlerInterceptor {
  @Autowired
  private UserSessionBean userSessionBean = null;
  
  /**
   * @return the userSessionBean
   */
  public UserSessionBean getUserSessionBean() {
      return userSessionBean;
  }
  
  /**
   * @param userSessionBean the userSessionBean to set
   */
  public void setUserSessionBean(UserSessionBean userSessionBean) {
      userSessionBean = userSessionBean;
  }
  
  @Override
  public boolean preHandle(HttpServletRequest request,
          HttpServletResponse response, Object handler) throws Exception {
  
      System.out.println("Pre-handle");
  
      return true;
  }
  
  @Override
  public void postHandle(HttpServletRequest request,
          HttpServletResponse response, Object handler,
          ModelAndView modelAndView) throws Exception {
      Random rnd = new Random();
      int tokenNumber = 100000 + rnd.nextInt(900000);
      userSessionBean.setAuthTokenNumber(String.valueOf(tokenNumber));
       response.addHeader("AUTH_TOKEN",userSessionBean.getAuthTokenNumber());
      System.out.println("Post-handle");
  }
  
  @Override
  public void afterCompletion(HttpServletRequest request,
          HttpServletResponse response, Object handler, Exception ex)
          throws Exception {
      System.out.println("After completion handle");
  }
  
  }
  </code></pre>
  
  <p>My dispatcher servlet </p>
  
  <pre><code>&lt;mvc:interceptors&gt;
    &lt;bean class="com.paychex.loginLite.remote.authentication.interceptors.AuthenticationInteceptor"&gt;
      &lt;property name="userSessionBean" ref="userSessionBean"&gt;&lt;/property&gt;
    &lt;/bean&gt;
  &lt;/mvc:interceptors&gt; 
  
  
  &lt;bean id="userSessionBean"
      class="com.paychex.loginLite.remote.authentication.model.UserSessionBean"
      scope="session"&gt;
      &lt;aop:scoped-proxy /&gt;
  &lt;/bean&gt;
  </code></pre>