spring 无法为 CacheableOperation[] 缓存找到名为 '' 的缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28020245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot find cache named '' for CacheableOperation[] caches
提问by Kamlesh Kanazariya
My error is :
我的错误是:
Exception in thread "main" java.lang.IllegalArgumentException: Cannot find cache named 'getActionsBycasId' for CacheableOperation[public java.util.List com.codinko.database.DataBaseConnection.getActionsByCasId(int)] caches=[getActionsBycasId] | key='' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless=''
at org.springframework.cache.interceptor.AbstractCacheResolver.resolveCaches(AbstractCacheResolver.java:81)
at org.springframework.cache.interceptor.CacheAspectSupport.getCaches(CacheAspectSupport.java:214)
at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContext.<init>(CacheAspectSupport.java:553)
at org.springframework.cache.interceptor.CacheAspectSupport.getOperationContext(CacheAspectSupport.java:227)
at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContexts.<init>(CacheAspectSupport.java:498)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:299)
at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
at com.codinko.database.DataBaseConnection$$EnhancerBySpringCGLIB$a0d8a.getActionsByCasId(<generated>)
at com.codinko.caching.EmployeeDAO.getActionBycasId(EmployeeDAO.java:47)
at com.codinko.caching.EmployeeDAO$$FastClassBySpringCGLIB$1aa49b.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:649)
at com.codinko.caching.EmployeeDAO$$EnhancerBySpringCGLIB$99d753.getActionBycasId(<generated>)
at com.codinko.caching.Main.main(Main.java:22)
My function is :
我的功能是:
@Cacheable("getActionsBycasId")
public List<SMSAction> getActionsByCasId(int casId){
System.out.println("Inside getActionsByCasId");
//My logic
return list;
}
when i add below on ehcache.xml then above error not comes but can't know why this error come .
当我在 ehcache.xml 上添加以下内容时,不会出现上述错误,但不知道为什么会出现此错误。
<cache name="getActionsBycasId" maxElementsInMemory="50" eternal="false"
overflowToDisk="false" memoryStoreEvictionPolicy="LFU" />
Is this above configuration required in ehcache.xml file even though i used annotation
????
即使我使用了 ehcache.xml 文件,是否也需要上述配置annotation
????
采纳答案by Piotr 'Kowcio' Kowalski
Try this :
尝试这个 :
@Bean
public CacheManager cacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
List<Cache> caches = new ArrayList<Cache>();
caches.add(new ConcurrentMapCache("getActionsBycasId"));
cacheManager.setCaches(caches);
return cacheManager;
}
回答by serdar
If you use spring cloud aws, disable automatic elasticache configuration.
如果您使用 spring cloud aws,请禁用自动 elasticache 配置。
@EnableAutoConfiguration(exclude = ElastiCacheAutoConfiguration.class)
@EnableCaching
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
回答by Ken007
@SpringBootApplication(exclude = {
ContextStackAutoConfiguration.class,
ElastiCacheAutoConfiguration.class
})
Just being nit-picky use @SpringBootApplicationinstead of @EnableAutoConfiguration
只是挑剔使用@SpringBootApplication而不是@EnableAutoConfiguration
回答by Viktor Reinok
Try changing
尝试改变
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="cacheManagerName" value="cacheManager_service" />
<property name="configLocation" value="classpath:ehcache-services.xml" />
<property name="shared" value="true" />
</bean>
To
到
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" primary="true">
<property name="cacheManagerName" value="cacheManager_service" />
<property name="configLocation" value="classpath:ehcache-services.xml" />
</bean>
回答by Dracontis
You can simplify cache manager creation using following code:
您可以使用以下代码简化缓存管理器的创建:
package com.stackoverflow.config;
import org.springframework.cache.CacheManager;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CacheConfig {
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager("logos");
}
}
回答by Harsha VK
You can define ehcache.xml in resources folder and consider configuring ,, tags and set alias as your cache able method. It worked for me.
您可以在资源文件夹中定义 ehcache.xml 并考虑配置 ,, 标签和设置别名作为您的可缓存方法。它对我有用。
回答by Senthil Arumugam SP
The above exclude option, didnt work for me. But, the below worked.!! If you are using spring aws cloud, then exclude the elastic like below.
上述排除选项对我不起作用。但是,下面的工作。!如果您使用的是 spring aws cloud,则排除如下弹性。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-aws-messaging</artifactId>
<exclusions>
<exclusion>
<groupId>com.amazonaws</groupId>
<artifactId>
elasticache-java-cluster-client
</artifactId>
</exclusion>
</exclusions>
</dependency>