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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

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

發布時間:2023/11/27 生活经验 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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配置文件中注入复杂类型属性的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。