詳解Java的Spring框架中bean的注入集合
使用value屬性和使用<property>標(biāo)簽的ref屬性在你的bean配置文件中的對(duì)象引用,這兩種情況下可以處理單值到一個(gè)bean,如果你想通過(guò)多元值,如Java Collection類(lèi)型List, Set, Map 及 Properties。要處理這種情況,Spring提供了四種類(lèi)型的如下集合的配置元素:
可以使用<list> 或<set> 來(lái)連接任何實(shí)現(xiàn)java.util.Collection或數(shù)組。
會(huì)遇到兩種情況(a)將收集的直接的值及(b)傳遞一個(gè)bean的引用作為集合的元素之一。
例子:
我們使用Eclipse IDE,然后按照下面的步驟來(lái)創(chuàng)建一個(gè)Spring應(yīng)用程序:
這里是JavaCollection.java文件的內(nèi)容:
package com.yiibai; 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文件的內(nèi)容:
package com.yiibai; 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文件里面有配置的集合的所有類(lè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.yiibai.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>
創(chuàng)建源代碼和bean配置文件完成后,讓我們運(yùn)行應(yīng)用程序。如果應(yīng)用程序一切順利,這將打印以下信息:
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定義,需要定義這樣一種方式,他們應(yīng)該能夠處理的參考,以及setter方法。
注入null和空字符串的值
如果需要傳遞一個(gè)空字符串作為值,如下所示:
<bean id="..." class="exampleBean"> <property name="email" value=""/> </bean>
前面的例子等同于Java代碼: exampleBean.setEmail("")
如果需要傳遞一個(gè)null值,如下所示:
<bean id="..." class="exampleBean"> <property name="email"><null/></property> </bean>
前面的例子等同于Java代碼:exampleBean.setEmail(null)
相關(guān)文章
SpringBoot2.0集成WebSocket實(shí)現(xiàn)后臺(tái)向前端推送信息
這篇文章主要介紹了SpringBoot2.0集成WebSocket實(shí)現(xiàn)后臺(tái)向前端推送信息,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01Java中Quartz高可用定時(shí)任務(wù)快速入門(mén)
如果你想做定時(shí)任務(wù),有高可用方面的需求,或者僅僅想入門(mén)快,上手簡(jiǎn)單,那么選用它準(zhǔn)沒(méi)錯(cuò),感興趣的小伙伴們可以參考一下2022-04-04java serialVersionUID解決序列化類(lèi)版本不一致問(wèn)題面試精講
這篇文章主要為大家介紹了serialVersionUID解決序列化類(lèi)版本不一致問(wèn)題的面試精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10Java實(shí)現(xiàn)ATM機(jī)操作系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)ATM機(jī)操作系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05Linux系統(tǒng)下搭建Java開(kāi)發(fā)環(huán)境
本文主要是記錄了如何在Linux環(huán)境下一步步安裝JAVA JDK環(huán)境,非常簡(jiǎn)單實(shí)用,有需要的朋友可以參考下2014-10-10Spring、SpringMVC和SpringBoot的區(qū)別及說(shuō)明
這篇文章主要介紹了Spring、SpringMVC和SpringBoot的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2022-10-10