【问题标题】:Ehcache 3 spring boot not working in integration testEhcache 3 spring boot在集成测试中不起作用
【发布时间】:2022-08-21 05:18:08
【问题描述】:

我需要你的帮助。我将 Ehcache 3 与 springboot 2 集成在一起。一切都在生产中完美运行。如果我手动执行集成测试(我正在使用 Intellij IDE),集成测试也可以工作。 但是当我运行“mvn install”时,我得到了这个错误:

java.lang.IllegalStateException:无法加载 ApplicationContext 引起:org.springframework.beans.factory.BeanCreationException: 创建类路径中定义的名称为 \'ehCacheManager\' 的 bean 时出错 资源 [com/afklm/belem/payment/config/EhCacheConfiguration.class]: 通过工厂方法实例化 Bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:失败 实例化[org.springframework.cache.CacheManager]:工厂方法 \'ehCacheManager\' 抛出异常;嵌套异常是 javax.cache.CacheException:已经有一个名为 [currenciesCache] 的缓存 存在

CurrencyRepository 接口:

@Repository
public interface CurrencyRepository extends JpaRepository<Currency, String> {
    /**
     * Gets currencies from Cache.
     * The real call to the DataBase will be performed if the response is not in cache
     *
     * @return list of {@link Currency}
     */
    @Cacheable(cacheNames = \"currenciesCache\", key =\"#root.methodName\", unless = \"#result==null or #result.size()==0\")
    default List<Currency> getCachedCurrencies() {
        return findAll();
    }

缓存配置类

import org.ehcache.config.CacheConfiguration;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.ExpiryPolicyBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.jsr107.Eh107Configuration;

import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.jcache.JCacheCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.springframework.cache.CacheManager;

import javax.cache.Caching;
import java.time.Duration;
import java.util.List;


@EnableCaching
@Configuration
public class EhCacheConfiguration extends CachingConfigurerSupport {

    @Bean
    public CacheManager ehCacheManager() {

        //-> currencies cache configuration
        CacheConfiguration<String, List> currenciesCacheConfig =
                CacheConfigurationBuilder
                        .newCacheConfigurationBuilder(String.class, List.class, ResourcePoolsBuilder.heap(1000))
                        .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofDays(30)))
                        .withDefaultEventListenersThreadPool()
                        .build();

        javax.cache.CacheManager cacheManager = Caching.getCachingProvider().getCacheManager();

        cacheManager.createCache(\"currenciesCache\", Eh107Configuration.fromEhcacheCacheConfiguration(currenciesCacheConfig));

  

        return  new JCacheCacheManager(cacheManager);
    }

谢谢你的帮助;我被困住了

  • 求救啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊

标签: java spring spring-boot ehcache ehcache-3


【解决方案1】:

我想我之前遇到过这个问题,当时我的至少一个测试有 @DirtiesContext 注释,它重新初始化了 Spring 上下文。在尝试创建缓存之前,我通过调用cacheManager.destroyCache 方法解决了这个问题。您可以尝试在cacheManager.createCache 语句之前添加语句cacheManager.destroyCache("currenciesCache"); 吗?或者,您可以尝试在不显式调用 createCache 方法的情况下初始化缓存管理器,就像在 this Stack Overflow answer 中所做的那样

【讨论】:

    猜你喜欢
    • 2017-04-15
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 2020-08-05
    • 2017-01-05
    • 2019-02-04
    • 2018-01-01
    • 2021-04-05
    相关资源
    最近更新 更多