EHCache 配置在升级后不起作用
spring boot 3.0.2
下面是gradle依赖
plugins {
id 'java'
id 'org.springframework.boot' version '3.0.2'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}
group = 'com.image'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-cache'
implementation 'net.sf.ehcache:ehcache:2.9.1'
}
EHCache配置类
package com.image.cache.config;
import java.lang.reflect.Field;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import net.sf.ehcache.Cache;
import net.sf.ehcache.config.CacheConfiguration;
@Configuration
public class EhCacheConfig {
@Bean
EhCacheManagerFactoryBean cacheManager() {
return new EhCacheManagerFactoryBean();
}
@Bean
EhCacheCacheManager testEhCacheManager() throws IllegalArgumentException, IllegalAccessException {
return new EhCacheCacheManager(cacheManager().getObject());
}
private CacheConfiguration setCacheConfiguration(String cacheName) {
CacheConfiguration cacheConfig = new CacheConfiguration()
.eternal(false) // if true, timeouts are ignored
.timeToIdleSeconds(3600) // time since last accessed before item is marked for removal
.timeToLiveSeconds(3600 * 24) // time since inserted before item is marked for removal
.maxEntriesLocalHeap(10000) // total items that can be stored in cache
.memoryStoreEvictionPolicy("LRU") // eviction policy for when items exceed cache. LRU = Least Recently Used
.name(cacheName);
return cacheConfig;
}
}
升级到 Spring Boot 3 后出现以下异常
The import org.springframework.cache.ehcache cannot be resolved
不是最佳选择,但您可以使用旧版本的 spring-context-support ,这应该可以让您使用 EhcacheManagerFactoryBean。
我使用了它并将其明确添加到我的 pom 中:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.3.34</version>
</dependency>