Spring基础07——配置集合属性
1.集合属性
在Spring中可以通过一组内置的xml标签(例如<list>,<set>或<map>)来配置集合属性。
2.配置List集合
配置java.util.List类型的属性,需要指定<list>标签,在标签中包含一些元素,这些标签可以通过<value>指定简单的常量值,通过<ref>指定对其他Bean的引用,通过<bean>指定内置Bean定义,通过<null/>指定空元素,甚至可以内嵌其他集合。
<bean id="car3" class="com.wzy.collection.Car">
<constructor-arg value="Audi" index="0" type="java.lang.String"/>
<constructor-arg value="ShangHai" index="1" type="java.lang.String"/>
<constructor-arg value="400000" index="2" type="double"/>
</bean> <bean id="car4" class="com.wzy.collection.Car">
<constructor-arg value="baoma" index="0" type="java.lang.String"/>
<constructor-arg value="Guangzhou" index="1" type="java.lang.String"/>
<constructor-arg value="30000000" index="2" type="double"/>
</bean> <bean id="car5" class="com.wzy.collection.Car">
<constructor-arg value="benchi" index="0" type="java.lang.String"/>
<constructor-arg value="ShenZhen" index="1" type="java.lang.String"/>
<constructor-arg value="2000000" index="2" type="double"/>
</bean> <!--测试如何配置集合属性-->
<bean class="com.wzy.collection.Person" id="person3">
<property name="name" value="mike"/>
<property name="age" value="20"/>
<property name="cars">
<list>
<ref bean="car3"/>
<ref bean="car4"/>
<ref bean="car5"/>
</list>
</property>
</bean>
Java代码:
private static void testCollection1() {
//测试集合注入
//1.获取IOC容器ApplicationContext
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
//2.通过容器获取对象
Person person = (Person) ctx.getBean("person3");
//3.输出person
System.out.println(person);
}
输出结果:
3.数组
数组的定义和List一样,都使用<list>。
4.Set集合
配置java.util.Set需要使用<set>标签,定义元素的方法与List一样。
5.Map集合
java.util.Map通过<map>标签定义,<map>标签里可以使用多个<entry>作为子标签,每个条目包含一个键和一个值。必须在<Key>标签定义键值因为键和值的类型没有限制,所以可以自由地为它们指定<value>、<ref>、<bean>、<null>元素。可以将Map的键和值作为<entry>的属性定义:简单常量使用key和value来定义;bean引用通过key-ref和value-ref属性定义。
<bean id="newPerson" class="com.wzy.collection.NewPerson">
<property name="name" value="wzy"/>
<property name="age" value="24"/>
<property name="cars">
<!--使用map节点及map的entry子节点配置Map类型的成员变量-->
<map>
<entry key="1" value-ref="car3"/>
<entry key="2" value-ref="car4"/>
<entry key="3" value-ref="car5"/>
</map>
</property>
</bean>
Java代码:
private static void testCollection2() {
//1.获取IOC容器ApplicationContext
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
//2.通过容器获取对象
NewPerson newPerson = (NewPerson) ctx.getBean("newPerson");
//3.输出newPerson
System.out.println(newPerson);
}
输出结果:
6.properties
使用<props>定义java.utils.Properties, 该标签使用多个<prop>作为子标签,每个<prop>标签必须定义key属性。
spring.xml
<bean id="dataSource" class="com.wzy.collection.DataSource">
<property name="properties">
<props>
<prop key="url">jdbc:mysql://localhost:3306/db</prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
<prop key="driver">Driver</prop>
</props>
</property>
</bean>
Java Code:
private static void testCollection3() {
//1.获取IOC容器ApplicationContext
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
//2.通过容器获取对象
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
//3.输出dataSource
System.out.println(dataSource);
}
输出结果:
7.使用utility scheme定义集合
在某些情况下,我们需要多个bean同时引用同一个集合元素,那么我们就需要将这些集合元素抽取出来,这样集合元素就可以在不同Bean之间共享集合。将集合元素抽取出来,可以使用util schema里的集合标签定义独立的集合Bean,需要注意的是必须在<beans>根元素里添加util schema定义。
这里首先需要引入util的命名空间
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:p="http://www.springframework.org/schema/p"
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">
通过util标签定义公共的集合,通过属性注入中的ref属性可以直接进行引用
<!--配置独立的集合bean以供多个bean进行引用-->
<util:list id="cars">
<ref bean="car3"/>
<ref bean="car4"/>
<ref bean="car5"/>
</util:list> <bean id="person4" class="com.wzy.collection.Person">
<property name="name" value="wzy"/>
<property name="age" value="24"/>
<property name="cars" ref="cars"/>
</bean>
Spring基础07——配置集合属性的更多相关文章
- Spring -配置集合属性
1 可使用<list> <map> <set>等来配置集合属性2 List <!-- 配置List属性 --> <bean id="pe ...
- Spring Boot -- 外部配置的属性使用
Spring Boot允许使用propertities文件.yaml文件或者命令行参数作为外部配置. 命令行参数配置 Spring Boot可以基于jar包运行,打成jar包的程序可以直接通过下面的命 ...
- Spring入门第一课:Spring基础与配置Bean
1.入门 Spring是简化java开发的一个框架,其中IoC和AOP是Spring的两个重要核心.由于Spring是非侵入性的,通过Ioc容器来管理bean的生命周期,还整合了许多其他的优秀框架,所 ...
- Spring基础12——使用外部属性文件
1.使用外部属性文件 在配置文件里配置Bean时,有时需要在Bean的配置文件里引入系统部署的细节信息(例如:文件的路径.数据源配置信息等),而这些部署细节实际上需要和bean配置相分离,因为我们修改 ...
- Spring框架xml配置中属性ref与value的区别
1.spring批量扫描mybatis的mapper,使用value 2.spring管理mybatis的单个mapper,用的是ref 虽然引用的是同一个bean,但两个对象的属相类型明显不一样,一 ...
- [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring基础——在Spring Config 文件中配置 Bean
一.基于 XML 的 Bean 的配置——通过全类名(反射) <bean <!-- id: bean 的名称在IOC容器内必须是唯一的若没有指定,则自动的将全限定类名作为 改 bean 的 ...
- Spring学习--集合属性
Spring 中可以通过一组内置的 xml 标签(例如: <list> , <set> 或 <map>) 来配置集合属性. 配置java.util.Set 需要使用 ...
- Spring事务配置的五种方式和spring里面事务的传播属性和事务隔离级别
转: http://blog.csdn.net/it_man/article/details/5074371 Spring事务配置的五种方式 前段时间对Spring的事务配置做了比较深入的研究,在此之 ...
随机推荐
- Python实现用户注册到文件
import getpass #引入getpass模块,主要用来输入密码关闭回显 f=open('d:/user','a+') #定义文件对象并以追加方式打开,不存在就创建 f.seek(0) #文件 ...
- bootstrap editable 行内编辑
除了那些bootstrap/bootstrap table的js , css之外,要额外添加editable的文件: <link href="../assets/css/bootstr ...
- 假设高度已知,请写出三栏布局,其中左栏、右栏各为300px,中间自适应的五种方法
假设高度已知,请写出三栏布局,其中左栏.右栏各为300px,中间自适应的五种方法 HTML CSS 页面布局 题目:假设高度已知,请写出三栏布局,其中左栏.右栏各为300px,中间自适应 <!D ...
- 二次封装axios,根据参数来实现多个请求多次拦截
1. 新建 axiosTool.js 文件,设置请求拦截和处理的逻辑 import Vue from 'vue' import axios from 'axios' //取消请求 let Cancel ...
- instanceOf与isInstance()方法之间的区别
instanceof运算符 只被用于对象引用变量,检查左边的被测试对象 是不是 右边类或接口的 实例化.如果被测对象是null值,则测试结果总是false.Class类的isInstance(Obje ...
- iOS 自定义一对UI表现相反的按钮
假如有一对按钮[重置][提交],要让他们的默认UI和点击的UI表现刚好相反 [提交]按钮,默认橙色,点击边框是橙色,字体是橙色,背景变白色 [重置]按钮,默认白色橙色,边框是橙色,点击字体是白色,背景 ...
- springboot+mybatis+SpringSecurity 实现用户角色数据库管理(一)
本文使用springboot+mybatis+SpringSecurity 实现用户权限数据库管理 实现用户和角色用数据库存储,而资源(url)和权限的对应采用硬编码配置. 也就是角色可以访问的权限通 ...
- 使用HeapAnalyzer分析内存泄漏
从IBM网站下载ha433包,释放,执行ha433.jar文件 https://www.ibm.com/developerworks/mydeveloperworks/groups/service/h ...
- VSCode - 使用 WSL(Windows Subsystem for Linux)
一开始我是只将 VSCode 集成的终端改成 WSL 的 Bash,结果发现内置的 GIt 用的还是 Windows 的 Git,Git Hooks 用的 Windows 的环境,上网搜了一下发现有很 ...
- 细数EDM营销中存在的两大盲点
国庆节了,祝大家国庆快乐,转眼博客至今已有三年了.下面博主为大家介绍EDM营销中存在的两大盲点,供大家参考. 一是忽略用户友好.用户友好策略是Email营销成功的关键要素,具体包括内容友好策略.方式友 ...