使用<property>
標(biāo)簽的value
屬性配置原始數(shù)據(jù)類型和ref
屬性配置對象引用的方式來定義Bean配置文件主巍。這兩種情況都涉及將單一值傳遞給Bean。那么如果您想傳遞多個值序六,例如Java集合類型淘捡,如List眷篇、Set垂谢、Map和Properties怎么辦朽肥?為了處理這種情況,Spring提供了四種類型的集合配置元素虐杯,如下所示:
序號 | 元素 & 描述 |
---|---|
1 | <list> |
用于注入一組值帅戒,允許重復(fù)灯帮。 | |
2 | <set> |
用于注入一組值,但不允許重復(fù)逻住。 | |
3 | <map> |
可用于注入一組名稱-值對钟哥,其中名稱和值可以是任何類型。 | |
4 | <props> |
可用于注入一組名稱-值對瞎访,其中名稱和值都是字符串瞪醋。 |
您可以使用<list>
或<set>
來注入java.util.Collection
的任何實(shí)現(xiàn)或數(shù)組。
在處理集合時装诡,通常會遇到兩種情況:(a)傳遞集合的直接值和(b)將Bean的引用作為集合元素之一傳遞银受。
示例
假設(shè)您已經(jīng)準(zhǔn)備好Eclipse IDE,并采取以下步驟創(chuàng)建Spring應(yīng)用程序:
步驟 描述
1 創(chuàng)建一個名為SpringExample的項(xiàng)目鸦采,在創(chuàng)建的項(xiàng)目中的src文件夾下創(chuàng)建一個名為com.tutorialspoint的包宾巍。
2 使用"Add External JARs"選項(xiàng)添加所需的Spring庫,如Spring Hello World示例章節(jié)中所述渔伯。
3 在com.tutorialspoint包下創(chuàng)建Java類JavaCollection和MainApp顶霞。
4 在src文件夾下創(chuàng)建Beans配置文件Beans.xml。
5 最后一步是創(chuàng)建所有Java文件和Bean配置文件的內(nèi)容锣吼,并按以下說明運(yùn)行應(yīng)用程序选浑。
以下是JavaCollection.java文件的內(nèi)容:
package com.tutorialspoint;
import java.util.*;
public class JavaCollection {
List addressList;
Set addressSet;
Map addressMap;
Properties addressProp;
// 用于設(shè)置List的setter方法
public void setAddressList(List addressList) {
this.addressList = addressList;
}
// 打印并返回列表的所有元素。
public List getAddressList() {
System.out.println("List Elements :" + addressList);
return addressList;
}
// 用于設(shè)置Set的setter方法
public void setAddressSet(Set addressSet) {
this.addressSet = addressSet;
}
// 打印并返回Set的所有元素玄叠。
public Set getAddressSet() {
System.out.println("Set Elements :" + addressSet);
return addressSet;
}
// 用于設(shè)置Map的setter方法
public void setAddressMap(Map addressMap) {
this.addressMap = addressMap;
}
// 打印并返回Map的所有元素古徒。
public Map getAddressMap() {
System.out.println("Map Elements :" + addressMap);
return addressMap;
}
// 用于設(shè)置Property的setter方法
public void setAddressProp(Properties addressProp) {
this.addressProp = addressProp;
}
// 打印并返回Property的所有元素。
public Properties getAddressProp() {
System.out.println("Property Elements :" + addressProp);
return addressProp;
}
}
以下是MainApp.java文件的內(nèi)容:
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配置文件的內(nèi)容:
<?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">
<!-- 產(chǎn)生 setAddressList(java.util.List) 調(diào)用 -->
<property name = "addressList">
<list>
<value>INDIA</value>
<value>Pakistan</value>
<value>USA</value>
<value>USA</value>
</list>
</property>
<!-- 產(chǎn)生 setAddressSet(java.util.Set) 調(diào)用 -->
<property name = "addressSet">
<set>
<value>INDIA</value>
<value>Pakistan</value>
<value>USA</value>
<value>USA</value>
</set>
</property>
<!-- 產(chǎn)生 setAddressMap(java.util.Map) 調(diào)用 -->
<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>
<!-- 產(chǎn)生 setAddressProp(java.util.Properties) 調(diào)用 -->
<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>
當(dāng)您完成創(chuàng)建源代碼和Bean配置文件后读恃,讓我們運(yùn)行應(yīng)用程序隧膘。如果一切正常代态,應(yīng)用程序?qū)⒋蛴∫韵孪ⅲ?/p>
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定義將幫助您了解如何將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方法。
注入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)
最后
為了方便其他設(shè)備和平臺的小伙伴觀看往期文章,鏈接奉上:
公眾號搜索Let us Coding
腔呜,知乎判莉,開源中國,CSDN育谬,思否,掘金帮哈,InfoQ膛檀,簡書,博客園娘侍,慕課咖刃,51CTO,helloworld憾筏,騰訊開發(fā)者社區(qū)嚎杨,阿里開發(fā)者社區(qū)
看完如果覺得有幫助,歡迎點(diǎn)贊氧腰、收藏和關(guān)注