Spring框架学习之第7节
配置Bean的细节
☞尽量使用scope=”singleton”,不要使用prototype,因为这样对我们的性能影响较大
②如何给集合类型注入值
Java中主要的map,set,list / 数组
Collection col = new ArrayList();
col能够使用的方法(点出来的方法)取决于左边的数据类型
beans.xml
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="department" class="com.litao.collection.Department">
<property name="name" value="财务部" />
<!-- 给数组注入值 -->
<property name="empName">
<list>
<value>小明</value>
<value>小明小明</value>
<value>小明小明小明小明</value>
</list>
</property>
<!-- 给list注入值 list中可以有相同的对象 -->
<property name="empList">
<list>
<ref bean="emp1"/>
<ref bean="emp2"/>
<ref bean="emp2"/>
<ref bean="emp2"/>
<ref bean="emp2"/>
</list>
</property>
<!-- 给set注入值 set中不能有相同的对象 -->
<property name="empsets">
<set>
<ref bean="emp1"/>
<ref bean="emp2"/>
<ref bean="emp2"/>
<ref bean="emp2"/>
<ref bean="emp2"/>
<ref bean="emp2"/>
</set>
</property>
<!-- 给map注入值 map中也不能有相同的对象,后面的会把前面的覆盖,map只要key一样就可以装配value对应的bean -->
<property name="empMaps">
<map>
<entry key="1" value-ref="emp1"></entry>
<entry key="2" value-ref="emp2"></entry>
<entry key="2" value-ref="emp2"></entry>
</map>
</property>
</bean>
<bean id="emp1" class="com.litao.collection.Employee">
<property name="name" value="北京" />
<property name="id" value="1" />
</bean>
<bean id="emp2" class="com.litao.collection.Employee">
<property name="name" value="天津" />
<property name="id" value="2" />
</bean>
</beans>
Employee.java
package com.litao.collection; public class Employee { private String name;
private int id; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} }
Department.java
package com.litao.collection; import java.util.List;
import java.util.Map;
import java.util.Set; public class Department { private String name;
private String[] empName;
private List<Employee> empList;
private Set<Employee> empsets;
private Map<String,Employee> empMaps; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getEmpName() {
return empName;
}
public void setEmpName(String[] empName) {
this.empName = empName;
} public List<Employee> getEmpList() {
return empList;
}
public void setEmpList(List<Employee> empList) {
this.empList = empList;
}
public Set<Employee> getEmpsets() {
return empsets;
}
public void setEmpsets(Set<Employee> empsets) {
this.empsets = empsets;
}
public Map<String, Employee> getEmpMaps() {
return empMaps;
}
public void setEmpMaps(Map<String, Employee> empMaps) {
this.empMaps = empMaps;
} }
App1.java
package com.litao.collection; import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class App1 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ac = new ClassPathXmlApplicationContext("com/litao/collection/beans.xml"); Department department = (Department)ac.getBean("department");
System.out.println(department.getName());
for (String emName : department.getEmpName()) {
System.out.println(emName);
} System.out.println("************通过list集合取出数据**************");
for(Employee e: department.getEmpList()){ System.out.println("name=" + e.getName() + " " + e.getId()); } //set取得时候不能保证顺序,list取时可以保证顺序
System.out.println("************通过set集合取出数据**************");
for(Employee e: department.getEmpsets()){ System.out.println("name=" + e.getName()); }
System.out.println("************通过map集合取出数据**************");
//1.用迭代器
Map<String,Employee> empmaps = department.getEmpMaps(); Iterator it = empmaps.keySet().iterator(); while (it.hasNext()) {
String key = (String)it.next();
//System.out.println(key);
Employee emp = empmaps.get(key);
System.out.println("key="+key+" "+emp.getName());
} //2.最简洁的方法
for(Entry<String,Employee> entry1:department.getEmpMaps().entrySet()){
System.out.println(entry1.getKey() + " " + entry1.getValue().getName()); } } }
Spring框架学习之第7节的更多相关文章
- Spring框架学习之第2节
传统的方法和使用spring的方法 使用spring,没有new对象,我们把创建对象的任务交给了spring的框架,通过配置用时get一下就行. 项目结构 applicationContext.xml ...
- Spring框架学习之第1节
spring快速入门 ① spring是什么? Struts是web框架(jsp/action/actionform) hibernate是orm框架(对象和关系映射框架),处于持久层 sprin ...
- Spring框架学习之第9节
aop编程 aop(aspect oriented programming)面向切面(方面)编程,是所有对象或者是一类对象编程,核心是(在不增加代码的基础上,还增加新功能) 汇编(伪机器指令 mov ...
- Spring框架学习之第8节
<bean id=”foo” class=”…Foo”> <property name=”属性”> <!—第一方法引用--> <ref bean=”bean对 ...
- Spring框架学习之第3节
model层(业务层+dao层+持久层) spring开发提倡接口编程,配合di技术可以更好的达到层与层之间的解耦 举例: 现在我们体验一下spring的di配合接口编程,完成一个字母大小写转换的案例 ...
- Spring框架学习之第6节
bean的生命周期 为什么总是一个生命当做一个重点? Servlet –> servlet生命周期 Java对象生命周期 往往笔试,面试总喜欢问生命周期的问题? ① 实例化(当我们的程序加载 ...
- Spring框架学习之第5节
request session global-session 三个在web开发中才有意义 如果配置成prototype有点类似于request 如果配置成singleton有点类似于web开发中的gl ...
- Spring框架学习之第4节
从ApplicaionContext应用上下文容器中获取bean和从bean工厂容器中有什么区别: 具体案例如下 结论: 1.如果使用上下文ApplicationContext,则配置的bean如果是 ...
- Spring框架学习一
Spring框架学习,转自http://blog.csdn.net/lishuangzhe7047/article/details/20740209 Spring框架学习(一) 1.什么是Spring ...
随机推荐
- 服务器 IIS 发布网站 支持下载 apk 和 ipa
方法/步骤 1 打开IIS服务管理器,找到服务器,右键-属性,打开IIS服务属性: 2 单击MIME类型下的"MIME类型"按钮,打开MIME类型设置窗口: 3 单击" ...
- [小技巧]初次接触 SSIS Package 的一点总结
1 动态改变数据源 package从创建到调试到测试到生产环境,往往需要运行在不同的服务器上.我们可以定义Environment和Server两个变量,一个用于改变 Server,一个用于接收实际Se ...
- Qt---- 点击按钮调用另一个窗口Ui
-------------------------------------------------- #include "subdialog.h" SubDialog::SubDi ...
- 关于在 loadView 中改变状态栏的可视性
这种问题不知道大家是否遇见过,在此用两句话(时间紧迫,还得加班)分享下今天犯的错误 我把状态栏的的可视性的改变写在了loadView 里面,然后就出现了调用了两次 loadView 和 viewDid ...
- 转载:centos上yum安装apache+php+mysql等
1. 更新系统内核到最新. [root@linuxfei ~]#yum -y update 系统更新后,如果yum安装时提示错误信息,请执行以下命令修复. [root@linuxfei ~]#rpm ...
- android开发类似coverflow效果的3d旋转
源码下载地址:http://download.csdn.net/detail/feijian_/8888219
- poj 1985 Cow Marathon 树的直径
题目链接:http://poj.org/problem?id=1985 After hearing about the epidemic of obesity in the USA, Farmer J ...
- UVA 10078 The Art Gallery
Problem: Century Arts has hundreds of art galleries scattered all around the country and you are hir ...
- ios 调用打印机
源码 无意中玩一个demo发现调用了打印机 才发现ios有快速调用打印机的功能. if ([UIPrintInteractionController isPrintingAvailable] == ...
- mingw fbx sdk /浮点数精度
接下来要做一个linux下的程序了. 下载linux version fbx sdk tar zxvf ...gz 按照安装说明 提升权限并没什么用 还是,cannot execute bin ...