2015-01-19 5 views
11

Mein Fehler ist:nicht-Cache namens gefunden '' für CacheableOperation [] Caches

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$$21a0d8a.getActionsByCasId(<generated>) 
    at com.codinko.caching.EmployeeDAO.getActionBycasId(EmployeeDAO.java:47) 
    at com.codinko.caching.EmployeeDAO$$FastClassBySpringCGLIB$$191aa49b.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$$3399d753.getActionBycasId(<generated>) 
    at com.codinko.caching.Main.main(Main.java:22)  

Meine Funktion ist:

@Cacheable("getActionsBycasId") 
public List<SMSAction> getActionsByCasId(int casId){ 
    System.out.println("Inside getActionsByCasId"); 
    //My logic 
    return list; 
}  

wenn ich auf ehcache.xml unten fügen Sie dann oben Fehler nicht kommt, aber Ich kann nicht wissen, warum dieser Fehler kommt.

<cache name="getActionsBycasId" maxElementsInMemory="50" eternal="false" 
    overflowToDisk="false" memoryStoreEvictionPolicy="LFU" />  

Ist die obige Konfiguration in ehcache.xml Datei erforderlich, obwohl ich annotation verwendet ????

+6

Der Punkt ist, dass Sie die Annotation @Cacheable verwenden, um Spring anzuweisen, das Ergebnis im Cache mit dem Namen "getActionsBycasId" zwischenzuspeichern. Bis Sie jedoch diesen Cache über die Ehcache-Konfigurationsdatei konfigurieren, haben Sie keinen Cache namens "getActionsBycasId". –

Antwort

4

Versuchen Sie folgendes:

@Bean 
public CacheManager cacheManager() { 
    SimpleCacheManager cacheManager = new SimpleCacheManager(); 
    List<Cache> caches = new ArrayList<Cache>(); 
    caches.add(new ConcurrentMapCache("getActionsBycasId")); 
    cacheManager.setCaches(caches); 
    return cacheManager; 
} 
6

Wenn Sie Federwolke aws verwenden, deaktivieren Sie die automatische ElastiCache Konfiguration.

@EnableAutoConfiguration(exclude = ElastiCacheAutoConfiguration.class) 
@EnableCaching 
@SpringBootApplication 
public class Application { 
    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 
} 
+0

Danke, dass mein Problem gelöst hat. Es ist jedoch seltsam, dass dieses Problem nicht auftritt, wenn meine Anwendung auf einer lokalen Box ausgeführt wird, sondern nur, wenn die Anwendung auf EC2 bereitgestellt wird. – kca2ply

3
@SpringBootApplication(exclude = { 
     ContextStackAutoConfiguration.class, 
     ElastiCacheAutoConfiguration.class 
}) 

Nur nit-picky Einsatz sein @SpringBootApplication statt @EnableAutoConfiguration

0

die oben ausschließen Option, für mich nicht funktioniert hat. Aber, das unten funktioniert. !! Wenn Sie eine Feder-aws-Wolke verwenden, schließen Sie das elastische Element wie unten aus.

<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>