31

他のアプリケーションで使用する jar を生成する util モジュールがあります。このモジュールでキャッシングを使用したいのですが、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"/>
...

上記のjarを依存関係として含めながら、展開可能なユニットにSpringアノテーションを介してキャッシュを持たせたいと思います。したがって、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"/>
...

質問:

構成を分離したまま、メイン プロジェクトと依存関係モジュールの両方でアノテーション ドリブン キャッシュを使用することは可能ですか?

そうでない場合は、そうでない理由の説明をいただければ幸いです。もしそうなら、上記の構成で何を変更する必要があるかについての説明をいただければ幸いです。

4

5 に答える 5

13

このクラスを使用します: 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-02-08T10:00:52.973 に答える
9

これは3.2M1で修正されているようです。https: //jira.springsource.org/browse/SPR-8696を参照してください

于 2012-02-03T09:42:47.743 に答える
5

Spring は現在、cacheManager がシングルトンであることを期待しています。これは ehcache-spring-annotations プロジェクトが遭遇したものであり、私はまだ要求が満たされているのを見たことがない. http://code.google.com/p/ehcache-spring-annotations/issues/detail?id=76

Java と Spring のすべてと同様に、クラスを再実装するオプションがあります。

http://forums.terracotta.org/forums/posts/list/5618.page#27960では、一部の人々が回避策として考え出したものの基本的な説明を提供し、

彼らが思いついた実際のコードです。このアプローチは従うべき規則を作成しますが、説明されている実際のアプローチが気に入らない場合は、独自のバージョンでこれを再実装するのは簡単です。

于 2012-01-20T02:36:39.847 に答える
2

私のプロジェクトでは、 XYZ war内でABC jarを使用しており、両方ともSpring 3.1 で ehCache を実装しています。 . そして、次のエラーが発生しています。

java.lang.IllegalArgumentException: Cannot find cache named [xxxxxx] for CacheableOperation[] caches=[Cxxxxxxxx] | condition='' | key='#xxxxxxxxxxxxx' 
at org.springframework.cache.interceptor.CacheAspectSupport.getCaches(CacheAspectSupport.java:163) [spring-context-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContext.<init>(CacheAspectSupport.java:443) [spring-context-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.cache.interceptor.CacheAspectSupport.getOperationContext(CacheAspectSupport.java:173) [spring-context-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.cache.interceptor.CacheAspectSupport.createOperationContext(CacheAspectSupport.java:404) [spring-context-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:192) [spring-context-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:66) [spring-context-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) [spring-aop-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90) [spring-aop-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) [spring-aop-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622) [spring-aop-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    at com.infy.flypp.dao.ContentDAO$$EnhancerByCGLIB$$9443481.getContentById(<generated>) [cglib-2.2.2.jar:] 

解決:

この問題を解決した方法は次のとおりです。

  1. すべてのキャッシュ構成をABCehCache.xml(ABC jar から) からXYZehCache.xml(XYZ war から) にコピーしました。
  2. (ABC jar から)を削除しましたが、内部のすべての構成 (および Spring AOPABCehCache.xmlの Bean インスタンス化など)は同じままです。ehCache.xmlABC-spring.xml
  3. では、複合キャッシュ マネージャXYZ-spring.xmlをインポートABC-spring.xmlして定義しました。

サポートされている構成ファイル:

ABC-spring.xml:

    <aop:aspectj-autoproxy proxy-target-class="true" />

    <bean id="CacheManager1" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="ehcache"></property>
    </bean>

    <bean id="ehcache"
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
        p:config-location="classpath:ABCEhcache.xml" />

XYZ-spring.xml:

<import resource="classpath*:ABC-spring.xml" />
<aop:aspectj-autoproxy proxy-target-class="true" />

    <bean id="cacheManager" class="org.springframework.cache.support.CompositeCacheManager">
    <property name="cacheManagers">
        <array>
            <ref bean="CacheManager1" />
            <ref bean="CacheManager2" />
        </array>
    </property>
    <property name="fallbackToNoOpCache" value="true" />
</bean>

    <bean id="CacheManager2" class="org.springframework.cache.ehcache.EhCacheCacheManager"
        p:cache-manager-ref="ehcache" />
    <bean id="ehcache"
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
        p:config-location="classpath:XYZEhcache.xml" />
于 2013-07-05T07:55:53.280 に答える