Spring 3.1 ehCache in jar and war其中jar将在war中使用:XML配置

时间:2013-07-05 08:06:36

标签: java jar war ehcache spring-3

在我的项目中,我一直在 XYZ war 中使用 ABC jar ,两者都使用Spring 3.1实现 ehCache,xml驱动配置(我们有ehCache.xml,然后是spring-context.xml,我们在两个项目中通过Spring AOP拦截缓存。我们收到了以下错误:

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 个答案:

答案 0 :(得分:1)

<强>解决方案:

这就是我们解决这个问题的方法:

  1. 我们将所有缓存配置从ABCehCache.xml(从ABC jar)复制到XYZehCache.xml(来自XYZ战争)。
  2. 我们删除了ABCehCache.xml(来自ABC jar),但ehCache.xml内的所有配置(如ABC-spring.xml和Spring AOP的bean实例化)都将保持不变。
  3. XYZ-spring.xml中,我们导入了ABC-spring.xml并定义了复合缓存管理器。
  4. 支持的配置文件:

    <强> 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" />
    

    希望这会有所帮助!!