2

我正在使用 spring 表达式从一些配置中读取地图。现在我该如何构造一个 bean。

<bean id="xyz"  class= "java.util.Map" >
   <!-- how to pass $xyz to this -->
    <!-- i know how to pass static entries to  it  using entry tag , but how abut map? !-->
</bean>

如何将我的地图传递给 bean xyz?

对于字符串,可以使用以下代码。

<bean id="x" class ="java.util.String >
  <construtor-arg>  $x
   </constructort-arg>
</bean>

但是对于 map 没有将 map 作为 inp 的构造函数

4

1 回答 1

4

使用 Spring util 架构

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
      http://www.springframework.org/schema/util
      http://www.springframework.org/schema/util/spring-util.xsd">


  <bean id="yourBean" class="yourBean">
    <property name="aPrameterOfTypeMapInYourBean">
      <map>
        <entry key="first" value="hallo"/>
        <entry key="second" value="world"/>
        <entry key="third" value="that is easy"/>
        <entry key="forth" value="and hopefully solve your problem"/>

        <entry key="yourParam" value="$x"/>

      </map>
    </property>
  </bean>

</beans>

Spring 还支持集合的合并(<map>应该有一个合并属性)。因此,您需要定义两个映射,并将其中一个指定为另一个的父级。

更多细节:

于 2013-04-12T07:11:21.813 回答