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

举报
小工匠 发表于 2021/09/10 00:31:51 2021/09/10
1.6k+ 0 0
【摘要】 概述步骤 声明命名空间和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>

  
 

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

    }
}
  
 

运行结果

这里写图片描述

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

    }
}
  
 

运行结果:

这里写图片描述

文章来源: artisan.blog.csdn.net,作者:小小工匠,版权归原作者所有,如需转载,请联系作者。

原文链接:artisan.blog.csdn.net/article/details/75639178

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。