无法访问net.sf.ehcache.cachemanager,找不到net.sf.ehcache.cachemanag



我一直在使用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

我在我的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>

以下是我的配置文件。

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: -

类型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有关,这不是我添加的依赖关系的一部分。

由于工件已移动以来,我应该以不同的方式编码第二种豆吗?还是我该如何解决?

ehcache 2在net.sf.ehcache软件包中。大多数教程都是关于它的,因为它有漫长而有用的生活。Ehcache 3,您配置的版本是非常新的(但当然更好(,在org.ehcache软件包中。

软件包移动了,因为拥有自己的域更好,但由于新版本完全不同,并且能够与旧版本同居(因为使用了某些框架(。

您的错误来自EhCacheCacheManager使用EHCACHE 2的事实。EHCACHE3不需要它,因为它符合JCache。因此,您可以改用JCacheCacheManager

因此,现在,您有弹簧布线和用于EHCACHE的ehcache.xml

要使用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>

最新更新