title: How to use session scoped bean properly
body: <p>There are 3 classes in my Spring MVC app: a <code>UserDetailsInterceptor</code> class, an <code>MyAdvice</code> class and a <code>UserDetails</code> class (session scoped).</p>
  
  <p><strong>What I want to accomplish</strong> is simple:</p>
  
  <ol>
  <li><code>UserDetailsInterceptor</code> intercepts requests and set user's id in a session scoped <code>UserDetails</code> bean.</li>
  <li>Later on, when the method in AOP advice class is called, retrieve user's id from the session scoped <code>UserDetails</code> bean.</li>
  </ol>
  
  <p><strong>Problem</strong> (also marked in the code below):<br/></p>
  
  <ol>
  <li><code>UserDetails</code> object is null in <code>MyAdvice</code> class.</li>
  <li>In <code>UserDetailsInterceptor</code>, <code>userDetails.setUserID(request.getRemoteUser());</code> does nothing.</li>
  </ol>
  
  <p><strong>Code</strong>:</p>
  
  <p><code>UserDetailsInterceptor</code> class:</p>
  
  <pre><code>public class UserDetailsInterceptor extends HandlerInterceptorAdapter {
  
      @Autowired
      private UserDetails userDetails;
  
      public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
          //set user ID, but next line doesn't do anything for some reason (e.g. `userID` is still null)
          userDetails.setUserID(request.getRemoteUser());
          return true;
      }
  }
  </code></pre>
  
  <p><code>MyAdvice</code> class:</p>
  
  <pre><code>public class MyAdvice implements MethodInterceptor {
  
      @Autowired
      private UserDetails userDetails; //It's null
  
      @Override
      public Object invoke(MethodInvocation invocation) throws Throwable {
          //Print user ID
          System.out.println(userDetails.getID());
          return invocation.proceed();
      }
  }
  </code></pre>
  
  <p><code>UserDetails</code> class:</p>
  
  <pre><code>public class UserDetails {
  
      private String userID;
  
      public void setUserID(String userID) {
          this.userID= userID;
      }
  
      public String getUserID() {
          return this.userID;
      }
  }
  </code></pre>
  
  <p>In <code>dispatcher-servlet.xml</code>:</p>
  
  <pre><code>&lt;bean id="userDetails " class="package.UserDetails " scope="session"&gt;
      &lt;aop:scoped-proxy/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p><code>MyPointcutAdvisor</code> class:</p>
  
  <pre><code>public class MyPointcutAdvisor implements PointcutAdvisor {
  
      private MyPointcut pointcut = new MyPointcut();
      private MyAdvice advice = new MyAdvice();
  
      @Override
      public Pointcut getPointcut() {
          return this.pointcut;
      }
  
      @Override
      public Advice getAdvice() {
          return this.advice;
      }
  
      @Override
      public boolean isPerInstance() {
          return false;
      }
  }
  </code></pre>
  
  <p>Any ideas please? Thanks in advance.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>By registering <code>MyAdvice</code> class, <code>userDetails</code> object in it is no longer null. However it is not the same object as the one in <code>UserDetailsInterceptor</code>. So the bean is not actually "session scoped"?</p>
  
  <p><strong>Answer</strong>:</p>
  
  <p>The problem lies in following code:</p>
  
  <pre><code>private MyPointcut pointcut = new MyPointcut();
  private MyAdvice advice = new MyAdvice();
  </code></pre>
  
  <p>Neither of them are managed by spring. As a result, things are being wired and not working the way we expected.</p>