Spring-注入参数详解-[集合类型属性]
- 概述
- 常用集合
- Set
- 实例
- List
- 实例
- Map
- 实例
- Properties
- 实例
- Set
- 强类型集合
- 实例
- 集合合并
- 实例
概述
java.util包中的集合类型是最常用的结构数据类型,主要包括List、Set、Map、Properties。
Spring为这些集合类型属性提供了专属的配置标签
常用集合
代码已托管到Github—> https://github.com/yangshangwei/SpringMaster
Set
实例
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();}
}
运行结果
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;}}
配置文件
<?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();}
}
运行结果
假设一个属性类型可以通过字符串字面值进行配置,那么该类型对应的数组类型的属性比入String[],int[]也可以采用<list>
方式进行配置.
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<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();}
}
运行结果:
如果Map元素的key和value都是对象,这可以采用以下配置方式
<entry><key><ref bean="keyBbean"/></key><ref bean="valueBean">
</entry>
Properties
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();}
}
运行结果
强类型集合
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();}
}
运行结果
集合合并
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();}
}
运行结果
Spring-注入参数详解-[集合类型属性]相关推荐
- Spring框架中提取list集合类型属性注入
提取list集合类型属性注入 前言 引入名称空间 编写`xml`配置文件 运行结果 前言 对于某一个类型属性通用性较高的情况下,可以单独的提取出来,给需要的bean进行引用. 有关类的创建见<S ...
- 《精通Spring4.X企业应用开发实战》读后感第五章(注入参数详解)
转载于:https://www.cnblogs.com/Michael2397/p/7954596.html
- spring依赖注入原理详解(转载)
spring依赖注入原理详解----转载 所谓依赖注入就是指:在运行期,由外部容器动态地将依赖对象注入到组件中.当spring容器启动后,spring容器初始化,创建并管理bean对象,以及销毁它.所 ...
- Spring的注入方式详解
[html] view plaincopy Spring的注入方式详解 Spring有三个注入方式,type1,type2,type3 type1 接口依赖 type2 setter/getter ...
- Java Spring Data Redis实战与配置参数详解 application.properties...
Redis作为开源分布式高并发缓存,使用范围非常广泛,主流互联网公司几乎都在使用. Java Spring Boot 2.0实战开发Redis缓存可以参考下面的步骤,Redis安装可以直接使用Linu ...
- [Spring5]IOC容器_Bean管理XML方式_注入集合类型属性
xml注入集合属性 1.注入数组类型属性 2.注入List集合类型属性 3.注入Map集合类型属性 (1)创建类,定义数组,list,map,set类型属性,生成对应set方法 package com ...
- IOC操作Bean管理XML方式(注入集合类型属性)
目录 IOC操作Bean管理XML方式(注入集合类型属性) (1)首先进行环境的搭建和准备 (2)创建一个类:用来完成集合类型属性注入 (3)在Spring 配置文件进行配置 (4)编写一个测试类进行 ...
- Java Spring Data Redis实战与配置参数详解 application.properties
Redis作为开源分布式高并发缓存,使用范围非常广泛,主流互联网公司几乎都在使用. Java Spring Boot 2.0实战开发Redis缓存可以参考下面的步骤,Redis安装可以直接使用Linu ...
- spring boot 实战 / 可执行war启动参数详解
概述 上一篇文章<spring boot 实战 / mvn spring-boot:run 参数详解>主要讲解了spring boot 项目基于maven插件启动过程中借助profil ...
最新文章
- 偷看日历?9款 APP 涉嫌过度获取权限
- 110道Python面试真题和面试简历资料(附链接)
- 如何评估深度学习模型效果?阿里工程师这么做
- NYOJ 658 字符串右移
- 牛客网JAVA专项联系共899题--个人记录学习经历
- Windows WSL Ubuntu下配置JDK环境变量
- 析构函数c+_了解C ++中的析构函数
- STM32F429HAL库定时器学习笔记
- Keil代码自动对齐 VS对齐功能
- 【图像处理】美图秀秀使用技巧:抠图、透明、改色、教程
- 最简单的c语言if程序,C语言简单实用的程序-if else 嵌套式的使用例子
- 【音频处理】使用 Adobe Audition 录制电脑内部声音 ( 启用电脑立体声混音 | Adobe Audition 中设置音频设备 | Adobe Audition 内录 )
- USDP使用笔记(二) 部署免费的USDP大数据双集群替代CDH CDP与HDP
- 罗格斯大学电子与计算机系排名,从罗格斯大学的专业排名看罗格斯大学的实力...
- 浪潮服务器dhcp修改ip,IP地址管理—DDI(DNS, DHCP, IPAM)解决方案
- vue2知识点:数据代理
- E. Arranging The Sheep
- 第一节 Python环境搭建
- Jackson的使用与创建Jackson工具类
- 找出1000以内的所有完数
热门文章
- java 包装类可以被继承_【Java基本功】一文了解Java中继承、封装、多态的细节...
- 概率统计笔记:分布的核
- R语言实战应用精讲50篇(五)-多重线性回归系列之模型拟合
- Flink从入门到精通100篇(十七)-Spark/Flink广播如何实现作业配置动态更新?
- 机器学习中的矩阵向量求导(二) 矩阵向量求导之定义法
- 使用网盘搭建svn服务器详解步骤
- 远程计算机未能及时反应,Win10无法打开软件提示“服务器没有及时响应或控制请求”怎么办...
- R语言-时间序列-arima模型-forecast、tseries包
- spring-session用mysql实现session共享实践
- 自底向上构建知识图谱全过程