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

vig*_*age 4 terracotta ehcache maven spring-boot spring-cache

我一直在使用我的项目中实现一些缓存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>
Run Code Online (Sandbox Code Playgroud)

请注意第三个依赖项EhCache.在我在网上找到的所有教程中,组ID都不同.我改变组ID的原因是它被移动到了org.ehcache.

我的classpath中有以下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>
Run Code Online (Sandbox Code Playgroud)

以下是我的配置文件.

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

现在,我在下面的行中收到错误.

return new EhCacheCacheManager(ehCacheCacheManager().getObject());
Run Code Online (Sandbox Code Playgroud)

那是:

在Eclipse中: -

net.sf.ehcache.CacheManager类型无法解析.它是从所需的.class文件间接引用的

在JIdea:

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

我没有在我的项目中添加任何net.sf.ehcache内容.正如我之前提到的,ehcache已经转移到了org.ehcache.

为什么我收到此错误?它与net.sf.ehcache我所添加的依赖项的一部分无关.

我应该以不同的方式对第二个bean进行编码,因为工件已被移动了吗?或者我怎样才能解决这个问题?

Hen*_*nri 11

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

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

您的错误来自EhCacheCacheManager使用Ehcache 2 的事实.Ehcache 3不需要它,因为它符合JCache.所以你可以JCacheCacheManager改用.

所以,现在,你有Spring接线和ehcache.xmlEhcache 2.依赖于Ehcache 3.你应该调整它们来解决你的问题.

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

spring.cache.jcache.config=ehcache.xml
Run Code Online (Sandbox Code Playgroud)

还有这个:

@EnableCaching
@Configuration
public class CacheConfig {
}
Run Code Online (Sandbox Code Playgroud)

就是这样.