问题描述:
多个maven module 下都使用了ehcache.xml 文件 ,导致会出现配置文件中的缓存块找不到异常。
问题前的配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">
<!-- <ehcache:annotation-driven /> -->
<!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
<cache:annotation-driven cache-manager='ehcacheManager'/>
<!-- cacheManager工厂类,指定ehcache.xml的位置 -->
<bean id='ehcacheManagerFactory' class='org.springframework.cache.ehcache.EhCacheManagerFactoryBean'>
<property name='configLocation' value='classpath:config/ehcache.xml' />
<property name="shared" value="true"/>
</bean>
<!-- 声明cacheManager -->
<bean id='ehcacheManager' class='org.springframework.cache.ehcache.EhCacheCacheManager'>
<property name='cacheManager' ref='ehcacheManagerFactory' />
</bean>
</beans>
解决方式:
将以上配置改为:(为每个CacheManagerName 取个名字)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">
<!-- <ehcache:annotation-driven /> -->
<!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
<cache:annotation-driven cache-manager='ehcacheManager'/>
<!-- cacheManager工厂类,指定ehcache.xml的位置 -->
<bean id='ehcacheManagerFactory' class='org.springframework.cache.ehcache.EhCacheManagerFactoryBean'>
<property name='configLocation' value='classpath:config/ehcache.xml' />
<!--多个配置文件导致后面的配置文件无法被加载,解决方法:去掉缓存共享配置,添加自身的缓存管理器名称 -->
<property name="cacheManagerName" value="exbManager"/>
</bean>
<!-- 声明cacheManager -->
<bean id='ehcacheManager' class='org.springframework.cache.ehcache.EhCacheCacheManager'>
<property name='cacheManager' ref='ehcacheManagerFactory' />
</bean>
</beans>
问题被完美解决!