Ehcache无法使用java中的泛型类进行初始化
我使用了Ehcache 3,并将缓存声明为:Ehcache无法使用java中的泛型类进行初始化,java,ehcache,ehcache-3,Java,Ehcache,Ehcache 3,我使用了Ehcache 3,并将缓存声明为: private Cache<String,GenericClassForList<Person>> personCache; 私有缓存personCache; 例如:我有许多要放入缓存的对象列表,它们来自数据库: List<Person> personlist=perDao.getAll(); List<Customer> custList=comDao.getAll(); List<Com
private Cache<String,GenericClassForList<Person>> personCache;
私有缓存personCache;
例如:我有许多要放入缓存的对象列表,它们来自数据库:
List<Person> personlist=perDao.getAll();
List<Customer> custList=comDao.getAll();
List<Company> companylist=compDao.getAll();
List personlist=perDao.getAll();
List custList=comDao.getAll();
List companylist=compDao.getAll();
因此,我需要将这些不同类型的列表放入缓存中。因此,我尝试使用泛型类将这些列表初始化为:
package com.cache.cachemanager;
import java.util.ArrayList;
import java.util.Collection;
public class GenericClassForList<T> extends ArrayList<T> {
public GenericClassForList(final Collection<? extends T> c) {
super(c);
}
}
package com.cache.cachemanager;
导入java.util.ArrayList;
导入java.util.Collection;
公共类GenericClassForList扩展了ArrayList{
public GenericClassForList(最终集合,错误消息显示将GenericClassForList.class
替换为GenericClassForList.class
为什么编译器不允许它。因为
“类型擦除可确保不会为参数化类型创建新类;因此,泛型不会产生运行时开销”因为错误消息显示将GenericClassForList.class替换为GenericClassForList.class
public class CacheHelper {
private CacheManager cacheManager;
private Cache<String,GenericClassForList<Person>> personCache;
public CacheHelper() {
}
//initialziing cache
public void putInCacheFromDb() {
System.getProperties().setProperty("java -Dnet.sf.ehcache.use.classic.lru", "true");
cacheManager= CacheManagerBuilder
.newCacheManagerBuilder().build();
cacheManager.init();
personCache = cacheManager
.createCache("cacheOfe", CacheConfigurationBuilder
.newCacheConfigurationBuilder(
String.class,GenericClassForList<Person>.class,
ResourcePoolsBuilder.heap(100000)).withExpiry(Expirations.timeToLiveExpiration(Duration.of(60000,
TimeUnit.SECONDS))));
}
}
.newCacheConfigurationBuilder(
String.class,GenericClassForList<Person>.class,