How to make Spring @Cacheable work on top of AspectJ aspect?

大兔子大兔子 提交于 2019-12-01 08:25:43

You can achieve similar results, by using Spring regular dependency injection mechanism and inject a org.springframework.cache.CacheManager into your aspect:

@Autowired
CacheManager cacheManager;

Then you can use the cache manager in the around advice:

@Around( "call(* *.getProperty(..))" )
public Object intercepting( ProceedingJoinPoint pjp ) throws Throwable
{
    Cache cache = cacheManager.getCache("aopCache");
    String key = "whatEverKeyYouGenerateFromPjp";
    Cache.ValueWrapper valueWrapper = cache.get(key);
    if (valueWrapper == null) {
        Object o;
        /* { modify o } */
        cache.put(key, o); 
        return o;
    }
    else {
        return valueWrapper.get();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!