Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用JCache annotation@CacheRemoveAll清除多个缓存?_Java_Spring_Ehcache_Jcache - Fatal编程技术网

Java 如何使用JCache annotation@CacheRemoveAll清除多个缓存?

Java 如何使用JCache annotation@CacheRemoveAll清除多个缓存?,java,spring,ehcache,jcache,Java,Spring,Ehcache,Jcache,我有一个方法,它的执行应该清除Spring+JCache+ehcache 3.5项目中的两个缓存 我试过: @CacheRemoveAll(cacheName = "cache1") @CacheRemoveAll(cacheName = "cache2") public void methodToBeCalled(){ } 及 首先,我得到: Duplicate annotation of non-repeatable type @CacheRemoveAll 在第二分钟,我得到: Dup

我有一个方法,它的执行应该清除Spring+JCache+ehcache 3.5项目中的两个缓存

我试过:

@CacheRemoveAll(cacheName = "cache1")
@CacheRemoveAll(cacheName = "cache2")
public void methodToBeCalled(){
}

首先,我得到:

Duplicate annotation of non-repeatable type @CacheRemoveAll
在第二分钟,我得到:

Duplicate attribute cacheName in annotation @CacheRemoveAll

你不能。注释不能重复,属性不能重复

您需要一个
@CacheRemoveAlls
注释,但框架没有计划这样做

最好的解决方案是在
methodToBeCalled
的开头为两个缓存调用
removeAll

代码如下所示:

public class MyClass {
    @Autowired
    private CacheManager cacheManager; // this is a Spring CacheManager

    public void methodToBeCalled(){
        cacheManager.getCache("cache1").clear();
        cacheManager.getCache("cache2").clear();
    }
}

如何在Java代码中调用此方法?我这样问是因为我使用ehcache.xml和SpringXMLServlet配置设置了缓存,并且我只通过注释使用它。那么我如何在代码中引用它呢?就像春天的一切一样。你注射它。我会更新我的答案给你看。
public class MyClass {
    @Autowired
    private CacheManager cacheManager; // this is a Spring CacheManager

    public void methodToBeCalled(){
        cacheManager.getCache("cache1").clear();
        cacheManager.getCache("cache2").clear();
    }
}