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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring 注入集合的成员变量属性

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

Spring支持list,set,map和prop四種集合類型的注入。

看一個例子:JavaCollection這個類包含了上述介紹的4種集合類型的成員變量:

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:

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.sap.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>

輸出:

總結(jié)

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

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