I have a util module that produces a jar to be used in other applications. I'd like this module to use caching and would prefer to use Spring's annotation-driven
caching.
So Util-Module
would have something like this:
DataManager.java
...
@Cacheable(cacheName="getDataCache")
public DataObject getData(String key) { ... }
...
data-manager-ehcache.xml
...
<cache name="getDataCache" maxElementsInMemory="100" eternal="true" />
...
data-manager-spring-config.xml
...
<cache:annotation-driven cache-manager="data-manager-cacheManager" />
<!-- ???? --->
<bean id="data-manager-cacheManager"
class="org.springframework.cache.ehcache.EhcacheCacheManager"
p:cache-manager="data-manager-ehcache"/>
<bean id="data-manager-ehcache"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:config-location="data-manager-ehcache.xml"/>
...
I would also like my deployable unit to have caching via Spring annotation, while including the above jar as a dependency. So my Deployable-Unit
would have something like this:
MyApp.java
...
@Cacheable(cacheName="getMyAppObjectCache")
public MyAppObject getMyAppObject(String key) { ... }
...
my-app-ehcache.xml
...
<cache name="getMyAppObjectCache" maxElementsInMemory="100" eternal="true" />
...
my-app-spring-config.xml
...
<cache:annotation-driven cache-manager="my-app-cacheManager" />
<!-- ???? --->
<bean id="my-app-cacheManager"
class="org.springframework.cache.ehcache.EhcacheCacheManager"
p:cache-manager="my-app-ehcache"/>
<bean id="my-app-ehcache"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:config-location="my-app-ehcache.xml"/>
...
Question:
Is it possible to use annotation driven caching in both your main project and a dependency module while keeping the configurations separated?
If not, an explanation of why it isn't would be appreciated. If so, an explanation of what needs to change in the above configuration would be appreciated.
I would consider the following much simpler alternatives:
I did not like the solution of CompositeCacheManager, since its behavior is very dependent on the implementation of underlying caches: it will only work as expected if all underlying cache managers return null on unknown cache name. Some implementations would create them on the fly, resulting in caches with configuration that you did not expect.
Spring currently expects the cacheManager to be a Singleton. This is something that the ehcache-spring-annotations project ran into and I've yet to see the request fulfilled. http://code.google.com/p/ehcache-spring-annotations/issues/detail?id=76
As with all things Java and Spring you do have the option to reimplement the class.
http://forums.terracotta.org/forums/posts/list/5618.page#27960 provides a base explanation of what some people have come up with as a workaround and
Is the actual code they came up with. The approach does create a convention to follow but it would be easy enough to reimplement this with your own version if you do not like the actual approach described.
this seems to be fixed in 3.2M1, see https://jira.springsource.org/browse/SPR-8696
In my Project I have been using ABC jar within XYZ war, both implementing ehCache with Spring 3.1,xml-driven configuration(we have ehCache.xml and then spring-context.xml where we are intercepting cache through Spring AOP in both the projects). And we are getting following error:
Solution:
This is how we solved this issue:
ABCehCache.xml
(from ABC jar) toXYZehCache.xml
(from XYZ war).ABCehCache.xml
(from ABC jar) but all the configuration(like bean instantiation forehCache.xml
and Spring AOP) insideABC-spring.xml
will remain same.XYZ-spring.xml
, We importedABC-spring.xml
and defined composite cache manager.Supported Configuration files:
ABC-spring.xml:
XYZ-spring.xml:
Use this class: http://static.springsource.org/autorepo/docs/spring/3.2.0.M1/api/org/springframework/cache/support/CompositeCacheManager.html like this: