从同一类中调用时,Spring cache @Cacheable方法将被忽略 [英] Spring cache @Cacheable method ignored when called from within the same class
问题描述
我正在尝试从同一类中调用@Cacheable
方法:
@Cacheable(value = "defaultCache", key = "#id")
public Person findPerson(int id) {
return getSession().getPerson(id);
}
public List<Person> findPersons(int[] ids) {
List<Person> list = new ArrayList<Person>();
for (int id : ids) {
list.add(findPerson(id));
}
return list;
}
,并希望也缓存findPersons
的结果,但是忽略@Cacheable
注释,并且每次都执行findPerson
方法.
我在这里做错什么了吗?还是故意的?
这是因为在Spring中创建了用于处理缓存和事务相关功能的代理的方式.这是Spring如何处理它的很好参考- and hoping that the results from Am I doing something wrong here, or this is intended? This is because of the way proxies are created for handling caching, transaction related functionality in Spring. This is a very good reference of how Spring handles it - Transactions, Caching and AOP: understanding proxy usage in Spring In short, a self call bypasses the dynamic proxy and any cross cutting concern like caching, transaction etc which is part of the dynamic proxies logic is also bypassed. The fix is to use AspectJ compile time or load time weaving. 这篇关于从同一类中调用时,Spring cache @Cacheable方法将被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!findPersons
are cached as well, but the @Cacheable
annotation is ignored, and findPerson
method got executed everytime.