集合属性

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. 各种数据库分页语句整理以及Oracle数据库中的ROWNUM和ORDER BY的区别

    .oracle数据库分页 select * from (select a.*,rownum rc from 表名 where rownum<=endrow) a where a.rc>=s ...

  2. C++11中rvalue references的使用

    Rvalue references are a feature of C++ that was added with the C++11 standard. The syntax of an rval ...

  3. Django admin操作

      无名小妖     昵称:无名小妖园龄:1年6个月粉丝:22关注:1 +加关注 搜索     常用链接 我的随笔 我的评论 我的参与 最新评论 我的标签 我的标签 Python(1) python3 ...

  4. pprof 查看goroutine

    package main import ( "net/http" "runtime/pprof" ) var quit chan struct{} = make ...

  5. 在Linux上进行mySql安装部署及遇到的问题的解决方法

    前提: Linux centOS虚拟机64位 1.首先确认是否已安装过MySQL 方法一:删除原有的MySQL目录: 使用查找语句: whereis mysql find / -name mysql ...

  6. 【个人训练】(UVa146)ID Codes

    题意与解析 这题其实特别简单,求给定排列的后继.使用stl(next_permutation)可以方便地解决这个问题.但是,想要自己动手解就是另外一回事了.我的解法是从后往前找到第一个$a_i$比$a ...

  7. 第一篇 Postman的初级使用之设置环境快速切换生成环境与测试环境

    POSTMAN是有谷歌的开源工具,在开发调试.测试执行过程中使用频率非常广泛,本文将记录一些postman在测试中常见的一些配置和使用方法 一.基本的页面区域 略,很简单,大家都会看,再有,学习下面的 ...

  8. LB

    LB(LBaaS)及Load Balance as a Service是 Neutron 提供的一项高级网络服务. LBaaS 允许租户在自己的网络中创建和管理 load balancer,load ...

  9. Docker 安装Neo4j

    拉取最新的neo4j镜像 docker pull neo4j 运行Neo4j 容器 docker run -it -d -p 7474:7474 -p 7687:7687 neo4j:latest 打 ...

  10. MATLAB中矢量场图的绘制 (quiver/quiver3/dfield/pplane) Plot the vector field with MATLAB

    1.quiver函数 一般用于绘制二维矢量场图,函数调用方法如下: quiver(x,y,u,v) 该函数展示了点(x,y)对应的的矢量(u,v).其中,x的长度要求等于u.v的列数,y的长度要求等于 ...