集合属性

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. The Road to learn React书籍学习笔记(第三章)

    The Road to learn React书籍学习笔记(第三章) 代码详情 声明周期方法 通过之前的学习,可以了解到ES6 类组件中的生命周期方法 constructor() 和 render() ...

  2. poj1050 dp动态规划

    Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...

  3. Android开发——Google关于Application Not Responding的建议

    秒内没有执行完毕. 2.       避免ANR的一些建议 Android applications normally run entirely on asingle (i.e. main) thre ...

  4. Electron入门应用打包exe(windows)

    最近在学习nodejs,得知Electron是通过将Chromium和Node.js合并到同一个运行时环境中,用HTML,CSS和JavaScript来构建跨平台桌面应用程序的一门技术.对于之前一直从 ...

  5. Hibernate-ORM:07.Hibernate中的参数绑定

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客会讲解Hibernate中的参数绑定,就是相当于sql语句中的where后面的条件 一,讲解概述: 1 ...

  6. JAXB轻松转换xml对象和java对象

    实体类如下: package com.cn.entity; import java.util.List; import javax.xml.bind.annotation.XmlAccessType; ...

  7. ArcGIS Server远程处理服务器(环境设置)

    当使用ArcGIS Server做远程处理服务器执行影像处理操作时,提示ERROR 999999通用错误代码,如下: Start Time: Mon Jul 03 13:49:06 2017Distr ...

  8. C++学习013多态

    何为多态 面向对象最要的特征之一就是多态,而纯虚函数是实现多态的主要方式.它可以提供一个通过用的接口,同样调用一个方法, 由于运算对象不同,方法也不同,这也就是所谓的动态绑定. #include &l ...

  9. 51单片机实现外部中断0-F

    #include< reg51.h> #define uint unsigned int #define uchar unsigned char sfr P0M0 = 0x94; sfr ...

  10. pandas DataFrame行或列的删除方法

    pandas DataFrame的增删查改总结系列文章: pandas DaFrame的创建方法 pandas DataFrame的查询方法 pandas DataFrame行或列的删除方法 pand ...