这就是我运行 junit 测试时发生的情况......
Another CacheManager with same name 'cacheManager' already exists in the same VM. Please
provide unique names for each CacheManager in the config or do one of following:
1. Use one of the CacheManager.create() static factory methods to reuse same
CacheManager with same name or create one if necessary
2. Shutdown the earlier cacheManager before creating new one with same name.
The source of the existing CacheManager is:
DefaultConfigurationSource [ ehcache.xml or ehcache-failsafe.xml ]
异常背后的原因是什么。是否可以有超过 1 个缓存管理器同时运行?
这就是我使用 Sping 3.1.1 配置 cachManager 的方法。它将cacheManager的范围明确设置为“singleton”
<ehcache:annotation-driven />
<bean
id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
scope="singleton"
/>
ehcache.xml 看起来像
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false"
maxBytesLocalHeap="100M"
name="cacheManager"
>
....
</ehcache>
终于我的课了
@Component
public class BookingCache implements CacheWrapper<String, BookingUIBean> {
@Autowired
private CacheManager ehCacheManager;
....
}
我非常确定我的代码库中只处理一个缓存管理器。第 n 个实例可能正在运行。
您的 EhCacheManagerFactoryBean 可能是单例,但它正在构建多个 CacheManager 并尝试为它们提供相同的名称。这违反了 Ehcache 2.5语义 http://ehcache.org/documentation/get-started/key-classes-methods#introduction.
Ehcache 2.5 之前的版本允许 JVM 中存在任意数量的同名(相同配置资源)的 CacheManager。
Ehcache 2.5及更高版本不允许同一个JVM中存在多个同名的CacheManager。创建非 Singleton CacheManager 的 CacheManager() 构造函数可能会违反此规则
通过设置来告诉工厂 bean 在 JVM 中创建 CacheManager 的共享实例shared http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/cache/ehcache/EhCacheManagerFactoryBean.html#setShared%28boolean%29属性为真。
<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:shared="true"/>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)