前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring-注入参数详解-[通过util命名空间简化集合类型的配置]

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

作者头像
小小工匠
发布2021-08-16 16:28:30
4310
发布2021-08-16 16:28:30
举报
文章被收录于专栏:小工匠聊架构

概述

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

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

步骤

代码已托管到Github—> https://github.com/yangshangwei/SpringMaster

声明命名空间和schema

代码语言:javascript
复制
<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">

.....

beans>

配置Bean

配置一个Map

POJO类

代码语言:javascript
复制
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 map = pets.getPetMap();

        for (Map.Entry entry : map.entrySet()) {

            System.out.println("编号Key = " + entry.getKey() + ",品种Value = "
                    + entry.getValue());

        }
    }

}

POJO类

代码语言:javascript
复制
package com.xgj.ioc.inject.construct.utilSchema;

import java.util.Map;

public class Pets {

    private Map petMap;

    public Map getPetMap() {
        return petMap;
    }

    public void setPetMap(Map petMap) {
        this.petMap = petMap;
    }

}

配置文件

代码语言:javascript
复制
<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>


    
    <bean id="pets" class="com.xgj.ioc.inject.construct.utilSchema.Pets">
        <property name="petMap">
            <util:map  map-class="java.util.HashMap">
                <entry key="101" value="dog" />
                <entry key="103" value="wolf" />
                <entry key="105" value="bird" />
            util:map>
        property>
    bean>

beans>

测试类

代码语言:javascript
复制
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();

    }
}

运行结果

这里写图片描述
这里写图片描述

支持key-type和value-type属性,指定Map的键和值的类型

代码语言:javascript
复制
class="java.util.HashMap" 
            key-type="java.lang.Integer" 
            value-type="java.lang.String">
                "101" value="dog" />
                "103" value="wolf" />
                "105" value="bird" />

和支持value-type属性,指定集合中的值的类型

配置一个Set

mapsetlistproperties实例汇总 代码


配置一个List

mapsetlistproperties实例汇总 代码


配置一个Properties

mapsetlistproperties实例汇总 代码


Map、Set、List、Properties实例汇总

POJO类

代码语言:javascript
复制
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 map = pets.getPetMap();

        for (Map.Entry 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 set = pets.getPetSet();
        Iterator it = set.iterator();
        while (it.hasNext()) {
            System.out.println("PetShop has " + it.next());
        }
    }

    /**
     * 
     * 
     * @Title: petsInfo
     * 
     * @Description: 获取容器注入的Properties,遍历输出
     * 
     * 
     * @return: void
     */
    public void petsInfo_Properties() {

        Map map = pets.getPetProperties();

        for (Map.Entry entry : map.entrySet()) {

            System.out.println("编号Key = " + entry.getKey() + ",品种Value = "
                    + entry.getValue());

        }
    }
}

POJO类

代码语言:javascript
复制
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 petMap;

    private List petList;

    private Set petSet;

    private Properties petProperties;

    public Properties getPetProperties() {
        return petProperties;
    }

    public void setPetProperties(Properties petProperties) {
        this.petProperties = petProperties;
    }

    public Set getPetSet() {
        return petSet;
    }

    public void setPetSet(Set petSet) {
        this.petSet = petSet;
    }

    public List getPetList() {
        return petList;
    }

    public void setPetList(List petList) {
        this.petList = petList;
    }

    public Map getPetMap() {
        return petMap;
    }

    public void setPetMap(Map petMap) {
        this.petMap = petMap;
    }

}

配置文件

代码语言:javascript
复制
<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>

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


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

    
    <util:list id="petList" list-class="java.util.ArrayList" value-type="java.lang.String">
        <value>DOGvalue>
        <value>CATvalue>
        <value>BIRDvalue>
    util:list>

     
    <util:set id="petSet" set-class="java.util.HashSet" value-type="java.lang.String">
        <value>牛value>
        <value>马value>
        <value>狗value>
    util:set>


    
    <util:properties id="petProperties">
            <prop key="151">PIGprop>
            <prop key="153">PINGUINprop>
    util:properties>




    


beans>

测试类

代码语言:javascript
复制
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();

    }
}

运行结果:

这里写图片描述
这里写图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017/07/21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 概述
  • 步骤
    • 声明命名空间和schema
      • 配置Bean
        • 配置一个Map
        • 配置一个Set
        • 配置一个List
        • 配置一个Properties
        • Map、Set、List、Properties实例汇总
    相关产品与服务
    容器服务
    腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档