集合属性

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学习--集合属性的更多相关文章

  1. Spring -配置集合属性

    1 可使用<list> <map> <set>等来配置集合属性2 List <!-- 配置List属性 --> <bean id="pe ...

  2. [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  3. Spring学习日记03_IOC_属性注入_集合类型属性

    Ioc操作Bean管理(xml注入集合属性) 注入数组类型属性 注入List集合类型属性 注入Map集合类型属性 Stu类 public class Stu { //1. 数组类型属性 private ...

  4. Spring学习(三)几种集合属性的注入方式

    1.前言 众所周知.java中不只有八大简单类型.还有一些集合类型.本文围绕集合类型的注入做一个总结. 2.项目骨架 3.过程 1.创建实体类AllCollectionType package com ...

  5. [原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  6. Spring学习记录(二)---容器和bean属性配置

    下载spring包,在eclipse搭建spring环境. 这步我在eclipse中无法导入包,看网上的: http://sishuok.(和谐)com/forum/blogPost/list/242 ...

  7. Spring 学习笔记 4. 尚硅谷_佟刚_Spring_属性配置细节

    1,字面值 •字面值:可用字符串表示的值,可以通过 <value> 元素标签或 value 属性进行注入. •基本数据类型及其封装类.String 等类型都可以采取字面值注入的方式 •若字 ...

  8. Spring4学习笔记2-配置集合属性

    1 可使用<list> <map> <set>等来配置集合属性 2 List <!-- 配置List属性 --> <bean id="p ...

  9. Spring学习(八)-----Spring注入值到集合类型的例子

    下面例子向您展示Spring如何注入值到集合类型(List, Set, Map, and Properties). 支持4个主要的集合类型: List – <list/> Set – &l ...

随机推荐

  1. Git的使用规范(一)

    1.建议使用 git add.2.不建议使用git commit -a3.撤销没有add的git checkout . 这个命令我们可以好好的介绍一下,如果当你对文件进行了修改,你没有进行git ad ...

  2. set<pair<int,int> > 的运用

    In this cafeteria, the N tables are all ordered in one line, where table number 1 is the closest to ...

  3. SharePoint2013修复报错

         今天项目组的Sharepoint2013不小心被卸载了,本想着直接修复,谁知道在修复的时候一直报错,说找不到什么文件.报的就是类似于这样的错误: Product: ######### -- ...

  4. python接口自动化: CAS系统验证,自动完成登录并获取token,遇到302请求重定向设置(requests模块 allow_redirects=False)即可

    import requestsimport re import requests import re class Crm_token(object): try: username=int(input( ...

  5. Django源码分析之server

    乍见 Django内置的server基本包括两部分:django.core.servers和django.core.handlers 相识 servers.basehttp是Django自身提供的一个 ...

  6. BZOJ 3569 DZY Loves Chinese II 树上差分+线性基

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3569 Description 神校XJ之学霸兮,Dzy皇考曰JC. 摄提贞于孟陬兮,惟庚寅 ...

  7. 利用jquery操作dom时,用event.target优化dom操作

    html: <ul id="contents"> <li data-link="#part1">Part 1</li> &l ...

  8. 关于百度Editor富文本编辑器 自定义上传位置

    因为要在网站上编辑富文本数据,所以直接采用百度的富文本编辑器,但是这个编辑器有个缺点,默认情况下,文件只能上传到网站的根目录,不能自定义路径. 而且json配置文件只能和controller.jsp在 ...

  9. Spring MVC前台POST/GET方式传参数的方法

    假设前台通过submit传值,代码如下: <form action="testPost.do" method="post"> 页码:<inpu ...

  10. hash function比较

    http://blog.csdn.net/kingstar158/article/details/8028635 由于工作需要,针对千万级别的数据,使用stl::map着实存在着效率问题,最后使用bo ...