We have a project that uses spring-boot-cache-starter, registering an ehCache
CacheManager
implementation, and spreading @Cacheables
accross the code.
Then, some other team created an starter, that basically relies on the default configuration autconfigured by spring-boot-cache
starter (hashmap) for its own processing of @Cacheable
methods.
Both codes contain the @EnableCaching
annotation, and our issue is that the behavior is different in case we comment our main project's @EnableCaching annotation.
- If we don't comment
@EnableCaching
in our project, when we use the custom starter, everything works fine.@Cacheables
from the starter are indexed and resolved in the starter scope but@Cacheables
from our domain are resolved in ourehcache
. - If we comment
@EnableCaching
in our project, then both the starter and our project's@Cacheables
are tried to be resolved against ourehCache
implementation.
This breaks a lot of preconceptions I had so far:
I always thought an annotation such as
@Enable
... applied to all the context, regardless of the placement (starter/application configuration), and regardless of whether it was found once or twice when scanning all@Configuration
classes.Why does the case work when both annotations are there, I guess the
CacheManager
in the spring-boot-cache-starter is a@ConditionalOnBean
, so in that case I would expect both projects using theehcache
bean for resolving, not each one's domain
P.S: the @EnableCaching
found in our main project is placed on an inner static @Configuration class. Could this be significant?
It is very hard to answer your question if you don't reveal what the custom starter does. In particular, this looks weird to me:
Your preconceptions 1 is valid: it doesn't matter where you you put the annotation or if you add it more than once. It will just enable caching for the whole
ApplicationContext
. In the case of Spring Boot, that will trigger the auto-configuration unless a customCacheManager
bean is defined in user's configuration.The "each one's domain" sounds broken to me. Are you sure this is what's happening? If you want to store in several cache managers there is not a lot of different ways:
CacheResolver
and refer to it in the@Cacheable
annotations (or@CacheConfig
)CacheManager
that knows where to find the caches in each underlying storesIf each domain has a standard use of
@Cacheable
it will go against theCacheManager
. If you notice the behaviour you are describing, it has nothing to do with@EnableCaching
at all.