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

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

概述


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

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


常用集合


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

20170721124335842.jpg

Set


20170721124417019.jpg


实例

POJO

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<Object> set = pets.getSet();
        Iterator<Object> 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;
    }
}


配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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>bear</value>
                <value>dog</value>
                <value>cat</value>
                <value>snake</value>
                <value>pig</value>
            </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();
    }
}


运行结果

20170721124532765.jpg


List

List属性既可以通过注入字符串,也可以通过注入容器中其他的Bean

20170721124010149.jpg


实例

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


配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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>dog</value>
                <value>cat</value>
                <value>bear</value>
                <value>rabbit</value>
                <value>bird</value>
            </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();
    }
}


运行结果


20170721124229854.jpg


假设一个属性类型可以通过字符串字面值进行配置,那么该类型对应的数组类型的属性比入String[],int[]也可以采用<list>方式进行配置.


Map


20170721124616901.jpg


实例

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<Object, Object> map = pets.getMap();
        for (Map.Entry<Object, Object> 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;
    }
}


配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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>135</value>
                    </key>
                    <value>cat</value>
                </entry>
                <entry>
                    <key>
                        <value>137</value>
                    </key>
                    <value>bird</value>
                </entry>
                <entry>
                    <key>
                        <value>139</value>
                    </key>
                    <value>dog</value>
                </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();
    }
}


运行结果:


20170721124758785.jpg

如果Map元素的key和value都是对象,这可以采用以下配置方式

<entry>
    <key>
        <ref bean="keyBbean"/>
    </key>
    <ref bean="valueBean">
</entry>


Properties

20170721124827756.jpg


Propertites键值对可以看做Map类型的特例, Map的键值对可以是任何类型,Properties的键值对只能是字符串,因此配置简单一些,注意Value的配置没有<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<Object, Object> map = pets.getProperties();
        for (Map.Entry<Object, Object> 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;
    }
}


配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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">cat</prop>
                <prop key="103">dog</prop>
                <prop key="105">bird</prop>
            </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();
    }
}


运行结果

20170721124932767.jpg


强类型集合

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<Integer, String> map = pets.getMap();
        for (Map.Entry<Integer, String> 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;
    }
}

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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>
                        <!-- 为Integer提供值,spring在设置值时,会转换为定义的Integer类型 -->
                        <value>111</value>
                    </key>
                    <value>cat</value>
                </entry>
                <entry>
                    <key>
                        <value>113</value>
                    </key>
                    <value>bird</value>
                </entry>
                <entry>
                    <key>
                        <value>115</value>
                    </key>
                    <value>dog</value>
                </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();
    }
}


运行结果


20170721125813259.jpg


集合合并


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<Integer, String> map = pets.getMap();
        for (Map.Entry<Integer, String> 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;
    }
}


配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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 abstract="true" -->
    <bean id="parentPets"   abstract="true" class="com.xgj.ioc.inject.construct.jihe.merge.Pets">
        <property name="map">
            <map>
                <entry>
                    <key>
                        <!-- 为Integer提供值,spring在设置值时,会转换为定义的Integer类型 -->
                        <value>111</value>
                    </key>
                    <value>cat</value>
                </entry>
                <entry>
                    <key>
                        <value>113</value>
                    </key>
                    <value>bird</value>
                </entry>
                <entry>
                    <key>
                        <value>115</value>
                    </key>
                    <value>dog</value>
                </entry>
            </map>
        </property>
    </bean>
    <bean id="pets" parent="parentPets"> <!-- 指定父Bean -->
        <property name="map">
            <!-- 设置merge="true" 和父bean的同名集合属性合并 -->
            <map merge="true"> 
                <entry>
                    <key>
                        <value>117</value>
                    </key>
                    <value>monkey</value>
                </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();
    }
}


运行结果


20170721130109014.jpg


相关文章
|
Java 测试技术 微服务
微服务——SpringBoot使用归纳——Spring Boot中的项目属性配置——少量配置信息的情形
本课主要讲解Spring Boot项目中的属性配置方法。在实际开发中,测试与生产环境的配置往往不同,因此不应将配置信息硬编码在代码中,而应使用配置文件管理,如`application.yml`。例如,在微服务架构下,可通过配置文件设置调用其他服务的地址(如订单服务端口8002),并利用`@Value`注解在代码中读取这些配置值。这种方式使项目更灵活,便于后续修改和维护。
320 0
|
Java Spring
在使用Spring的`@Value`注解注入属性值时,有一些特殊字符需要注意
【10月更文挑战第9天】在使用Spring的`@Value`注解注入属性值时,需注意一些特殊字符的正确处理方法,包括空格、引号、反斜杠、新行、制表符、逗号、大括号、$、百分号及其他特殊字符。通过适当包裹或转义,确保这些字符能被正确解析和注入。
933 3
|
XML Java 测试技术
Spring AOP—通知类型 和 切入点表达式 万字详解(通俗易懂)
Spring 第五节 AOP——切入点表达式 万字详解!
1089 25
|
Java Spring
一键注入 Spring 成员变量,顺序编程
介绍了一款针对Spring框架开发的插件,旨在解决开发中频繁滚动查找成员变量注入位置的问题。通过一键操作(如Ctrl+1),该插件可自动在类顶部添加`@Autowired`注解及其成员变量声明,同时保持光标位置不变,有效提升开发效率和代码编写流畅度。适用于IntelliJ IDEA 2023及以上版本。
235 2
一键注入 Spring 成员变量,顺序编程
|
Java 数据库 微服务
微服务——SpringBoot使用归纳——Spring Boot中的项目属性配置——指定项目配置文件
在实际项目中,开发环境和生产环境的配置往往不同。为简化配置切换,可通过创建 `application-dev.yml` 和 `application-pro.yml` 分别管理开发与生产环境配置,如设置不同端口(8001/8002)。在 `application.yml` 中使用 `spring.profiles.active` 指定加载的配置文件,实现环境快速切换。本节还介绍了通过配置类读取参数的方法,适用于微服务场景,提升代码可维护性。课程源码可从 [Gitee](https://gitee.com/eson15/springboot_study) 下载。
633 0
|
Java 微服务 Spring
微服务——SpringBoot使用归纳——Spring Boot中的项目属性配置——少量配置信息的情形
在微服务架构中,随着业务复杂度增加,项目可能需要调用多个微服务。为避免使用`@Value`注解逐一引入配置的繁琐,可通过定义配置类(如`MicroServiceUrl`)并结合`@ConfigurationProperties`注解实现批量管理。此方法需在配置文件中设置微服务地址(如订单、用户、购物车服务),并通过`@Component`将配置类纳入Spring容器。最后,在Controller中通过`@Resource`注入配置类即可便捷使用,提升代码可维护性。
270 0
|
Java 开发者 Spring
Spring高手之路24——事务类型及传播行为实战指南
本篇文章深入探讨了Spring中的事务管理,特别是事务传播行为(如REQUIRES_NEW和NESTED)的应用与区别。通过详实的示例和优化的时序图,全面解析如何在实际项目中使用这些高级事务控制技巧,以提升开发者的Spring事务管理能力。
433 1
Spring高手之路24——事务类型及传播行为实战指南
|
安全 算法 Java
强大!基于Spring Boot 3.3 六种策略识别上传文件类型
【10月更文挑战第1天】在Web开发中,文件上传是一个常见的功能需求。然而,如何确保上传的文件类型符合预期,防止恶意文件入侵,是开发者必须面对的挑战。本文将围绕“基于Spring Boot 3.3 六种策略识别上传文件类型”这一主题,分享一些工作学习中的技术干货,帮助大家提升文件上传的安全性和效率。
894 0
|
XML Java 数据格式
Spring【依赖注入】就是这么简单(二)
在Spring的第二篇中主要讲解了Spring Core模块的使用IOC容器创建对象的问题,Spring Core模块主要是解决对象的创建和对象之间的依赖关系,因此本博文主要讲解如何使用IOC容器来解决对象之间的依赖关系!
266 0
Spring【依赖注入】就是这么简单(二)
|
Java 测试技术 容器
Spring【依赖注入】就是这么简单
前言 在Spring的第二篇中主要讲解了Spring Core模块的使用IOC容器创建对象的问题,Spring Core模块主要是解决对象的创建和对象之间的依赖关系,因此本博文主要讲解如何使用IOC容器来解决对象之间的依赖关系! 回顾以前对象依赖 我们来看一下我们以前关于对象依赖,是怎么的历程 直接new对象 在最开始,我们是直接new对象给serice的userDao属性赋值.
1309 0

热门文章

最新文章