Spring使用value-type作为接口注入map

时间:2016-07-01 00:41:45

标签: java spring spring-mvc

我想将这个HashMap注入我的CentralCache bean,其中Cacheable是一个接口。在applicationContext.xml文件中我有:

    <bean id="externalSystemView" class="beans.ExternalSystemViewBean"> 
    </bean>

    <util:map id="cacheMap" map-class="java.util.HashMap" key-type="java.lang.String" value-type="caches.CacheableBean">
        <entry key="externalSystemView" value-ref="externalSystemView" />
    </util:map>

    <bean id="centralCache" class="caches.CentralCache">
        <property name="cacheableBeanMap" ref="cacheMap"/>
    </bean>

这是我的CacheableBean接口:

    public interface CacheableBean extends Serializable{
        public void update();
    }

我的ExternalSystemViewBean是map条目的值:

    @ManagedBean(name="externalSystemView")
    public class ExternalSystemViewBean implements CacheableBean {

        /**
         * 
         */
        private static final long serialVersionUID = -2314127844478571215L;

        ...

        @Override
        public void update() {
             ...
        }

    }

最后我的CentralCache:

    public class CentralCache {
    private CacheManager cm;
    private HashMap<String, CacheableBean> cacheableBeanMap = new HashMap<>(); 
    private HashMap<String, CacheWrapper> cacheMap = new HashMap<>();
    private Logger logger = Logger.getLogger(CentralCache.class);

    public CentralCache() {
        logger.info("creating CentralCache bean.");
        this.cm = CacheManager.create();
    }

    public HashMap<String, CacheableBean> getCacheableBeanMap() {
        return this.cacheableBeanMap;
    }

    public void setCacheableBeanMap(HashMap<String, CacheableBean> cacheableBeanMap) {
        logger.info("Starting to assign properties");
        for (Map.Entry<String, CacheableBean> entry : cacheableBeanMap.entrySet()) {
            String key = (String) entry.getKey();
            CacheableBean value = (CacheableBean) entry.getValue();
            CacheWrapper wrapper = new CacheWrapper(key, this.cm, value);
            this.cacheMap.put(key, wrapper);
        }
    }

    public CacheableBean getBean(String beanName) {
        return cacheMap.get(beanName).getBean();
    }
}

它给了我以下错误:

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'centralCache' defined in ServletContext resource [/WEB-INF/config/web-application-config.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'cacheableBeanMap' threw exception; nested exception is java.lang.NullPointerException
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1427)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1132)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
        at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5016)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5528)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
        at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1090)
        at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1900)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
        at java.util.concurrent.FutureTask.run(FutureTask.java:262)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'cacheableBeanMap' threw exception; nested exception is java.lang.NullPointerException
        at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:101)
        at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:57)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1424)
        ... 26 more

但是,如果我将value-type更改为ExternalSystemViewBean本身(当然相关类中的任何相关内容)。它会工作。那么Spring不支持接口作为map的值类型?或者它不支持map的值类型中的任何超类型?

0 个答案:

没有答案