集合属性

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. ionic打包apkFailed to execute shell command "input,keyevent,82"" on device: Error: adb: Command failed with exit code 137

    错误代码如下 BUILD SUCCESSFUL in 12s 46 actionable tasks: 1 executed, 45 up-to-date Built the following ap ...

  2. 6 wireshark 安装使用 数据包抓取

    1.wireshark安装 2.开始使用 3.界面详情 4. 数据包抓取 5.过滤数据

  3. 【好帖】 Mark

    1. 管理篇 2. 程序员选择公司的8个标准 3. 实用工具 4. 离职跳槽 5. DBA 6. 做一个网站多少钱? 7. 十大算法 8. 寻求用户评价App的正确方法 9. 工程师忽略的隐形成本 1 ...

  4. Scala学习笔记(二):运行脚本文件

    在某个目录(如:F:\)下新建一个文本文件,命名为:hello.scala 其内容为: println("Hello World!") 那么这个时候该怎么运行这个脚本文件呢? 通过 ...

  5. 最后一片蓝海的终极狂欢-写在Win10发布前夕

    作为一名Windows8.x+系统平台从业者,从工作伊始,耳边不断充斥着Windows将走向没落的言论,Win10今日晚些时候即将发布,笔者借此机会,说说自己的看法. 早在2012年的时候,IDC曾预 ...

  6. 【APUE】Chapter5 Standard I/O Library

    5.1 Introduction 这章介绍的standard I/O都是ISOC标准的.用这些standard I/O可以不用考虑一些buffer allocation.I/O optimal-siz ...

  7. Nullable可空类型

    一个Nullable类型就是基本类型加上一个"是否为null指示器"的合成类型.对于一个类型,如果既可以给他分配一个值,也可以给它分配null引用,我们就说这个类型是可空的. 可空 ...

  8. 虚拟现实-VR-UE4-构建光照显示光照构建失败,Swarm启动失败

    闲的无聊折腾,发现想构建光照的时候,总是显示失败 如下图 百度许久,有大神指出是我在编译源码的的时候没有将其中的某个模块编译进去,只需要重新编译摸个模块就好 在UE4 的sln文件下,会看到一个Unr ...

  9. 第二十三篇 logging模块(******)

    日志非常重要,而且非常常用,可以通过logging模块实现. 热身运动 import logging logging.debug("debug message") logging. ...

  10. linux学习(二)——汤哥的推荐书籍

    成为一名精通 Linux程序设计的高级程序员一直是不少朋友孜孜以求的目标. 根据中华英才网统计数据,北京地区 Linux 程序员月薪平均为 Windows程序员的 1.8 倍.Java 程序员的 2. ...