前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring-注入参数详解-[集合类型属性]

Spring-注入参数详解-[集合类型属性]

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

概述

java.util包中的集合类型是最常用的结构数据类型,主要包括List、Set、Map、Properties。

Spring为这些集合类型属性提供了专属的配置标签

常用集合

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

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

Set

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

实例

POJO类

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

import java.util.Iterator;
import java.util.Set;

public class PetShop {

    private Pets pets;

    public void setPets(Pets pets) {
        this.pets = pets;
    }

    /**
     * 
     * 
     * @Title: petsInfo
     * 
     * @Description: 获取注入的set,遍历输出
     * 
     * 
     * @return: void
     */
    public void petsInfo() {
        Set set = pets.getSet();
        Iterator it = set.iterator();
        while (it.hasNext()) {
            System.out.println("PetShop has " + it.next());
        }
    }
}
 
POJO类 
package com.xgj.ioc.inject.construct.jihe.set;

import java.util.HashSet;
import java.util.Set;

public class Pets {

    private Set set = new HashSet();

    public Set getSet() {
        return set;
    }

    public void setSet(Set set) {
        this.set = set;
    }

}
 
配置文件 

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="pets" class="com.xgj.ioc.inject.construct.jihe.set.Pets">
        <property name="set">
            <set>
                <value>bearvalue>
                <value>dogvalue>
                <value>catvalue>
                <value>snakevalue>
                <value>pigvalue>
            set>
        property>
    bean>

    <bean id="petShop" class="com.xgj.ioc.inject.construct.jihe.set.PetShop">
        <property name="pets" ref="pets"/>
    bean>

beans>
 
测试类 
package com.xgj.ioc.inject.construct.jihe.set;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class InjectSetTest {
    public static void main(String[] args) {

        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "classpath:com/xgj/ioc/inject/construct/jihe/set/beans.xml");

        PetShop shop = ctx.getBean("petShop", PetShop.class);

        shop.petsInfo();

    }
}
 
运行结果 
 
 
List 
List属性既可以通过注入字符串,也可以通过注入容器中其他的Bean 
 
实例 
POJO类 
package com.xgj.ioc.inject.construct.jihe.list;

public class PetShop {

    private Pets pets;

    public void setPets(Pets pets) {
        this.pets = pets;
    }

    /**
     * 
     * 
     * @Title: petsInfo
     * 
     * @Description: 获取容器注入的List,遍历输出
     * 
     * 
     * @return: void
     */
    public void petsInfo() {
        for (int i = 0; i < pets.getPetsList().size(); i++) {
            System.out.println("PetShop has " + pets.getPetsList().get(i));
        }
    }

}
 
POJO类 
package com.xgj.ioc.inject.construct.jihe.list;

import java.util.ArrayList;
import java.util.List;

public class Pets {

    private List petsList = new ArrayList();

    public List getPetsList() {
        return petsList;
    }

    public void setPetsList(List petsList) {
        this.petsList = petsList;
    }

}
 
配置文件 

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="pets" class="com.xgj.ioc.inject.construct.jihe.list.Pets">
        <property name="petsList">
            <list>
                <value>dogvalue>
                <value>catvalue>
                <value>bearvalue>
                <value>rabbitvalue>
                <value>birdvalue>
            list>
        property>
    bean>

    <bean id="petShop" class="com.xgj.ioc.inject.construct.jihe.list.PetShop">
        <property name="pets">
            <ref bean="pets"/>
        property>
    bean>

beans>
 
测试类 
package com.xgj.ioc.inject.construct.jihe.list;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class InjectListTest {

    public static void main(String[] args) {

        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "classpath:com/xgj/ioc/inject/construct/jihe/list/beans.xml");

        PetShop shop = ctx.getBean("petShop", PetShop.class);

        shop.petsInfo();

    }
}
 
运行结果 
 
假设一个属性类型可以通过字符串字面值进行配置,那么该类型对应的数组类型的属性比入String[],int[]也可以采用方式进行配置. 
 
Map 
 
实例 
POJO类 
package com.xgj.ioc.inject.construct.jihe.map;

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 = pets.getMap();

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

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

        }
    }
}
 
POJO类 
package com.xgj.ioc.inject.construct.jihe.map;

import java.util.HashMap;
import java.util.Map;

public class Pets {

    private Map map = new HashMap();

    public Map getMap() {
        return map;
    }

    public void setMap(Map map) {
        this.map = map;
    }

}
 
配置文件 

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="pets" class="com.xgj.ioc.inject.construct.jihe.map.Pets">
        <property name="map">
            <map>
                <entry>
                    <key>
                        <value>135value>
                    key>
                    <value>catvalue>
                entry>
                <entry>
                    <key>
                        <value>137value>
                    key>
                    <value>birdvalue>
                entry>
                <entry>
                    <key>
                        <value>139value>
                    key>
                    <value>dogvalue>
                entry>
            map>
        property>
    bean>

    <bean id="petShop" class="com.xgj.ioc.inject.construct.jihe.map.PetShop">
        <property name="pets" ref="pets" />
    bean>

beans>
 
测试类 
package com.xgj.ioc.inject.construct.jihe.map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class InjectMapTest {

    public static void main(String[] args) {

        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "classpath:com/xgj/ioc/inject/construct/jihe/map/beans.xml");

        PetShop shop = ctx.getBean("petShop", PetShop.class);

        shop.petsInfo();

    }
}
 
运行结果: 
 
如果Map元素的key和value都是对象,这可以采用以下配置方式 
<entry>
    <key>
        <ref bean="keyBbean"/>
    key>
    <ref bean="valueBean">
entry> 
 
Properties 
 
Propertites键值对可以看做Map类型的特例, Map的键值对可以是任何类型,Properties的键值对只能是字符串,因此配置简单一些,注意Value的配置没有子元素标签 
实例 
POJO类 
package com.xgj.ioc.inject.construct.jihe.properties;

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 = pets.getProperties();

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

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

        }
    }
}
 
POJO类 
package com.xgj.ioc.inject.construct.jihe.properties;

import java.util.Properties;

public class Pets {

    private Properties properties;

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

}
 
配置文件 

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="pets" class="com.xgj.ioc.inject.construct.jihe.properties.Pets">
        <property name="properties">
            <props>
                <prop key="101">catprop>
                <prop key="103">dogprop>
                <prop key="105">birdprop>
            props>
        property>
    bean>

    <bean id="petShop" class="com.xgj.ioc.inject.construct.jihe.properties.PetShop">
        <property name="pets" ref="pets" />
    bean>

beans>
 
测试类 
package com.xgj.ioc.inject.construct.jihe.properties;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class InjectPropertiesTest {

    public static void main(String[] args) {

        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "classpath:com/xgj/ioc/inject/construct/jihe/properties/beans.xml");

        PetShop shop = ctx.getBean("petShop", PetShop.class);

        shop.petsInfo();

    }
}
 
运行结果 
 
 
强类型集合 
Java5.0提供了强类型集合的新功能,允许为集合元素指定特定类型。 
实例 
POJO类 
package com.xgj.ioc.inject.construct.jihe.strongType;

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 = pets.getMap();

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

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

        }
    }
}
 
POJO类 
package com.xgj.ioc.inject.construct.jihe.strongType;

import java.util.HashMap;
import java.util.Map;

public class Pets {

    private Map<Integer, String> map = new HashMap<Integer, String>();

    public Map<Integer, String> getMap() {
        return map;
    }

    public void setMap(Map<Integer, String> map) {
        this.map = map;
    }

}
 
配置文件 

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="pets" class="com.xgj.ioc.inject.construct.jihe.strongType.Pets">
        <property name="map">
            <map>
                <entry>
                    <key>
                        
                        <value>111value>
                    key>
                    <value>catvalue>
                entry>
                <entry>
                    <key>
                        <value>113value>
                    key>
                    <value>birdvalue>
                entry>
                <entry>
                    <key>
                        <value>115value>
                    key>
                    <value>dogvalue>
                entry>
            map>
        property>
    bean>

    <bean id="petShop" class="com.xgj.ioc.inject.construct.jihe.strongType.PetShop">
        <property name="pets" ref="pets" />
    bean>

beans>
 
测试类 
package com.xgj.ioc.inject.construct.jihe.strongType;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class InjectStrongTypeTest {

    public static void main(String[] args) {

        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "classpath:com/xgj/ioc/inject/construct/jihe/strongType/beans.xml");

        PetShop shop = ctx.getBean("petShop", PetShop.class);

        shop.petsInfo();

    }
}
 
运行结果   
 
集合合并 
Spring支持集合合并的功能,允许子bean继承父bean的同名属性集合元素,并将子bean和父bean中配置的集合属性组合并起来作为最终的bean的属性值。 
实例 
POJO类 
package com.xgj.ioc.inject.construct.jihe.merge;

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 = pets.getMap();

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

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

        }
    }
}
 
POJO类 
package com.xgj.ioc.inject.construct.jihe.merge;

import java.util.HashMap;
import java.util.Map;

public class Pets {

    private Map<Integer, String> map = new HashMap<Integer, String>();

    public Map<Integer, String> getMap() {
        return map;
    }

    public void setMap(Map<Integer, String> map) {
        this.map = map;
    }

}
 
配置文件 

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    
    <bean id="parentPets"   abstract="true" class="com.xgj.ioc.inject.construct.jihe.merge.Pets">
        <property name="map">
            <map>
                <entry>
                    <key>
                        
                        <value>111value>
                    key>
                    <value>catvalue>
                entry>
                <entry>
                    <key>
                        <value>113value>
                    key>
                    <value>birdvalue>
                entry>
                <entry>
                    <key>
                        <value>115value>
                    key>
                    <value>dogvalue>
                entry>
            map>
        property>
    bean>

    <bean id="pets" parent="parentPets"> 
        <property name="map">
            
            <map merge="true"> 
                <entry>
                    <key>
                        <value>117value>
                    key>
                    <value>monkeyvalue>
                entry>
            map>
        property>
    bean>

    <bean id="petShop" class="com.xgj.ioc.inject.construct.jihe.merge.PetShop">
        <property name="pets" ref="pets" />
    bean>

beans>
 
测试类 
package com.xgj.ioc.inject.construct.jihe.merge;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class InjectStrongTypeTest {

    public static void main(String[] args) {

        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "classpath:com/xgj/ioc/inject/construct/jihe/merge/beans.xml");

        PetShop shop = ctx.getBean("petShop", PetShop.class);

        shop.petsInfo();

    }
}
 
运行结果 
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017/07/20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

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