【问题标题】:cannot access net.sf.ehcache.CacheManager, class file for net.sf.ehcache.CacheManager not found无法访问 net.sf.ehcache.CacheManager,找不到 net.sf.ehcache.CacheManager 的类文件
【发布时间】:2017-06-03 07:32:34
【问题描述】:

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

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 进行编码?或者我该如何解决这个问题?

【问题讨论】:

    标签: maven spring-boot ehcache spring-cache terracotta


    【解决方案1】:

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

    之所以移动包是因为拥有自己的域更好,而且还因为新版本完全不同并且能够与旧版本共存是必要的(因为某些框架使用它)。

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

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

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

    spring.cache.jcache.config=ehcache.xml
    

    还有这个:

    @EnableCaching
    @Configuration
    public class CacheConfig {
    }
    

    就是这样。

    【讨论】:

    • 非常感谢。在尝试了几件事之后,我想出的解决方案是使用旧的 Ehcache 2。这解决了这个问题。不过,这也是一种解决办法。我正在接受这一点。 :)
    • 谢谢 ;-) 正如我所说,将依赖项移动到 ehcache 2 当然也可以 :-)
    • 谢谢!我找到了一个使用 ehcache 2 的示例代码:github.com/TechPrimers/spring-ehcache-example/blob/master/src/…
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-14
      • 2018-02-04
      • 2019-09-17
      • 2018-07-20
      • 2018-10-08
      • 1970-01-01
      相关资源
      最近更新 更多