+-
Spring-从XML注入HashMap>
现在,我有一个名为A的类,其中包含这样的属性:

private Map<Class<?>, List<EntityIndexConfig>> relatedEntitiesMap;

我最初的想法是从XML注入一些键及其各自的列表,但无法使其起作用.相反,我使用@PostConstruct创建了一个方法:

@PostConstruct @SuppressWarnings("serial")
public void loadRelatedEntities() {
   /* And here I load it */
   relatedEntitiesMap = new HashMap<Class<?>, List<EntityIndexConfig>>(){{
       put(Agency.class, new ArrayList<EntityIndexConfig>() {{
           add(new EntityIndexConfig("Package.listByAgency", applicationContext.getBean(PackageRepository.class), "agencyId"));
       }});
       /* More entries here... */
   }
}

尽管可行,但我希望能够在Spring XML文件中进行此配置.有什么想法如何创建Map并将类作为键和EntityIndexConfig的列表?

如果您知道的话,我将不胜感激.谢谢.

最佳答案
如文档中的 here所述:

<util:map key-type="java.lang.Class">  
        <entry key="com.MyClass">  
            <util:list>  
              <ref bean="EntityIndexConfig1"/>  
              <bean class="com.mypackage.SomeEntityIndexConfig/>  
            </util:list>  
        </entry> 
</util:map> 

假设您已声明util名称空间前缀:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

请记住,我写了此徒手画,很可能在某处犯了错字或语法错误.但这至少应该使您非常接近.

点击查看更多相关文章

转载注明原文:Spring-从XML注入HashMap> - 乐贴网