单元测试-net.sf.ehcache.CacheException

报错问题

在使用IDEA进行springmvc+spring+hibernate框架单元测试的时候,只能单个测试方法正确运行,一旦多个测试方法便报错。

报错报文

Caused by: net.sf.ehcache.CacheException: Another CacheManager with same name 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: URLConfigurationSource net.sf.ehcache.CacheManager.assertNoCacheManagerExistsWithSameName(CacheManager.java:626)
    at net.sf.ehcache.CacheManager.init(CacheManager.java:391)
    at net.sf.ehcache.CacheManager.(CacheManager.java:269)
    at org.hibernate.cache.ehcache.EhCacheRegionFactory.start(EhCacheRegionFactory.java:74)
    … 81 more

解决办法

找到数据库的配置文件,添加或修改配置

hibernate.cache.region.factory_class = org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory

hibernate.cache.provider_class = net.sf.ehcache.hibernate.SingletonEhCacheProvider

将EhCacheRegionFactory和EhCacheProvider设置为单例模式,也就是保证每个方法共享同一个缓存管理器。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Spring框架中,当我们使用注解或配置文件进行依赖注入时,有时会遇到"No qualifying bean of type"的错误。这个错误通常表示Spring容器无法找到符合要求的bean。 针对你提到的"No qualifying bean of type net.sf.ehcache.Ehcache"错误,可能有以下几种原因和解决方法: 1. 缺少依赖:首先,请确保你的项目中已经添加了Ehcache的相关依赖。可以通过Maven或Gradle等构建工具来添加依赖。例如,在Maven中可以添加以下依赖: ```xml <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.10.6</version> </dependency> ``` 2. 配置错误:检查你的配置文件,确保已经正确配置了Ehcache相关的bean。可以使用`<bean>`标签或者注解来配置Ehcache相关的bean。例如,在XML配置文件中可以添加以下配置: ```xml <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml"/> </bean> ``` 3. 包扫描问题:如果你使用了包扫描来自动装配bean,确保Ehcache相关的类所在的包已经包含在扫描范围内。可以在配置类上使用`@ComponentScan`注解指定要扫描的包。 以上是可能导致"No qualifying bean of type net.sf.ehcache.Ehcache"错误的一些常见原因和解决方法。如果以上方法都没有解决问题,可以提供更多的代码和配置信息,以便更好地帮助你解决问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值