Spring学习(三)几种集合属性的注入方式
1、前言
众所周知、java中不只有八大简单类型、还有一些集合类型、本文围绕集合类型的注入做一个总结。
2、项目骨架

3、过程
1、创建实体类AllCollectionType
package com.feng.entity;
import java.util.*;
public class AllCollectionType {
    private List<String> listElement;
    private String[] arrayElement;
    private Set<String> setElement;
    private Map<String,String> mapElement;
    private Properties propsElement;
    @Override
    public String toString() {
        return "AllCollectionType{" +
                "listElement=" + listElement +
                ", arrayElement=" + Arrays.toString(arrayElement) +
                ", setElement=" + setElement +
                ", mapElement=" + mapElement +
                ", propsElement=" + propsElement +
                '}';
    }
    public List<String> getListElement() {
        return listElement;
    }
    public void setListElement(List<String> listElement) {
        this.listElement = listElement;
    }
    public String[] getArrayElement() {
        return arrayElement;
    }
    public void setArrayElement(String[] arrayElement) {
        this.arrayElement = arrayElement;
    }
    public Set<String> getSetElement() {
        return setElement;
    }
    public void setSetElement(Set<String> setElement) {
        this.setElement = setElement;
    }
    public Map<String, String> getMapElement() {
        return mapElement;
    }
    public void setMapElement(Map<String, String> mapElement) {
        this.mapElement = mapElement;
    }
    public Properties getPropsElement() {
        return propsElement;
    }
    public void setPropsElement(Properties propsElement) {
        this.propsElement = propsElement;
    }
}
2、配置applicationContext.xml
<?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="collectionDemo" class="com.feng.entity.AllCollectionType">
        <property name="listElement">
            <list>
                <value>足球</value>
                <value>蓝球</value>
                <value>乒乓球</value>
            </list>
        </property>
        <property name="arrayElement">
            <array>
                <value>足球1</value>
                <value>蓝球1</value>
                <value>乒乓球1</value>
            </array>
        </property>
        <property name="setElement">
            <set>
                <value>足球2</value>
                <value>蓝球2</value>
                <value>乒乓球2</value>
            </set>
        </property>
        <property name="mapElement">
            <map>
                <entry>
                    <key>
                        <value>foot3</value>
                    </key>
                    <value>足球3</value>
                </entry>
                <entry>
                    <key>
                        <value>basket3</value>
                    </key>
                    <value>蓝球3</value>
                </entry>
                <entry>
                    <key>
                        <value>pp3</value>
                    </key>
                    <value>乒乓球3</value>
                </entry>
            </map>
        </property>
        <property name="propsElement">
            <props>
                <prop key="foot4">足球4</prop>
                <prop key="basket4">蓝球4</prop>
                <prop key="pp4">乒乓球4</prop>
            </props>
        </property>
    </bean>
</beans>
3、编写测试类
package com.feng.test;
import com.feng.entity.AllCollectionType;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
    public static void collectionDemo() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        AllCollectionType type = (AllCollectionType)context.getBean("collectionDemo");
        System.out.println(type);
    }
    public static void main(String[] args) {
        collectionDemo();
    }
}
4、运行结果


4、总结
本文简单讲述了四种集合属性的注入方式、对之前的注入方式进行了一次补充。
Spring学习(三)几种集合属性的注入方式的更多相关文章
- day38 14-Spring的Bean的属性的注入:集合属性的注入
		集合:List.Set.Map. package cn.itcast.spring3.demo6; import java.util.List; import java.util.Map; impor ... 
- 【Spring学习笔记-MVC-4】SpringMVC返回Json数据-方式2
		<Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ... 
- 【Spring学习笔记-MVC-3】SpringMVC返回Json数据-方式1
		<Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ... 
- Spring学习(三)--高级装配
		一.Spring profile 在开发软件的时候,有一个很大的挑战就是将应用程序从一个环境迁 移到另外一个环境.开发阶段中,某些环境相关做法可能并不适合迁 移到生产环境中,甚至即便迁移过去也无法正常 ... 
- Spring学习(三)
		DI (Dependency Injection) 1.依赖注入,组件之间的依赖关系由容器在运行期间决定.Ioc容器注入应用程序某个对象,它所需要的外部资源(包括对象,资源,常量数据). birthd ... 
- Spring4学习笔记2-配置集合属性
		1 可使用<list> <map> <set>等来配置集合属性 2 List <!-- 配置List属性 --> <bean id="p ... 
- Spring学习笔记--自动装配Bean属性
		Spring提供了四种类型的自动装配策略: byName – 把与Bean的属性具有相同名字(或者ID)的其他Bean自动装配到Bean的对应属性中. byType – 把与Bean的属性具有相同类型 ... 
- Spring学习三----------注入方式
		© 版权声明:本文为博主原创文章,转载请注明出处 Spring注入方式 本篇博客只讲最常用的两种注入方式:设值注入和构造器注入.代码为完整代码,复制即可使用. 1.目录结构 2.pom.xml < ... 
- spring学习三:Spring的Aop、代理
		ref:https://mp.weixin.qq.com/s/J77asUvw8FcnF-6YlX6AAw AOP相关术语: Joinpoint(连接点):类里面可以被增强的方法,这些方法称为连 ... 
随机推荐
- 【shell】真正解决syntax error:unexpected end of file?
			今天写了个较长的shell脚本,结构嵌套比较多,最后运行时,出现了syntax error: unexpected end of file的错误. 这个之前碰到过,经常在win系统转移脚本文件到uni ... 
- PAML 选择压力的计算
			简介 PAML(Phylogenetic Analysis by Maximum Likelihood)是伦敦大学的杨子恒(Yang Ziheng)教 授开发的一套基于最大似然估计来对蛋白质和核酸序列 ... 
- Linux学习——Gdb基本调试方法&&多线程调试
			1.Gdb的基本调试 示例代码 //e.c #include <stdio.h> void debug(char *str) { printf("debug info :%s\n ... 
- hadoop基础题
			转自:http://blog.csdn.net/pelick/article/details/8299482 //Hadoop基础 Doug Cutting所创立的项目的名称都受到其家人的启发,以下项 ... 
- 5.Maximum Product Subarray-Leetcode
			f(j+1)为以下标j结尾的连续子序列最大乘积值(1) 状态转移方程如何表示呢: 这里我们知道A[j]可能为正数(或0)或负数,那么当A[j]为正数,期望前j个乘积为正数,若为负数,则期望前面的为负数 ... 
- 关于SQL中Union和Join的用法
			转自帘卷西风的专栏(http://blog.csdn.net/ljxfblog) https://blog.csdn.net/ljxfblog/article/details/52066006 Uni ... 
- tensorboard 拒绝连接无法打开相应页面
			启动tensorboard时没有报错,但打开页面却拒绝连接. 解决方法:tensorboard --logdir=TEC4FN --host=127.0.0.1 在命令最后添加 --host=127. ... 
- acquire
			An acquired taste is an appreciation for something unlikely to be enjoyed by a person who has not ha ... 
- day02 web主流框架
			day02 web主流框架 今日内容概要 手写简易版本web框架 借助于wsgiref模块 动静态网页 jinja2模板语法 前端.web框架.数据库三种结合 Python主流web框架 django ... 
- day01 前端bootstrap框架
			day01 django框架之bootstrap框架 今日内容概要 前端框架之bootstrap 该框架支持cv编写前端页面 利用socket模块编写一个简易版本的web框架 利用wsgiref模块编 ... 
