Spring学习--集合属性
集合属性
Spring 中可以通过一组内置的 xml 标签(例如: <list> , <set> 或 <map>) 来配置集合属性。
配置java.util.Set 需要使用 <set> 标签 , 定义元素的方法与 List 一样。
下面我们就以 List 和 Map 为例:
配置List属性
<?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="person" class="com.itdoc.spring.beans.Person">
<property name="name" value="华崽儿"/>
<property name="sex" value="女"/>
<property name="age" value="27"/>
<property name="cars">
<list>
<ref bean="car1"/>
<ref bean="car2"/> <!--可引入 Bean , 也可用内部 Bean-->
<bean id="car3" class="com.itdoc.spring.beans.Car">
<property name="brand" value="Ferrari"/>
<property name="price" value="22500000"/>
<property name="maxSpeed" value="330"/>
</bean>
</list>
</property>
</bean> <bean id="car1" class="com.itdoc.spring.beans.Car">
<property name="brand" value="Lamborghini"/>
<property name="price" value="27000000"/>
<property name="maxSpeed" value="300"/>
</bean>
<bean id="car2" class="com.itdoc.spring.beans.Car">
<property name="brand" value="Rolls-Royce"/>
<property name="price" value="24500000"/>
<property name="maxSpeed" value="310"/>
</bean> </beans>
package com.itdoc.spring.beans; /**
* 集合中的对象
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-02-28 19:56
*/
public class Car { private String brand; private double price; private int maxSpeed; public String getBrand() {
return brand;
} public void setBrand(String brand) {
this.brand = brand;
} public double getPrice() {
return price;
} public void setPrice(double price) {
this.price = price;
} public int getMaxSpeed() {
return maxSpeed;
} public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
} @Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", price=" + price +
", maxSpeed=" + maxSpeed +
'}';
}
}
package com.itdoc.spring.beans; import java.util.List; /**
* 集合属性注入
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-02-28 19:58
*/
public class Person { private String name; private String sex; private int age; private List<Car> cars; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public List<Car> getCars() {
return cars;
} public void setCars(List<Car> cars) {
this.cars = cars;
} @Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", cars=" + cars +
'}';
}
}
package com.itdoc.spring.beans; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-02-28 20:12
*/
public class Main { public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("applicationConfig.xml");
Person person = (Person) app.getBean("person");
System.out.println(person); }
}
控制台输出:
| Person{name='华崽儿', sex='女', age=27, cars=[Car{brand='Lamborghini', price=2.7E7, maxSpeed=300}, Car{brand='Rolls-Royce', price=2.45E7, maxSpeed=310}, Car{brand='Ferrari', price=2.25E7, maxSpeed=330}]} |
配置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="person" class="com.itdoc.spring.map.Person">
<property name="name" value="华崽儿"/>
<property name="sex" value="女"/>
<property name="age" value="28"/>
<property name="cars">
<map>
<entry key="Lamborghini" value-ref="car1"/>
<entry key="Rolls-Royce" value-ref="car2"/> <!--可以引入 Bean , 也可用内部 Bean-->
<entry key="Ferrari">
<bean id="car3" class="com.itdoc.spring.beans.Car">
<property name="brand" value="Ferrari"/>
<property name="price" value="22500000"/>
<property name="maxSpeed" value="330"/>
</bean>
</entry>
</map>
</property>
</bean> <bean id="car1" class="com.itdoc.spring.beans.Car">
<property name="brand" value="Lamborghini"/>
<property name="price" value="27000000"/>
<property name="maxSpeed" value="300"/>
</bean> <bean id="car2" class="com.itdoc.spring.beans.Car">
<property name="brand" value="Rolls-Royce"/>
<property name="price" value="24500000"/>
<property name="maxSpeed" value="310"/>
</bean> </beans>
package com.itdoc.spring.map; import com.itdoc.spring.beans.Car; import java.util.Map; /**
* 集合属性注入
* http://www.cnblogs.com/goodcheap
*
* @author: Wáng Chéng Dá
* @create: 2017-02-28 19:58
*/
public class Person { private String name; private String sex; private int age; private Map<String, Car> cars; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public Map<String, Car> getCars() {
return cars;
} public void setCars(Map<String, Car> cars) {
this.cars = cars;
} @Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", cars=" + cars +
'}';
}
}
控制台输出:
| Person{name='华崽儿', sex='女', age=28, cars={Lamborghini=Car{brand='Lamborghini', price=2.7E7, maxSpeed=300}, Rolls-Royce=Car{brand='Rolls-Royce', price=2.45E7, maxSpeed=310}, Ferrari=Car{brand='Ferrari', price=2.25E7, maxSpeed=330}}} |
<props>标签
<?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="dataSource" class="com.itdjx.spring.dependency.injection.DataSource">
<property name="properties" >
<props>
<prop key="user">root</prop>
<prop key="possword">123456</prop>
<prop key="jdbcUrl">jdbc:mysql:///test</prop>
<prop key="driverClass">com.mysql.jdbc.Driver</prop>
</props>
</property>
</bean> </beans>
package com.itdjx.spring.dependency.injection; import java.util.Properties; /**
* @author Wáng Chéng Dá
* @create 2017-03-01 10:21
*/
public class DataSource { private Properties properties; public Properties getProperties() {
return properties;
} public void setProperties(Properties properties) {
this.properties = properties;
} @Override
public String toString() {
return "DataSource{" +
"properties=" + properties +
'}';
}
}
package com.itdjx.spring.dependency.injection; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* 依赖注入main
*
* @author Wáng Chéng Dá
* @create 2017-02-28 15:16
*/
public class MainIOC { public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("applicationConfig.xml");
DataSource dataSource = (DataSource) app.getBean("dataSource");
System.out.println(dataSource); }
}
控制台输出:
| DataSource{properties={driverClass=com.mysql.jdbc.Driver, user=root, jdbcUrl=jdbc:mysql:///test, possword=123456}} |
Spring学习--集合属性的更多相关文章
- Spring -配置集合属性
1 可使用<list> <map> <set>等来配置集合属性2 List <!-- 配置List属性 --> <bean id="pe ...
- [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring学习日记03_IOC_属性注入_集合类型属性
Ioc操作Bean管理(xml注入集合属性) 注入数组类型属性 注入List集合类型属性 注入Map集合类型属性 Stu类 public class Stu { //1. 数组类型属性 private ...
- Spring学习(三)几种集合属性的注入方式
1.前言 众所周知.java中不只有八大简单类型.还有一些集合类型.本文围绕集合类型的注入做一个总结. 2.项目骨架 3.过程 1.创建实体类AllCollectionType package com ...
- [原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring学习记录(二)---容器和bean属性配置
下载spring包,在eclipse搭建spring环境. 这步我在eclipse中无法导入包,看网上的: http://sishuok.(和谐)com/forum/blogPost/list/242 ...
- Spring 学习笔记 4. 尚硅谷_佟刚_Spring_属性配置细节
1,字面值 •字面值:可用字符串表示的值,可以通过 <value> 元素标签或 value 属性进行注入. •基本数据类型及其封装类.String 等类型都可以采取字面值注入的方式 •若字 ...
- Spring4学习笔记2-配置集合属性
1 可使用<list> <map> <set>等来配置集合属性 2 List <!-- 配置List属性 --> <bean id="p ...
- Spring学习(八)-----Spring注入值到集合类型的例子
下面例子向您展示Spring如何注入值到集合类型(List, Set, Map, and Properties). 支持4个主要的集合类型: List – <list/> Set – &l ...
随机推荐
- xpath简单入门
语法: 选取节点: 实例: (贴图转载自w3school) 补充: /a/@href #获取a标签的href属性 当<div class="demo">& ...
- python2.7练习小例子(二十九)
29):1.题目:按相反的顺序输出列表的值. #!/usr/bin/python # -*- coding: UTF-8 -*- a = ['one', 'two', 'three'] for ...
- 8 TFTP代码详解 协议写在程序中
1.版本1:发送请求 # -*- coding:utf-8 -*- import struct from socket import * #0. 获取要下载的文件名字: downloadFileNam ...
- Lambda方式左连接有Linq方式左连接
网上查到的直接使用Join+DefaultIfEmpty的方式是错误的,实际生成SQL是两表先内联接,然后再LEFT JOIN.经过查证,参考资料,最终得到如下两种方式的左连接写法: public v ...
- 《.NET 微服务:适用于容器化 .NET 应用的体系结构》关键结论
作为总结和要点,以下是本指南中最重要的结论.1 使用容器的好处: 基于容器的解决方案有节约成本的好处,因为容器是针对生产环境中缺少依赖而导致的部署问题提出的解决方案.容器能够显著改善devops和生产 ...
- TortoiseGit小乌龟 git管理工具
1.新建分支git远端新建分支: b001本地git目录:右击--TortoiseGit--获取(会获取到新建分支) 2.本地新建分支对应远端分支本地新建分支:b001 关联远端分支b001(之后工作 ...
- Jmeter非GUI命令参数说明
查看帮助 -h, --help print usage information and exit 查看版本 -v, --version print the version information an ...
- pip消失后复原
pip是python中比较常用的管理依赖包的工具.今天心血来潮更新一下pip版本,结果悲剧发生了. -bash: /Library/Frameworks/Python.framework/Versio ...
- HDU 1271 整数对(思路题)
假设删除第k位,把整数A表示成如下形式: A = a * 10^(k+1) + b * 10 ^k + c; 则: B = a * 10^k + c; N = A + B = (11*a+b)*10^ ...
- NO9——线段相关
#include <stdio.h> #include <iostream> #include <math.h> #include <algorithm> ...