當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring 注入集合的成员变量属性
生活随笔
收集整理的這篇文章主要介紹了
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>輸出:
總結
以上是生活随笔為你收集整理的Spring 注入集合的成员变量属性的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MAC 把隔空投送添加到个人收藏边栏
- 下一篇: 魔兽世界怀旧服TBC防骑天赋怎么加