在您将此标记为重复之前,请先阅读问题。我已经阅读了所有关于此异常的内容,但它并没有解决我的问题。而且我确实遇到了稍微不同的异常,例如 Another CacheManager with same name 'myCacheManager' already exists
而不是 Another unnamed CacheManager already exists
。
Spring配置:
<cache:annotation-driven cache-manager="cacheManager"/>
<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cacheManager-ref="ehcache"/>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="ehcache.xml"
p:cacheManagerName="myCacheManager"
p:shared="true"/>
ehcache
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false" name="myCacheManager">
</ehcache>
问题在于我有1个(未来可能更多)测试类用于测试安全性,这些类还会加载SecurityContext.xml文件。
因此,大多数测试类都具有以下注释:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationContext.xml")
然而,导致问题的类是:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:ApplicationContext.xml",
"classpath:SecurityContext.xml"
})
看起来由于位置不同,上下文被重新加载,但是ehcacheManager仍然处于之前测试的活动状态。
注意:只有在运行多个测试(例如像清理+构建)时才会出现这种情况。单独运行此测试类可以完美地解决问题。
问题是什么?我该如何解决?