當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Cache抽象-使用SpEL表达式
生活随笔
收集整理的這篇文章主要介紹了
Spring Cache抽象-使用SpEL表达式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- 概述
- SpEl表達式
概述
在Spring Cache注解屬性中(比如key,condition和unless),Spring的緩存抽象使用了SpEl表達式,從而提供了屬性值的動態生成及足夠的靈活性。
下面的代碼根據用戶的userCode進行緩存,對于key屬性,使用了表達式自定義鍵的生成。
public class UserService {private Map<Integer, User> users = new HashMap<Integer, User>();{users.put(1, new User("1", "w1",37));users.put(2, new User("2", "w2", 34));}@Cacheable(value = "users", key = "#user.userCode" condition = "#user.age < 35")public User getUser(User user) {System.out.println("User with id " + user.getUserId() + " requested.");return users.get(Integer.valueOf(user.getUserId()));}SpEl表達式
SpEL表達式可基于上下文并通過使用緩存抽象,提供與root獨享相關聯的緩存特定的內置參數。
| methodName | root對象 | 當前被調用的方法名 | #root.methodname |
| method | root對象 | 當前被調用的方法 | #root.method.name |
| target | root對象 | 當前被調用的目標對象實例 | #root.target |
| targetClass | root對象 | 當前被調用的目標對象的類 | #root.targetClass |
| args | root對象 | 當前被調用的方法的參數列表 | #root.args[0] |
| caches | root對象 | 當前方法調用使用的緩存列表 | #root.caches[0].name |
| Argument Name | 執行上下文 | 當前被調用的方法的參數,如findArtisan(Artisan artisan),可以通過#artsian.id獲得參數 | #artsian.id |
| result | 執行上下文 | 方法執行后的返回值(僅當方法執行后的判斷有效,如 unless cacheEvict的beforeInvocation=false) | #result |
總結
以上是生活随笔為你收集整理的Spring Cache抽象-使用SpEL表达式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Cache抽象-缓存管理器
- 下一篇: Spring Cache抽象-基于XML