我有一个util模块,它生成一个jar以供其他应用程序使用。我希望这个模块使用缓存,并且更喜欢使用Spring的annotation-driven缓存。
所以Util-Module会有这样的东西:
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"/>
...我还希望我的可部署单元通过Spring注释进行缓存,同时将上面的jar作为一个依赖项。所以我的Deployable-Unit会有这样的东西:
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"/>
...问题:
是否可以在主项目和依赖模块中使用注释驱动的缓存,同时保持配置分离?
如果不是的话,解释一下为什么不这样做会很有帮助。如果是这样的话,请解释一下在上面的配置中需要更改什么。
发布于 2012-02-03 09:42:47
这似乎在3.2M1中得到了修正,参见https://jira.springsource.org/browse/SPR-8696
发布于 2012-02-08 10:00:52
像这样使用这个类:http://static.springsource.org/autorepo/docs/spring/3.2.0.M1/api/org/springframework/cache/support/CompositeCacheManager.html:
<cache:annotation-driven cache-manager="cacheManager" />
<bean id="cacheManager" class="org.springframework.cache.support.CompositeCacheManager">
<property name="cacheManagers">
<array>
<ref bean="cacheManager1" />
<ref bean="cacheManager2" />
</array>
</property>
<property name="addNoOpCache" value="true" />
</bean>发布于 2012-01-20 02:36:39
Spring目前预计cacheManager将是一个单例。这是ehcache-spring注释项目遇到的事情,我还没有看到请求得到满足。http://code.google.com/p/ehcache-spring-annotations/issues/detail?id=76
与所有事情一样,Java和Spring也有重新实现类的选项。
http://forums.terracotta.org/forums/posts/list/5618.page#27960提供了一个基本的解释,说明一些人想出了什么作为解决办法,
他们想出的实际代码。这种方法确实创建了一个可以遵循的约定,但是如果您不喜欢所描述的实际方法,那么用您自己的版本重新实现这个方法就很容易了。
https://stackoverflow.com/questions/8658789
复制相似问题