无法访问net.sf.ehcache.CacheManager,找不到net.sf.ehcache.CacheManager的类文件 [英] cannot access net.sf.ehcache.CacheManager, class file for net.sf.ehcache.CacheManager not found

查看:542
本文介绍了无法访问net.sf.ehcache.CacheManager,找不到net.sf.ehcache.CacheManager的类文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 EhCache 在我的项目中实现一些缓存.我在pom.xml中添加了以下依赖项

I have been implementing some caching in my project using EhCache. I have added following dependencies to my 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 .

Note the third dependency which is for EhCache. In all the tutorials I found online, the group id is different. The reason for me to change the group id was it had been moved to org.ehcache.

我的类路径中有以下ehcache.xml文件.

I have the following ehcache.xml file in my classpath.

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

}

现在,我在下一行出现错误.

Now, I am getting an error in following line.

return new EhCacheCacheManager(ehCacheCacheManager().getObject());

也就是说:

在Eclipse中:-

In Eclipse:-

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

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 .

I have not added any net.sf.ehcache stuff into my project. As I mentioned before, the ehcache has been moved to org.ehcache.

为什么我会收到此错误?它与 net.sf.ehcache 有关,这不是我添加的依赖项的一部分.

Why I am getting this error? It is related to net.sf.ehcache which is not a part of the dependencies I have added.

自工件被移动以来,我是否应该以不同的方式编码第二个bean?或如何解决这个问题?

Should I code the second bean in a different way since the artifact has been moved? or how can I get this resolved?

推荐答案

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

Ehcache 2 is in the net.sf.ehcache package. Most tutorial are about it since it had a long and useful life. Ehcache 3, the version you configured, is quite new (but of course better) and in the org.ehcache package.

之所以选择该软件包,是因为拥有自己的域名更好,而且还因为新版本有很大的不同,并且有必要能够与旧版本同居(因为某些框架正在使用它).

The package moved because having its own domain was nicer but also because the new version is quite different and be able to cohabit with the old version can be necessary (because of some framework using it).

您的错误来自于 EhCacheCacheManager 使用Ehcache 2的事实.Ehcache3不需要它,因为它兼容JCache.因此,您可以改用 JCacheCacheManager .

Your error comes from the fact that EhCacheCacheManager is using Ehcache 2. Ehcache 3 doesn't need it because it is JCache compliant. So you can use JCacheCacheManager instead.

因此,现在,您具有Spring接线和用于Ehcache 2的 ehcache.xml ,并具有对Ehcache 3的依赖关系.您应该对齐它们以解决问题.

So, right now, you have the Spring wiring and an ehcache.xml for Ehcache 2. And a dependency to Ehcache 3. You should align them to fix your problem.

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

To use Ehcache 3, the easiest is to add this in your application.properties

spring.cache.jcache.config=ehcache.xml

这:

@EnableCaching
@Configuration
public class CacheConfig {
}

就是这样.

这篇关于无法访问net.sf.ehcache.CacheManager,找不到net.sf.ehcache.CacheManager的类文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆