spring3 緩存註解

作一個自定義的緩存註解策略,好比要在新增、修改的操做時,使用自定義註解更靈活的去清除指定的緩存:java

spring本身的CacheEvict中key="#user.id" 是能起做用的,在Cacheable..中去使用spel均可以獲取入參的信息spring

可是我本身定義的註解MyCacheEvict裏,在屬性中同樣的表達式去獲取方法入參信息卻拿不到值。數組

是須要額外加入什麼配置才能使用springEL嗎?跪求各位大神解惑!!緩存

service方法截圖:app

import org.springframework.cache.annotation.CacheEvict;  
import com.example.common.cacheCustomer.MyCacheEvict;

@CacheEvict(value = "users",key = "'user_'.concat(#user.id)")    
@MyCacheEvict(value = "users",key ="'user_'.concat(#user.id)",  keys = {"#{user.id}", "list"}, keyRegex = "list")
// spel: #user.id  或者 #user.getId()
public int updateOne(Users user){        
    return userMapper.updateByPrimaryKeySelective(user);
}

自定義註解:ide

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Component
@Documented
public @interface MyCacheEvict{
    //設置default值,則屬性爲非必填項
    String value(); //cacheName
    String key() default "" ; //key
    String[] keys() default {} ; //key 數組
    String keyRegex() default "" ; //key 模糊匹配
    boolean allEntries() default false ; //是否清除此 cache 下全部緩存    
}

aop 配置緩存策略:spa

@Pointcut("@annotation(com.example.common.cacheCustomer.MyCacheEvict)")
//聲明切點
 public void annotationPointCut(){};
 @Autowired
 CacheManager cacheManager;

 @After("annotationPointCut()")
 public void after(JoinPoint joinpoint){
    MethodSignature signature = (MethodSignature) joinpoint.getSignature();
    Method method = signature.getMethod();
    MyCacheEvict myCacheEvict = method.getAnnotation(MyCacheEvict.class);
    //反射獲得註解上的屬性
    String value = myCacheEvict.value();
    String key = myCacheEvict.key();
    String regex = myCacheEvict.keyRegex();
    String[] keys = myCacheEvict.keys();
    boolean allEntries = myCacheEvict.allEntries();
    System.out.println("註解式緩存策略---"+value+"-"+key);//
    Cache cache = cacheManager.getCache(value);
    Element element = cache.get(key);
    System.out.println(element);
 }
 @Before("annotationPointCut()")
 public void before(JoinPoint joinpoint){

 }

http://blog.csdn.net/lsgqjh/article/details/54381202.net

相關文章
相關標籤/搜索