Spring-注入参数详解-[通过util命名空间简化集合类型的配置]

时间:2022-02-21 13:46:15

概述

如果希望配置一个集合类型的Bean,而非一个集合类型的属性,则可以通过util命名空间进行配置。

在spring的配置文件中util命名空间类似于java.util包类对应,util命名空间提供了集合相关的配置,在使用命名空间前要导入util命名空间。

步骤

声明命名空间和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会自动关联到最新版本 -->
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">

.....

</beans>

配置Bean

配置一个Map

POJO类

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("编号Key = " + entry.getKey() + ",品种Value = "
+ entry.getValue());

}
}

}

POJO类

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.xsd

http://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>

<!-- 第一种写法 ,通过ref引用,此时需要在 uitl-map中声明id 推荐这种写法

<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>

-->

<!-- 第二种写法,嵌入内部 -->
<bean id="pets" class="com.xgj.ioc.inject.construct.utilSchema.Pets">
<property name="petMap">
<util:map map-class="java.util.HashMap"><!-- 可以通过map-class显示指定Map的实现类 -->
<entry key="101" value="dog" />
<entry key="103" value="wolf" />
<entry key="105" value="bird" />
</util:map>
</property>
</bean>

</beans>

测试类

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();

}
}

运行结果

Spring-注入参数详解-[通过util命名空间简化集合类型的配置]

<util:map>支持key-type和value-type属性,指定Map的键和值的类型

<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属性,指定集合中的值的类型

配置一个Set

mapsetlistproperties实例汇总 代码


配置一个List

mapsetlistproperties实例汇总 代码


配置一个Properties

mapsetlistproperties实例汇总 代码


Map、Set、List、Properties实例汇总

POJO类

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("编号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("编号Key = " + entry.getKey() + ",品种Value = "
+ entry.getValue());

}
}
}

POJO类

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.xsd

http://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>

<!-- 第一种写法 ,通过ref引用,此时需要在 uitl-map中声明id 推荐这种写法 -->
<!-- property 中的name对应类中属性, ref对应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>


<!-- 配置一个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>

<!-- 配置一个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配置一个Set集合 -->
<util:set id="petSet" set-class="java.util.HashSet" value-type="java.lang.String">
<value></value>
<value></value>
<value></value>
</util:set>


<!--配置一个 Properties-->
<util:properties id="petProperties">
<prop key="151">PIG</prop>
<prop key="153">PINGUIN</prop>
</util:properties>




<!-- 第二种写法,嵌入内部
<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"> 可以通过map-class显示指定Map的实现类
<entry key="101" value="dog" />
<entry key="103" value="wolf" />
<entry key="105" value="bird" />
</util:map>
</property>
</bean>

-->



</beans>

测试类

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();

}
}

运行结果:

Spring-注入参数详解-[通过util命名空间简化集合类型的配置]