你已经看到了如何使用 value 属性来配置基本数据类型和在你的 bean 配置文件中使用<property>
标签的 ref 属性来配置对象引用。这两种情况下处理奇异值传递给一个 bean。
现在如果你想传递多个值,如 java collection 类型 list、set、map 和 properties,应该怎么做呢。为了处理这种情况,spring 提供了四种类型的集合的配置元素,如下所示:
元素 | 描述 |
---|---|
<list> | 它有助于连线,如注入一列值,允许重复。 |
<set> | 它有助于连线一组值,但不能重复。 |
<map> | 它可以用来注入名称-值对的集合,其中名称和值可以是任何类型。 |
<props> | 它可以用来注入名称-值对的集合,其中名称和值都是字符串类型。 |
你可以使用<list>
或<set>
来连接任何 java.util.collection
的实现或数组。
你会遇到两种情况(a)传递集合中直接的值(b)传递一个 bean 的引用作为集合的元素。
我们在适当的位置使用 eclipse ide,然后按照下面的步骤来创建一个 spring 应用程序:
步骤 | 描述 |
---|---|
1 | 创建一个名称为 springexample 的项目,并且在创建项目的 src 文件夹中创建一个包 com.tutorialspoint 。 |
2 | 使用 add external jars 选项,添加所需的 spring 库,解释见 spring hello world example 章节。 option as explained in the chapter. |
3 | 在 com.tutorialspoint 包中创建java类texteditor、spellchecker 和 mainapp。 |
4 | 在 src 文件夹中创建 beans 配置文件 beans.xml。 |
5 | 最后一步是创建的所有java文件和bean配置文件的内容,并运行应用程序,解释如下所示。 |
这里是 javacollection.java 文件的内容:
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.java 文件的内容:
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="india"/>
<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>
一旦你创建源代码和 bean 配置文件完成后,我们就可以运行该应用程序。你应该注意这里不需要配置文件。如果你的应用程序一切都正常,将输出以下信息:
list elements :[india, pakistan, usa, usa]
set elements :[india, pakistan, usa]
map elements :{1=india, 2=pakistan, 3=usa, 4=usa}
property elements :{two=pakistan, one=india, three=usa, four=usa}
下面的 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="india"/>
<entry key ="two" value-ref="address1"/>
<entry key ="three" value-ref="address2"/>
</map>
</property>
</bean>
</beans>
为了使用上面的 bean 定义,你需要定义 setter 方法,它们应该也能够是用这种方式来处理引用。
如果你需要传递一个空字符串作为值,那么你可以传递它,如下所示:
<bean id="..." class="examplebean">
<property name="email" value=""/>
</bean>
前面的例子相当于 java 代码:examplebean.setemail("")。
如果你需要传递一个 null 值,那么你可以传递它,如下所示:
<bean id="..." class="examplebean">
<property name="email"><null/></property>
</bean>
前面的例子相当于 java 代码:examplebean.setemail(null)。