maven - 无法访问net.sf.ehcache.CacheManager,找不到net.sf.ehcache.CacheManager的类文件

标签 maven spring-boot ehcache spring-cache terracotta

我一直在使用EhCache在我的项目中实现一些缓存。我已经将以下依赖项添加到我的pom.xml中

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.8.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>4.3.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.ehcache/ehcache -->
<dependency>
    <groupId>org.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>3.3.1</version>
</dependency>

注意第三个依赖项是EhCache。在网上找到的所有教程中,组ID是不同的。我更改组ID的原因是它已移至org.ehcache

我的类路径中有以下ehcache.xml文件。
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd"
         updateCheck="true"
         monitoring="autodetect"
         dynamicConfig="true">

    <diskStore path="java.io.tmpdir"/>

    <cache name="cardTypes"
           maxEntriesLocalHeap="100"
           maxEntriesLocalDisk="1000"
           eternal="false"
           timeToIdleSeconds="300"
           timeToLiveSeconds="600"
           memoryStoreEvictionPolicy="LFU"
           transactionalMode="off">
        <persistence strategy="localTempSwap"/>
    </cache>

</ehcache>

以下是我的配置文件。
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.cache.CacheManager;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.core.io.ClassPathResource;

@EnableCaching
@Configuration
public class CacheConfig {

    @Bean
    public CacheManager cacheManager() {
        return new EhCacheCacheManager(ehCacheCacheManager().getObject());
    }

    @Bean
    public EhCacheManagerFactoryBean ehCacheCacheManager() {
        EhCacheManagerFactoryBean factory = new EhCacheManagerFactoryBean();
        factory.setConfigLocation(new ClassPathResource("ehcache.xml"));
        factory.setShared(true);
        return factory;
    }

}

现在,我在下一行出现错误。
return new EhCacheCacheManager(ehCacheCacheManager().getObject());

那是:

在Eclipse中:-

The type net.sf.ehcache.CacheManager cannot be resolved. It is indirectly referenced from required .class files



在JIdea中:

Error:(21, 71) java: cannot access net.sf.ehcache.CacheManager
  class file for net.sf.ehcache.CacheManager not found


我没有在项目中添加任何net.sf.ehcache内容。如前所述,ehcache已移至org.ehcache

为什么我会收到此错误?它与net.sf.ehcache有关,它不是我添加的依赖项的一部分。

自 Artifact 被移动以来,我是否应该以不同的方式编码第二个bean?或如何解决?

最佳答案

Ehcache 2在net.sf.ehcache包中。大多数教程都是关于它的,因为它具有很长的使用生命周期。您配置的版本Ehcache 3是全新的(但当然更好),并且在org.ehcache包中。

该软件包之所以搬迁是因为拥有自己的域名更好,而且还因为新版本有很大的不同,并且有必要能够与旧版本同居(因为某些框架正在使用它)。

您的错误来自EhCacheCacheManager使用Ehcache2。Ehcache3不需要它,因为它符合JCache。因此,您可以改用JCacheCacheManager

因此,现在,您具有Spring接线和Ehcache 2的ehcache.xml,并且具有对Ehcache 3的依赖关系。您应该对齐它们以解决问题。

要使用Ehcache 3,最简单的方法是将其添加到application.properties

spring.cache.jcache.config=ehcache.xml

和这个:
@EnableCaching
@Configuration
public class CacheConfig {
}

就是这样。

关于maven - 无法访问net.sf.ehcache.CacheManager,找不到net.sf.ehcache.CacheManager的类文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44341553/

相关文章:

java - 将 spring boot 与 redis 混合

java - Maven:插件配置异常

java - Spring Boot - Hibernate,异常时回滚成功保存

java - 创建后在 apache tomcat 上部署 war 文件

java - Spring Boot 数据库只初始化一次

java - 具有大量并发事务的 Hibernate 二级缓存 ObjectNotFoundException

java - Ehcache日志级别

java - Ehcache 正在为磁盘持久性创建不必要的时间戳目录

java - 无法在eclipse中打开新创建的Maven项目

java - 将 javaFX (.jar) 文件导入 Netbeans Maven 项目