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

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人文社科 > 生活经验 >内容正文

生活经验

Spring配置文件中注入复杂类型属性

發(fā)布時(shí)間:2023/11/27 生活经验 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)題。

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