簡體   English   中英

Ehcache無法使用Java中的Generic類進行初始化

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

我已經將Ehcache 3和Declared Cache用作:

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

例如:我有許多要放入緩存的對象列表,這些對象來自數據庫:

List<Person> personlist=perDao.getAll();
List<Customer> custList=comDao.getAll();
List<Company> 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);
    }


}

我從這里訪問此通用類為:

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

但是,在GenericClassForList<Person>.class這一行代碼中,如下所示,它向我顯示紅色標記(錯誤):

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

我無法初始化緩存,因為它顯示了我:

無法從參數化類型中選擇

我的通用類中是否有任何問題,或者如何使我的緩存能夠識別該通用類?我的緩存沒有給我初始化該通用類。

如錯誤消息所述,將GenericClassForList<Person>.class替換為GenericClassForList.class

為什么編譯器不允許這樣做。 由於類型擦除

“類型擦除確保不會為參數化類型創建新類;因此,泛型不會產生運行時開銷”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM