title: Can Spring's @Cacheable annotation have the same scope as the annotated method's bean?
body: <p>Is there an easy way to use Spring's <code>@Cacheable</code> annotation with a non-singleton (e.g. session-scoped) bean and have the cache have the same scope as said bean?</p>
  
  <p>Example:</p>
  
  <pre><code>import javax.inject.Inject;
  import javax.inject.Named;
  
  import org.springframework.cache.annotation.Cacheable;
  import org.springframework.context.annotation.Scope;
  import org.springframework.security.core.context.SecurityContextHolder;
  
  @Named
  @Scope("session")
  public class UserNameRetriever {
  
      @Inject private UserDao userDao;
  
      @Cacheable("userName")
      public String getUserName() {
          return userDao.getUserByLogin((String)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getName();
      }
  
  }
  </code></pre>
  
  <p>Ideally, <code>UserNameRetriever.getUserName()</code> would fetch the username from the <code>UserDao</code> once per session, but this code actually caches application-wide.</p>