gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:50:59 26 4
gpt4 key购买 nike

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

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

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

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

最佳答案

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

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

您的错误来自EhCacheCacheManager使用Ehcache2。Ehcache3不需要它,因为它符合JCache。因此,您可以改用JCacheCacheManager

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

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

spring.cache.jcache.config=ehcache.xml

和这个:
@EnableCaching
@Configuration
public class CacheConfig {
}

就是这样。

关于maven - 无法访问net.sf.ehcache.CacheManager,找不到net.sf.ehcache.CacheManager的类文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44341553/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com