Spring?注入集合實(shí)現(xiàn)過程示例詳解
定義Bean配置文件
使用<property>
標(biāo)簽的value
屬性配置原始數(shù)據(jù)類型和ref
屬性配置對(duì)象引用的方式來定義Bean配置文件。這兩種情況都涉及將單一值傳遞給Bean。那么如果您想傳遞多個(gè)值,例如Java集合類型,如List、Set、Map和Properties怎么辦?為了處理這種情況,Spring提供了四種類型的集合配置元素
Spring集合配置元素
如下所示:
序號(hào) | 元素 & 描述 |
---|---|
1 | <list> |
用于注入一組值,允許重復(fù)。 | |
2 | <set> |
用于注入一組值,但不允許重復(fù)。 | |
3 | <map> |
可用于注入一組名稱-值對(duì),其中名稱和值可以是任何類型。 | |
4 | <props> |
可用于注入一組名稱-值對(duì),其中名稱和值都是字符串。 |
用<list>或<set>注入
您可以使用<list>
或<set>
來注入java.util.Collection
的任何實(shí)現(xiàn)或數(shù)組。
在處理集合時(shí),通常會(huì)遇到兩種情況:(a)傳遞集合的直接值和(b)將Bean的引用作為集合元素之一傳遞。
示例
假設(shè)您已經(jīng)準(zhǔn)備好Eclipse IDE,并采取以下步驟創(chuàng)建Spring應(yīng)用程序:
步驟描述
1 創(chuàng)建一個(gè)名為SpringExample的項(xiàng)目,在創(chuàng)建的項(xiàng)目中的src文件夾下創(chuàng)建一個(gè)名為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)
以上就是Spring 注入集合實(shí)現(xiàn)過程示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Spring 注入集合的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解析Java和Eclipse中加載本地庫(.dll文件)的詳細(xì)說明
本篇文章是對(duì)Java和Eclipse中加載本地庫(.dll文件)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05SpringBoot整合easyExcel實(shí)現(xiàn)CSV格式文件的導(dǎo)入導(dǎo)出
這篇文章主要為大家詳細(xì)介紹了SpringBoot整合easyExcel實(shí)現(xiàn)CSV格式文件的導(dǎo)入導(dǎo)出,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴可以參考下2024-02-02spring的TransactionalEventListener事務(wù)感知源碼解析
這篇文章主要為大家介紹了spring的TransactionalEventListener事務(wù)感知源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09SpringBoot關(guān)于自動(dòng)注入mapper為空的坑及解決
這篇文章主要介紹了SpringBoot關(guān)于自動(dòng)注入mapper為空的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07