Spring-注入参数详解-[通过util命名空间简化集合类型的配置]
【摘要】
概述步骤
声明命名空间和schema配置Bean
配置一个Map配置一个Set配置一个List配置一个PropertiesMapSetListProperties实例汇总...
概述
如果希望配置一个集合类型的Bean,而非一个集合类型的属性,则可以通过util命名空间进行配置。
在spring的配置文件中util命名空间类似于java.util包类对应,util命名空间提供了集合相关的配置,在使用命名空间前要导入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会自动关联到最新版本 -->
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
.....
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
配置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());
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
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;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
配置文件
<?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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
测试类
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();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
运行结果
<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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
<util:list>
和<util:set>
支持value-type属性,指定集合中的值的类型
配置一个Set
配置一个List
配置一个Properties
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());
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
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;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
配置文件
<?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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
测试类
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();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
运行结果:
文章来源: artisan.blog.csdn.net,作者:小小工匠,版权归原作者所有,如需转载,请联系作者。
原文链接:artisan.blog.csdn.net/article/details/75639178
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)