Spring 3.1缓存 – 如何在SpEL中使用返回值

我试图在Spring管理的缓存(Spring 3.1抽象)中驱逐一个条目。

我需要在注释中的“key”属性的SpEL中引用方法的返回值:

/* (How to refer to the 'T' returned value in the "KEY_ID"?) */ @Caching(evict = { @CacheEvict(value = CACHE_BY_ID, key = KEY_ID) }) public T delete(AppID appID, UserID userID) throws UserNotFoundException { return inner.delete(appID, userID); } 

有没有办法做到这一点?

似乎没有任何方法可以引用返回的对象:

http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/cache.html#cache-spel-context

但为什么你需要这样做? 您可以参考@CacheEvict“key”值中的参数,例如:

 @CacheEvict(value = CACHE_BY_ID, key = "#userID") public T delete(AppID appID, UserID userID) throws UserNotFoundException { ... } 

响应下面的响应的更多示例代码,关于必须使用User对象的多个属性从多个缓存中逐出:

 @Caching(evict = { @CacheEvict(value = CACHE_BY_ID, key = "#user.userID"), @CacheEvict(value = CACHE_BY_LOGIN_NAME, key = "#user.loginName") // etc. }) public T delete(AppID appID, User user) throws UserNotFoundException { ... }