【发布时间】: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