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

6

我正在使用 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 中出现以下错误:
在 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

我没有在我的项目中添加任何net.sf.ehcache的东西。正如我之前提到的,ehcache已经移动到org.ehcache

为什么会出现这个错误?这与我添加的依赖项中不包括net.sf.ehcache有关。

由于该组件已经被移动,我应该用不同的方式编写第二个bean吗?还是有其他方法可以解决这个问题?

2个回答

21

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

就是这样。


1
非常感谢。尝试了几种方法后,我想出的解决方案是使用旧的Ehcache 2。这解决了问题。然而,这也是一个解决方案。我接受了它。 :) - vigamage
谢谢;-)就像我说的那样,当然也可以把依赖项移到ehcache 2。 - Henri
谢谢!我找到了一个使用ehcache 2的示例代码,它可以正常工作:https://github.com/TechPrimers/spring-ehcache-example/blob/master/src/main/resources/ehcache.xml - Aliya

0
请在pom文件中添加以下依赖项:
<dependency>
     <groupId>net.sf.ehcache</groupId>
     <artifactId>ehcache</artifactId> 
     <version>2.10.4</version>
</dependency>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接