Spring配置文件中注入复杂类型属性
生活随笔
收集整理的這篇文章主要介紹了
Spring配置文件中注入复杂类型属性
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Spring在整合其他框架時(shí)在配置文件中可能會(huì)涉及到復(fù)雜類(lèi)型屬性的注入,以下舉例說(shuō)明:
1.數(shù)組類(lèi)型
2.List集合類(lèi)型
3.Set集合類(lèi)型
4.Map集合類(lèi)型
5.Properties屬性類(lèi)型
直接上代碼:
實(shí)體類(lèi):CollectionBean.java
package com.imooc.ioc.demo5;import java.util.*;/*** Spring復(fù)雜類(lèi)型的集合注入*/ public class CollectionBean {private String[] arrs; //數(shù)組類(lèi)型private List<String> list; //list類(lèi)型private Set<String> set; //set類(lèi)型private Map<String , Integer> map; //map類(lèi)型private Properties properties; //屬性類(lèi)型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"><!--數(shù)組類(lèi)型注入--><property name="arrs"><list><value>111</value><value>222</value><value>333</value></list></property><!--list類(lèi)型注入--><property name="list"><list><value>aaa</value><value>bbb</value><value>ccc</value></list></property><!--set類(lèi)型注入--><property name="set"><set><value>ddd</value><value>eee</value><value>fff</value></set></property><!--map類(lèi)型注入--><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類(lèi)型注入--><property name="properties"><props><prop key="aaa">555</prop><prop key="bbb">666</prop><prop key="ccc">777</prop></props></property></bean> </beans>
?
測(cè)試類(lèi):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);}}
測(cè)試類(lèi)輸出結(jié)果:
?
轉(zhuǎn)載于:https://www.cnblogs.com/cuijiade/p/9573663.html
總結(jié)
以上是生活随笔為你收集整理的Spring配置文件中注入复杂类型属性的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 不同数据类型的处理函数(一)
- 下一篇: Java基于Quartz的定时任务调度服