日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring 注入集合

發(fā)布時間:2023/12/3 javascript 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring 注入集合 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

轉(zhuǎn)載自? ?Spring 注入集合

注入集合

你已經(jīng)看到了如何使用?value?屬性來配置基本數(shù)據(jù)類型和在你的 bean 配置文件中使用<property>標簽的?ref?屬性來配置對象引用。這兩種情況下處理奇異值傳遞給一個 bean。

現(xiàn)在如果你想傳遞多個值,如 Java Collection 類型 List、Set、Map 和 Properties,應該怎么做呢。為了處理這種情況,Spring 提供了四種類型的集合的配置元素,如下所示:

元素描述
<list>它有助于連線,如注入一列值,允許重復。
<set>它有助于連線一組值,但不能重復。
<map>它可以用來注入名稱-值對的集合,其中名稱和值可以是任何類型。
<props>它可以用來注入名稱-值對的集合,其中名稱和值都是字符串類型。

你可以使用<list>或<set>來連接任何?java.util.Collection?的實現(xiàn)或數(shù)組。

你會遇到兩種情況(a)傳遞集合中直接的值(b)傳遞一個 bean 的引用作為集合的元素。

例子

我們在適當?shù)奈恢檬褂?Eclipse IDE,然后按照下面的步驟來創(chuàng)建一個 Spring 應用程序:

步驟描述
1創(chuàng)建一個名稱為?SpringExample?的項目,并且在創(chuàng)建項目的?src?文件夾中創(chuàng)建一個包?com.tutorialspoint?。
2使用?Add External JARs?選項,添加所需的 Spring 庫,解釋見?Spring Hello World Example?章節(jié)。 option as explained in the chapter.
3在?com.tutorialspoint?包中創(chuàng)建Java類TextEditor、SpellChecker?和?MainApp。
4在?src?文件夾中創(chuàng)建 Beans 配置文件?Beans.xml。
5最后一步是創(chuàng)建的所有Java文件和Bean配置文件的內(nèi)容,并運行應用程序,解釋如下所示。

這里是?JavaCollection.java?文件的內(nèi)容:

package com.tutorialspoint; import java.util.*; public class JavaCollection {List addressList;Set addressSet;Map addressMap;Properties addressProp;// a setter method to set Listpublic 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 Setpublic 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 Mappublic 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 Propertypublic 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.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/beanshttp://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>

一旦你創(chuàng)建源代碼和 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 定義將幫助你理解如何注入 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/beanshttp://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)。

總結(jié)

以上是生活随笔為你收集整理的Spring 注入集合的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。