简体   繁体   中英

Issue with Spring Cache Abstraction & Ehcache

I am facing an issue wherein the code tries to find cache named "AdministrationContactManagerImpl.retrieve" but it fails to find it.The target method is annotated with @cacheable.

@Cacheable(value="administrationContactManagerImpl.retrieve", key="#prefix + #contract + #effectiveDate + #administrationRoles")
    public List<AdministrationContact> retrieve(Set<AdministrationRole> administrationRoles, Date effectiveDate,

I have not defined <ehcache:annotation-driven> in my main applicationContext file.This means that caching should not get enabled and I should not run in to the errors as output in the stacktrace. I suspect some jar's in the classpath might be enabling the caching but how it is enabled and how can I detect it?

Can anyone tell how could I disable the caching so that I can avoid this error.

We are using spring version 3.1.1.Release jars.

 java.lang.IllegalArgumentException: Cannot find cache named [administrationContactManagerImpl.retrieve] for CacheableOperation[public java.util.List com.principal.ris.contact.internal.impl.AdministrationContactManagerImpl.retrieve(java.util.Set,java.util.Date,java.lang.Integer,java.lang.String)] caches=[administrationContactManagerImpl.retrieve] | condition='' | key='#prefix + #contract + #effectiveDate + #administrationRoles'
[4/17/13 14:44:50:001 IST] 0000001a SystemErr     R     at org.springframework.cache.interceptor.CacheAspectSupport.getCaches(CacheAspectSupport.java:163)
[4/17/13 14:44:50:001 IST] 0000001a SystemErr     R     at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContext.<init>(CacheAspectSupport.java:443)
[4/17/13 14:44:50:001 IST] 0000001a SystemErr     R     at org.springframework.cache.interceptor.CacheAspectSupport.getOperationContext(CacheAspectSupport.java:173)
[4/17/13 14:44:50:001 IST] 0000001a SystemErr     R     at org.springframework.cache.interceptor.CacheAspectSupport.createOperationContext(CacheAspectSupport.java:404)
[4/17/13 14:44:50:001 IST] 0000001a SystemErr     R     at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:192)
[4/17/13 14:44:50:001 IST] 0000001a SystemErr     R     at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:66)
[4/17/13 14:44:50:001 IST] 0000001a SystemErr     R     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
[4/17/13 14:44:50:001 IST] 0000001a SystemErr     R     at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)
[4/17/13 14:44:50:001 IST] 0000001a SystemErr     R     at com.principal.ris.contact.internal.impl.AdministrationContactManagerImpl$$EnhancerByCGLIB$$ddaad355.retrieve(<generated>)
[4/17/13 14:44:50:001 IST] 0000001a SystemErr     R     at com.principal.ris.tpaview.listener.TpaViewUserTemplateListener.contextCreated(TpaViewUserTemplateListener.java:118)

Updating this post : I have selected the answer as best answer but I will tell you all what I did. In you java maven projects, you really do not need to include such annotations which can impact your context. As a best practice, always keep the switches configurations in your war's pom file.

This section of the reference docs gives details on how the support for caching annotations is enabled in Spring. Based on that, there must be a <cache:annotation-driven/> tag in one of your xml context files. Double-check that there are no (potentially transitively) imported configuration files that contain this instruction.

Reply to your comment:
Having <cache:annotation-driven/> in an imported xml file will definitely enable caching support. All the imported (and transitively imported) context files are merged into one single Spring context, and the annotation-driven tag instructs Spring to look for annotations on beans in the whole context , not only on beans defined in the same xml file where this instruction is placed. The important thing to understand is that bean definitions might phisically be divided into several files (that can also come from different jars), but that won't result in any kind of logical modularisation of the context.

Such modularisation can only be achieved by defining parent-child relationships between contexts, where the parent context is loaded first, then the child context second (designating the first one as its parent). This results in a setup where the parent context is basically unaware of its child, but the child can access any beans defined in its parent.
You could use this to first load the context defined in the external jar, then load your own context as a child. This way the annotation-driven in the parent context wouldn't affect your beans, while you could still access anything from the partent context.
Problem is that there is no easy way to declare a parent context if your application is a Spring MVC webapp.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM