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

cannot access net.sf.ehcache.CacheManager, class file for net.sf.ehcache.CacheManager not found

我一直在使用 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。我在网上找到的所有教程中,group 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 有关,这不是我添加的依赖项的一部分。

既然工件已经移动,我是否应该以不同的方式对第二个 bean 进行编码?或者我该如何解决这个问题?

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

移动包是因为拥有自己的域更好,但也因为新版本非常不同并且能够与旧版本共存是必要的(因为某些框架使用它)。

您的错误来自于 EhCacheCacheManager 正在使用 Ehcache 2。Ehcache 3 不需要它,因为它与 JCache 兼容。所以你可以使用 JCacheCacheManager 代替。

所以,现在,您有 Spring 线路和 Ehcache 2 的 ehcache.xml。以及对 Ehcache 3 的依赖。您应该调整它们以解决您的问题。

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

spring.cache.jcache.config=ehcache.xml

还有这个:

@EnableCaching
@Configuration
public class CacheConfig {
}

就是这样。

在pom文件中添加如下依赖:

<dependency>
     <groupId>net.sf.ehcache</groupId>
     <artifactId>ehcache</artifactId> 
     <version>2.10.4</version>
</dependency>