Spring cache does not work for abstract classes

I am trying to use Spring Cache in abstract classes, but this will not work, because from what I see, Spring searches for CacheNames in the abstract class. I have a REST API that uses a service layer and a dao layer. The idea is to have a different cache name for each subclass.

My abstract service class is as follows:

    @Service
    @Transactional
    public abstract class AbstractService<E> {

...

    @Cacheable
    public List<E> findAll() {
        return getDao().findAll();
    }
}

An extension of an abstract class will look like this:

@Service
@CacheConfig(cacheNames = "textdocuments")
public class TextdocumentsService extends AbstractService<Textdocuments> {
...
}

So when I run the application with this code, Spring gives me the following exception:

Caused by: java.lang.IllegalStateException: No cache names could be detected on 'public java.util.List foo.bar.AbstractService.findAll()'. Make sure to set the value parameter on the annotation or declare a @CacheConfig at the class-level with the default cache name(s) to use.
    at org.springframework.cache.annotation.SpringCacheAnnotationParser.validateCacheOperation(SpringCacheAnnotationParser.java:240) ~[spring-context-4.1.6.RELEASE.jar:?]

I think this is because Spring is looking for a CacheName in an abstract class, despite the fact that it is declared in a subclass.

Trying to use

 @Service
 @Transactional
 @CacheConfig
        public abstract class AbstractService<E> {
    }

leads to the same exception; using

 @Service
 @Transactional
 @CacheConfig(cacheNames = "abstractservice")
        public abstract class AbstractService<E> {
    }

, Spring , . , ?

+4

All Articles