您曾经理解了如何应用值属性配置原始数据类型,并应用Bean 配置文件中的 <property> 标记的ref属性配置对象援用。这两种状况都波及将奇怪值传递给 bean。
当初,如果您想传递多个值,例如 Java 汇合类型,例如 List、Set、Map 和 Properties,该怎么办。为了解决这种状况,Spring 提供了四种类型的汇合配置元素,如下所示 -
没有 元素和形容
1
<列表>
这有助于接线,即注入值列表,容许反复。
2
<设置>
这有助于连贯一组值但没有任何反复。
3
<地图>
这可用于注入名称-值对的汇合,其中名称和值能够是任何类型。
4
<道具>
这可用于注入名称和值都是字符串的名称-值对汇合。
您能够应用 <list> 或 <set> 来连贯 java.util.Collection 或数组的任何实现。
您将遇到两种状况 (a) 传递汇合的间接值和 (b) 传递 bean 的援用作为汇合元素之一。
例子
让咱们有一个工作的 Eclipse IDE 并采取以下步骤来创立一个 Spring 应用程序 -
脚步 形容
1 创立一个名为SpringExample的我的项目,并在创立的我的项目的src文件夹下创立一个包com.tutorialspoint。
2 应用增加内部 JAR选项增加所需的 Spring 库,如Spring Hello World 示例章节中所述。
3 创立Java类JavaCollection和MainApp下com.tutorialspoint包。
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.or...d">
<!-- 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 = "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]
ap Elements :{1 = INDIA, 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.or...d">
<!-- 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 办法,以便它们也应该可能解决援用。
注入 null 和空字符串值
如果您须要传递一个空字符串作为值,那么您能够按如下形式传递它 -
<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)