java - jar 和 war 中的 Spring 3.1 ehCache 将在 war : XML configuration 中使用 jar

标签 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. 我们将所有缓存配置从 ABCehCache.xml(来自 ABC jar)复制到 XYZehCache.xml(来自 XYZ war)。
  2. 我们删除了 ABCehCache.xml(来自 ABC jar)但所有配置(如 ehCache.xml 和 Spring AOP 的 bean 实例化)在 ABC- 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" />

希望这会有所帮助!

关于java - jar 和 war 中的 Spring 3.1 ehCache 将在 war : XML configuration 中使用 jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17484027/

相关文章:

java - 使用 Spring JDBC 进行多线程

java - 使用 ant 使用库创建可执行 jar 后出现 NoClassDefFoundError

java - Ant 可以在构建 jar 文件时确定依赖关系吗

java - 如何用单个文件部署多个java项目?

java - 为什么 jhat 的 -baseline 选项不起作用?

java - Telegram 群组成员列表(来自 ID)

java - 从 Java 程序内部顺序执行 jar 文件

java - Eclipse:.war 生成的依赖项

java - 如何使用它的相对路径访问tomcat服务器中的文件夹?

java - Derby 错误 "Subquery is only allowed to return a single column."