Spring配置文件中注入复杂类型属性
生活随笔
收集整理的這篇文章主要介紹了
Spring配置文件中注入复杂类型属性
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Spring在整合其他框架時在配置文件中可能會涉及到復雜類型屬性的注入,以下舉例說明:
1.數組類型
2.List集合類型
3.Set集合類型
4.Map集合類型
5.Properties屬性類型
直接上代碼:
實體類:CollectionBean.java
package com.imooc.ioc.demo5;import java.util.*;/*** Spring復雜類型的集合注入*/ public class CollectionBean {private String[] arrs; //數組類型private List<String> list; //list類型private Set<String> set; //set類型private Map<String , Integer> map; //map類型private Properties properties; //屬性類型public String[] getArrs() {return arrs;}public void setArrs(String[] arrs) {this.arrs = arrs;}public List<String> getList() {return list;}public void setList(List<String> list) {this.list = list;}public Set<String> getSet() {return set;}public void setSet(Set<String> set) {this.set = set;}public Map<String, Integer> getMap() {return map;}public void setMap(Map<String, Integer> map) {this.map = map;}public Properties getProperties() {return properties;}public void setProperties(Properties properties) {this.properties = properties;}@Overridepublic String toString() {return "CollectionBean{" +"arrs=" + Arrays.toString(arrs) +", list=" + list +", set=" + set +", map=" + map +", properties=" + properties +'}';} }
?
配置文件:applicationContext.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/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="collectionBean" class="com.imooc.ioc.demo5.CollectionBean"><!--數組類型注入--><property name="arrs"><list><value>111</value><value>222</value><value>333</value></list></property><!--list類型注入--><property name="list"><list><value>aaa</value><value>bbb</value><value>ccc</value></list></property><!--set類型注入--><property name="set"><set><value>ddd</value><value>eee</value><value>fff</value></set></property><!--map類型注入--><property name="map"><map><entry key="aaa" value="111"></entry><entry key="bbb" value="222"></entry><entry key="ccc" value="333"></entry></map></property><!--properties類型注入--><property name="properties"><props><prop key="aaa">555</prop><prop key="bbb">666</prop><prop key="ccc">777</prop></props></property></bean> </beans>
?
測試類:SpringDemo5.java
package com.imooc.ioc.demo5;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringDemo5 {@Testpublic void demo1(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");CollectionBean collectionContext = (CollectionBean)applicationContext.getBean("collectionBean");System.out.println(collectionContext);}}
測試類輸出結果:
?
轉載于:https://www.cnblogs.com/cuijiade/p/9573663.html
總結
以上是生活随笔為你收集整理的Spring配置文件中注入复杂类型属性的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 不同数据类型的处理函数(一)
- 下一篇: 求校园乐队题材电影