Spring教程 - Spring Map属性

2018-01-09 19:06 更新

Spring教程 - Spring Map属性


我们可以将值或值列表填充到Spring xml配置文件中定义的Java bean。

以下部分显示如何向地图填充数据。

Java Bean

为了展示如何使用xml配置文件来填充集合属性,我们定义了一个具有四个集合属性的Customer对象。

package com.www.w3cschool.cnmon;

import java.util.HashMap;
import java.util.Map;

public class Customer {
  private Map<Object, Object> maps = new HashMap<Object, Object>();

  public String toString() {
    return maps.toString();
  }

  public Map<Object, Object> getMaps() {
    return maps;
  }

  public void setMaps(Map<Object, Object> maps) {
    this.maps = maps;
  }
}

这里是Person bean。

package com.www.w3cschool.cnmon;

public class Person {
  private String name;
  private int age;
  private String address;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }

  public String getAddress() {
    return address;
  }

  public void setAddress(String address) {
    this.address = address;
  }

  @Override
  public String toString() {
    return "Person [name=" + name + ", age=" + age + ", address=" + address
        + "]";
  }  
}


地图

以下代码显示如何将数据填充到java.util.Set类型化属性。

代码填充三个值。 第一个是硬编码值1.第二个是一个bean参考。 我们必须在某处定义PersonBean,以便在此处使用它。 第三个是bean定义与属性设置。 当为java.util.Map填充数据时,我们必须提供键和值对。

...
<property name="maps">
    <map>
      <entry key="Key 1" value="1" />
      <entry key="Key 2" value-ref="PersonBean" />
      <entry key="Key 3">
        <bean class="com.www.w3cschool.cnmon.Person">
          <property name="name" value="java2sMap" />
          <property name="address" value="address" />
          <property name="age" value="28" />
        </bean>
      </entry>
    </map>
</property>
...


例子

Full Spring的bean配置文件。

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  <bean id="CustomerBean" class="com.www.w3cschool.cnmon.Customer">
    <!-- java.util.Map -->
    <property name="maps">
      <map>
        <entry key="Key 1" value="1" />
        <entry key="Key 2" value-ref="PersonBean" />
        <entry key="Key 3">
          <bean class="com.www.w3cschool.cnmon.Person">
            <property name="name" value="java2sMap" />
            <property name="address" value="address" />
            <property name="age" value="28" />
          </bean>
        </entry>
      </map>
    </property>
  </bean>
  <bean id="PersonBean" class="com.www.w3cschool.cnmon.Person">
    <property name="name" value="java2s1" />
    <property name="address" value="address 1" />
    <property name="age" value="28" />
  </bean>
</beans>

下面是加载和运行配置的代码。

package com.www.w3cschool.cnmon;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App 
{
    public static void main( String[] args )
    {
      ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml");
      Customer cust = (Customer)context.getBean("CustomerBean");
      System.out.println(cust);
    }
}

输出


Download Java2s_Spring_Map_Properties.zip

MapFactoryBean

MapFactoryBean类可以创建Map集合类HashMap或TreeMap在Spring的bean配置文件中。

以下代码显示了如何创建HashMap,填充数据,然后将其注入到bean属性中。

这里是Java bean类。

package com.www.w3cschool.cnmon;
//from w  w  w .  ja va2 s  .  c  om
import java.util.HashMap;
import java.util.Map;

public class Customer {
  private Map<Object, Object> maps = new HashMap<Object, Object>();

  public String toString() {
    return maps.toString();
  }

  public Map<Object, Object> getMaps() {
    return maps;
  }

  public void setMaps(Map<Object, Object> maps) {
    this.maps = maps;
  }
}

这里是Spring的bean配置文件。

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  <bean id="CustomerBean" class="com.www.w3cschool.cnmon.Customer">
    <property name="maps">
      <bean class="org.springframework.beans.factory.config.MapFactoryBean">
        <property name="targetMapClass">
          <value>java.util.HashMap</value>
        </property>
        <property name="sourceMap">
          <map>
            <entry key="Key1" value="1" />
            <entry key="Key2" value="2" />
            <entry key="Key3" value="3" />
          </map>
        </property>
      </bean>
    </property>
  </bean>
</beans>

Download Java2s_Spring_MapFactoryBean.zip

MapFactoryBean...

我们还可以使用util模式和< util:map> 以将数据填充到java.util.Map。

<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-2.5.xsd
  http://www.springframework.org/schema/util
  http://www.springframework.org/schema/util/spring-util-2.5.xsd">
  <bean id="CustomerBean" class="com.www.w3cschool.cnmon.Customer">
    <property name="maps">
      <util:map map-class="java.util.HashMap">
        <entry key="Key1" value="1" />
        <entry key="Key2" value="2" />
        <entry key="Key3" value="3" />
      </util:map>
    </property>
  </bean>
</beans>

Download Java2s_Spring_Map_util.zip

这里是运行应用程序的代码。

package com.www.w3cschool.cnmon;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App 
{
    public static void main( String[] args )
    {
      ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml");
      Customer cust = (Customer)context.getBean("CustomerBean");
      System.out.println(cust);
    }
}

上面的代码生成以下结果。

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号