javascript
Spring-注入参数详解-[通过util命名空间简化集合类型的配置]
- 概述
- 步驟
- 聲明命名空間和schema
- 配置Bean
- 配置一個(gè)Map
- 配置一個(gè)Set
- 配置一個(gè)List
- 配置一個(gè)Properties
- MapSetListProperties實(shí)例匯總
概述
如果希望配置一個(gè)集合類(lèi)型的Bean,而非一個(gè)集合類(lèi)型的屬性,則可以通過(guò)util命名空間進(jìn)行配置。
在spring的配置文件中util命名空間類(lèi)似于java.util包類(lèi)對(duì)應(yīng),util命名空間提供了集合相關(guān)的配置,在使用命名空間前要導(dǎo)入util命名空間。
步驟
代碼已托管到Github—> https://github.com/yangshangwei/SpringMaster
聲明命名空間和schema
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"<!-- 命名空間 -->xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd<!-- schema 如果未指定spring-util.xsd,Spring會(huì)自動(dòng)關(guān)聯(lián)到最新版本 -->http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">.....</beans>配置Bean
配置一個(gè)Map
POJO類(lèi)
package com.xgj.ioc.inject.construct.utilSchema;import java.util.Map;public class PetShop {private Pets pets;public void setPets(Pets pets) {this.pets = pets;}/*** * * @Title: petsInfo* * @Description: 獲取容器注入的Map,遍歷輸出* * * @return: void*/public void petsInfo_Map() {Map<Integer, String> map = pets.getPetMap();for (Map.Entry<Integer, String> entry : map.entrySet()) {System.out.println("編號(hào)Key = " + entry.getKey() + ",品種Value = "+ entry.getValue());}}}POJO類(lèi)
package com.xgj.ioc.inject.construct.utilSchema;import java.util.Map;public class Pets {private Map<Integer, String> petMap;public Map<Integer, String> getPetMap() {return petMap;}public void setPetMap(Map<Integer, String> petMap) {this.petMap = petMap;}}配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"><bean id="petShop" class="com.xgj.ioc.inject.construct.utilSchema.PetShop"><property name="pets" ref="pets" /></bean><!-- 第一種寫(xiě)法 ,通過(guò)ref引用,此時(shí)需要在 uitl-map中聲明id 推薦這種寫(xiě)法<bean id="pets" class="com.xgj.ioc.inject.construct.utilSchema.Pets"><property name="petMap" ref="petMap" /></bean><util:map id="petMap" map-class="java.util.HashMap"><entry key="101" value="dog" /><entry key="103" value="wolf" /><entry key="105" value="bird" /></util:map>--><!-- 第二種寫(xiě)法,嵌入內(nèi)部 --><bean id="pets" class="com.xgj.ioc.inject.construct.utilSchema.Pets"><property name="petMap"><util:map map-class="java.util.HashMap"><!-- 可以通過(guò)map-class顯示指定Map的實(shí)現(xiàn)類(lèi) --><entry key="101" value="dog" /><entry key="103" value="wolf" /><entry key="105" value="bird" /></util:map></property></bean></beans>測(cè)試類(lèi)
package com.xgj.ioc.inject.construct.utilSchema;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class UtilSchemaTest {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:com/xgj/ioc/inject/construct/utilSchema/beans.xml");PetShop petShop = ctx.getBean("petShop", PetShop.class);petShop.petsInfo_Map();} }運(yùn)行結(jié)果
<util:map>支持key-type和value-type屬性,指定Map的鍵和值的類(lèi)型
<util:map map-class="java.util.HashMap" key-type="java.lang.Integer" value-type="java.lang.String"><entry key="101" value="dog" /><entry key="103" value="wolf" /><entry key="105" value="bird" /></util:map><util:list> 和<util:set>支持value-type屬性,指定集合中的值的類(lèi)型
配置一個(gè)Set
見(jiàn) mapsetlistproperties實(shí)例匯總 代碼
配置一個(gè)List
見(jiàn) mapsetlistproperties實(shí)例匯總 代碼
配置一個(gè)Properties
見(jiàn) mapsetlistproperties實(shí)例匯總 代碼
Map、Set、List、Properties實(shí)例匯總
POJO類(lèi)
package com.xgj.ioc.inject.construct.utilSchema;import java.util.Iterator; import java.util.Map; import java.util.Set;public class PetShop {private Pets pets;public void setPets(Pets pets) {this.pets = pets;}/*** * * @Title: petsInfo* * @Description: 獲取容器注入的Map,遍歷輸出* * * @return: void*/public void petsInfo_Map() {Map<Integer, String> map = pets.getPetMap();for (Map.Entry<Integer, String> entry : map.entrySet()) {System.out.println("編號(hào)Key = " + entry.getKey() + ",品種Value = "+ entry.getValue());}}/*** * * @Title: petsInfo* * @Description: 獲取容器注入的List,遍歷輸出* * * @return: void*/public void petsInfo_List() {for (int i = 0; i < pets.getPetList().size(); i++) {System.out.println("PetShop has " + pets.getPetList().get(i));}}/*** * * @Title: petsInfo* * @Description: 獲取注入的set,遍歷輸出* * * @return: void*/public void petsInfo_Set() {Set<String> set = pets.getPetSet();Iterator<String> it = set.iterator();while (it.hasNext()) {System.out.println("PetShop has " + it.next());}}/*** * * @Title: petsInfo* * @Description: 獲取容器注入的Properties,遍歷輸出* * * @return: void*/public void petsInfo_Properties() {Map<Object, Object> map = pets.getPetProperties();for (Map.Entry<Object, Object> entry : map.entrySet()) {System.out.println("編號(hào)Key = " + entry.getKey() + ",品種Value = "+ entry.getValue());}} }POJO類(lèi)
package com.xgj.ioc.inject.construct.utilSchema;import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set;public class Pets {private Map<Integer, String> petMap;private List<String> petList;private Set<String> petSet;private Properties petProperties;public Properties getPetProperties() {return petProperties;}public void setPetProperties(Properties petProperties) {this.petProperties = petProperties;}public Set<String> getPetSet() {return petSet;}public void setPetSet(Set<String> petSet) {this.petSet = petSet;}public List<String> getPetList() {return petList;}public void setPetList(List<String> petList) {this.petList = petList;}public Map<Integer, String> getPetMap() {return petMap;}public void setPetMap(Map<Integer, String> petMap) {this.petMap = petMap;}}配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"><bean id="petShop" class="com.xgj.ioc.inject.construct.utilSchema.PetShop"><property name="pets" ref="pets" /></bean><!-- 第一種寫(xiě)法 ,通過(guò)ref引用,此時(shí)需要在 uitl-map中聲明id 推薦這種寫(xiě)法 --><!-- property 中的name對(duì)應(yīng)類(lèi)中屬性, ref對(duì)應(yīng)uitl的id --><bean id="pets" class="com.xgj.ioc.inject.construct.utilSchema.Pets"><property name="petMap" ref="petMap"/><property name="petList" ref="petList"/><property name="petSet" ref="petSet"/><property name="petProperties" ref="petProperties"/></bean><!-- 配置一個(gè)Map集合 --><util:map id="petMap" map-class="java.util.HashMap"><entry key="101" value="dog" /><entry key="103" value="wolf" /><entry key="105" value="bird" /></util:map><!-- 配置一個(gè)List集合 --><util:list id="petList" list-class="java.util.ArrayList" value-type="java.lang.String"><value>DOG</value><value>CAT</value><value>BIRD</value></util:list><!-- util配置一個(gè)Set集合 --><util:set id="petSet" set-class="java.util.HashSet" value-type="java.lang.String"><value>牛</value><value>馬</value><value>狗</value></util:set><!--配置一個(gè) Properties--><util:properties id="petProperties"><prop key="151">PIG</prop><prop key="153">PINGUIN</prop></util:properties><!-- 第二種寫(xiě)法,嵌入內(nèi)部 <bean id="pets" class="com.xgj.ioc.inject.construct.utilSchema.Pets"><property name="petMap"><util:map map-class="java.util.HashMap" key-type="java.lang.Integer" value-type="java.lang.String"> 可以通過(guò)map-class顯示指定Map的實(shí)現(xiàn)類(lèi)<entry key="101" value="dog" /><entry key="103" value="wolf" /><entry key="105" value="bird" /></util:map></property></bean>--></beans>測(cè)試類(lèi)
package com.xgj.ioc.inject.construct.utilSchema;import org.apache.log4j.Logger; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class UtilSchemaTest {static Logger logger = Logger.getLogger(UtilSchemaTest.class);public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:com/xgj/ioc/inject/construct/utilSchema/beans.xml");PetShop petShop = ctx.getBean("petShop", PetShop.class);logger.info("---------------Map--------------------");petShop.petsInfo_Map();logger.info("---------------List--------------------");petShop.petsInfo_List();logger.info("---------------Set--------------------");petShop.petsInfo_Set();logger.info("---------------Properties--------------------");petShop.petsInfo_Properties();} }運(yùn)行結(jié)果:
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專(zhuān)家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的Spring-注入参数详解-[通过util命名空间简化集合类型的配置]的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Spring-注入参数详解-[简化配置方
- 下一篇: Spring-基于注解的配置[01定义B