简体   繁体   English

Ehcache无法使用Java中的Generic类进行初始化

[英]Ehcache not able to initialize with the Generic class in java

I have used Ehcache 3 and Declared Cache as: 我已经将Ehcache 3和Declared Cache用作:

 private Cache<String,GenericClassForList<Person>> personCache;

For example:I have many List of object to put in Cache which are coming from database: 例如:我有许多要放入缓存的对象列表,这些对象来自数据库:

List<Person> personlist=perDao.getAll();
List<Customer> custList=comDao.getAll();
List<Company> companylist=compDao.getAll();

So,i need to put these different kind of List in my cache.So,I tried to make generic class to initialize these list as: 因此,我需要将这些不同类型的列表放入我的缓存中。因此,我试图使通用类将这些列表初始化为:

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


}

I am accessing this generic class from here as: 我从这里访问此通用类为:

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

But,at this line of code in GenericClassForList<Person>.class as shown below,it is showing me red marker(error): 但是,在GenericClassForList<Person>.class这一行代码中,如下所示,它向我显示红色标记(错误):

.newCacheConfigurationBuilder(
                                String.class,GenericClassForList<Person>.class,

I am not able to initialize the cache as it shows me: 我无法初始化缓存,因为它显示了我:

Cannot select from parameterized type 无法从参数化类型中选择

Is there any problem in my generic class or how can I make my cache able to identify this generic class?my cache is not giving me to initialize this generic class. 我的通用类中是否有任何问题,或者如何使我的缓存能够识别该通用类?我的缓存没有给我初始化该通用类。

As the error message says replace GenericClassForList<Person>.class with GenericClassForList.class 如错误消息所述,将GenericClassForList<Person>.class替换为GenericClassForList.class

Why it's not allowed by the compiler. 为什么编译器不允许这样做。 Because of Type Erasure 由于类型擦除

"Type erasure ensures that no new classes are created for parameterized types; consequently, generics incur no runtime overhead" “类型擦除确保不会为参数化类型创建新类;因此,泛型不会产生运行时开销”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM