Spring @CacheEvict not working
<p>I've experience in Spring MVC, but first time using Cache. These are steps that I've done yet.</p>

<p><strong>Step : 1</strong></p>

<p>// In spring config</p>

<pre><code>@Bean
public CacheManager cacheManager() {
    return new ConcurrentMapCacheManager("user");
}
</code></pre>

<p>// Cached Object</p>

<pre><code>public class CachedUser {   
    private String username;
    private String token;
    // Public getter-setter
}
</code></pre>

<p>// AuthServiceImp</p>

<pre><code>@Service
public class AuthServiceImp implements AuthService {

  @Override
  @Cacheable(value="user", key="#token")
  @Transactional
  public CachedUser loadUserDetailsFromDb(String username, String token) {
     // codes here
  }

  @Override
  @CacheEvict(value="user", key="#token")
  @Transactional
  public void removeUser(String username, String token) {
     // codes here
  }
}
</code></pre>

<p>// My Filter</p>

<pre><code>public class AuthenticationTokenFilter extends UsernamePasswordAuthenticationFilter {
  AuthService authService = WebApplicationContextUtils
            .getRequiredWebApplicationContext(this.getServletContext())
            .getBean(AuthService.class);
  CachedUser user = this.authService.loadUserDetailsFromDb(username, authToken);
}
</code></pre>

<p>// Controller</p>

<pre><code>@RestController
public class AuthenticationController {
  @Autowired
  private AuthService authService;
  @GetMapping("logout2")
  public ResponseModel logout(@RequestAttribute("username") String username,
        HttpServletRequest request) {
    String token = request.getHeader(tokenHeader);
    authService.removeUser(username, token);
    return new ResponseModel(200,"Success",null);
  }
}
</code></pre>

<p>Whenever calling <code>loadUserDetailsFromDb</code> from <code>AuthenticationTokenFilter</code> it returns cached object (except in first call obviously). That means <code>@Cacheable(value="user", key="#token")</code> is working fine.</p>

<p>But even after I logged out and called <code>authService.removeUser()</code>,  calling <code>loadUserDetailsFromDb()</code> fetches the cached object. That means <code>@CacheEvict(value="user", key="#token")</code> is not working.</p>

<p><strong>Step: 2</strong></p>

<p>Referred <a href="http://forum.spring.io/forum/spring-projects/container/119059-cacheevict-not-working-when-there-s-cacheable-in-the-same-class" rel="nofollow noreferrer">this</a> and moved <code>removeUser()</code> to another service ( say <code>CacheServiceImp implements CacheService</code> ), yet same problem.</p>

<p><strong>Step: 3</strong></p>

<p>Reffered <a href="https://stackoverflow.com/questions/27842637/cacheevict-not-working-while-calling-the-method-from-different-controller">this</a> and , by my understanding, moved <code>@Cache*</code> annotation to interface <code>AuthService</code>, got following error.</p>

<blockquote>
  <p>java.lang.IllegalArgumentException: Null key returned for cache
  operation (maybe you are using named params on classes without debug
  info?)</p>
</blockquote>

<p><strong>Note :</strong> Is the problem of not evicting, because I'm calling <code>@Cacheable</code> and <code>@CacheEvict</code> methods from different classes. That is from <code>AuthenticationTokenFilter</code> and <code>AuthenticationController</code></p>