第十三章 Spring注入集合(Spring Framework3.1教程)

你已经看到在你的Bean配置文件<property>标签中如何使用“value"属性配置基本类型数据和使用”ref“属性配置对象引用类型。这两种情况都是处理单一值到bean。

现在怎么样,如果你想配置Java集合类型的值如List、Set、Map、Properties。处理这种情况Spring提供了四种类型的集合配置元素,如下:

元素

描述

<list>

这个帮助包装注入list的值允许重复

<set>

这个帮助装配set类型的值不允许重复。

<map>

这个使用注入名值对类型的集合,名值对可以是任何类型。

<props>

这个使用注入名值对类型的集合,名值对必须是String类型。

你可以使用<list>或者<set>包装任何java.unit.Collection或一个数组的实现。

你会遇到两种情况一个是传递简单的值类型的集合,另一个传递引用类型的bean的集合。

示例

让我们使用EclipseIDE按如下的步骤创建一个Spring应用:

步骤 

描述

1

创建一个SpringExample的项目并在src目录下创建com.tutorialspoint包。

2

在Add External JARs选项卡中添加Spring库如在Spring Hello World章节中所讲。

3

在com.tutorialspoint包下创建JavaCollection、MainApp类。

4

在src目录下创建bean的配置文件Beans.xml

5

最后一步在Java类中和Bean配置文件中添加内容,并运行应用。

 

如下是JavaCollection的源代码:

	package com.tutorialspoint;

import java.util.*;

public class JavaCollection {
    List addressList;
    Set addressSet;
    Map addressMap;
    Properties addressProp;
 // a setter method to set List
    public void setAddressList(List addressList) {
        this.addressList = addressList;
    }

    // prints and returns all the elements of the list.
    public List getAddressList() {
        System.out.println("List Elements :" + addressList);
        return addressList;
    }

    // a setter method to set Set
    public void setAddressSet(Set addressSet) {
        this.addressSet = addressSet;
    }

    // prints and returns all the elements of the Set.
    public Set getAddressSet() {
        System.out.println("Set Elements :" + addressSet);
        return addressSet;
    }

    // a setter method to set Map
    public void setAddressMap(Map addressMap) {
        this.addressMap = addressMap;
    }

    // prints and returns all the elements of the Map.
    public Map getAddressMap() {
        System.out.println("Map Elements :" + addressMap);
        return addressMap;
    }

    // a setter method to set Property
    public void setAddressProp(Properties addressProp) {
        this.addressProp = addressProp;
    }

    // prints and returns all the elements of the Property.
    public Properties getAddressProp() {
        System.out.println("Property Elements :" + addressProp);
        return addressProp;
    }
}

如下是MainApp的源代码:

package com.tutorialspoint;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");

        JavaCollection jc = (JavaCollection) context.getBean("javaCollection");

        jc.getAddressList();
        jc.getAddressSet();
        jc.getAddressMap();
        jc.getAddressProp();
     }
}

如下是包含所有类型集合的配置文件Beans.xml:

<?xml version="1.0" encoding="UTF-8"?>

<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-3.0.xsd">

	<!-- Definition for javaCollection -->
	<bean id="javaCollection" class="com.tutorialspoint.JavaCollection">

		<!-- results in a setAddressList(java.util.List) call -->
		<property name="addressList">
			<list>
				<value>INDIA</value>
				<value>Pakistan</value>
				<value>USA</value>
				<value>USA</value>
			</list>
		</property>
<!-- results in a setAddressSet(java.util.Set) call -->
		<property name="addressSet">
			<set>
				<value>INDIA</value>
				<value>Pakistan</value>
				<value>USA</value>
				<value>USA</value>
			</set>
		</property>

		<!-- results in a setAddressMap(java.util.Map) call -->

		<property name="addressMap">
			<map>
				<entry key="1" value="NDIA" />
				<entry key="2" value="Pakistan" />
				<entry key="3" value="USA" />
				<entry key="4" value="USA" />
			</map>
		</property>

		<!-- results in a setAddressProp(java.util.Properties) call -->
		<property name="addressProp">
			<props>
				<prop key="one">INDIA</prop>
				<prop key="two">Pakistan</prop>
				<prop key="three">USA</prop>
				<prop key="four">USA</prop>
			</props>
		</property>
	</bean>
</beans>

一旦你完成了上面源代码和配置文件,运行应用,如果一切正常,会打印如下消息:

List Elements :[INDIA, Pakistan, USA, USA]
Set Elements :[INDIA, Pakistan, USA]
Map Elements :{1=NDIA, 2=Pakistan, 3=USA, 4=USA}
Property Elements :{two=Pakistan, one=INDIA, three=USA, four=USA}

注入Bean的引用

如下的bean定义会帮助你理解如何注入一个bean的引用为集合的一个元素。甚至你可以混合引用和值在一起如下所示:

<?xml version="1.0" encoding="UTF-8"?>

<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-3.0.xsd">

	<!-- Bean Definition to handle references and values -->
	<bean id="..." class="...">

		<!-- Passing bean reference for java.util.List -->
		<property name="addressList">
			<list>
				<ref bean="address1" />
				<ref bean="address2" />
				<value>Pakistan</value>

			</list>
		</property>

		<!-- Passing bean reference for java.util.Set -->
		<property name="addressSet">
			<set>
				<ref bean="address1" />
				<ref bean="address2" />
				<value>Pakistan</value>
			</set>
		</property>

		<!-- Passing bean reference for java.util.Map -->
		<property name="addressMap">
			<map>
				<entry key="one" value="NDIA" />
				<entry key="two" value-ref="address1" />
				<entry key="three" value-ref="address2" />
			</map>
		</property>
	</bean>
</beans>

使用上面的bean定义,你需要定义setter方法他们也可以处理引用。

注入null和空string值

如果你需要传递一个空值你可以如下传递:

<bean id="..." class="exampleBean">
	<property name="email" value="" />
</bean> 

之前的例子等价于:exampleBean.setEmail("")。如果你需要传递NULL值你可以如下:

<bean id="..." class="exampleBean"> 
     <property name="email"><null/></property> 
 </bean>

之前的例子等价于Java代码:exampleBean.setEmail(null)






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值