spring学习(4)
在spring容器内拼凑bean叫做装配。装配bean的时候,需要告诉容器哪些bean以及容器如何使用依赖注入将它们配合在一起。
上下文定义文件的根元素是<beans>,<beans>中有很多<bean>
id不能重复,class要写全。
scope
prototype,singleton,request,session,global-session,
默认为singleton
使用原型bean会对性能产生影响。
特别强调一点,尽量使用scope=singleton,不要使用原型prototype,因为这样对我们的性能影响比较大。
实例化和销毁init-method destroy-method
有时候没有用init-method destroy-method,却使用了注解。如:
@PostConstruct
public void init(){
System.out.println("我自己的init方法");
}
可以注入任何东西,从基本类型到集合类,甚至可以是应用系统的bean。
②如何给集合类型注入值
java主要有几种集合:map set list / 数组
Collection col = new ArrayList()
col只能使用Collection本身有的方法,但是却可以调用被ArrayList实现了的方法。
set使用方法和list一样。
注入list和set。
list中可以注入许多相同的bean
set也可以,但是后面的会把前面的覆盖。
Department类
Employee类
数组的使用:
Depart类
package com.hsp.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 Map<String, Employee> getEmpMaps() {
return empMaps;
}
public void setEmpMaps(Map<String, Employee> empMaps) {
this.empMaps = empMaps;
}
public Set<Employee> getEmpsets() {
return empsets;
}
public void setEmpsets(Set<Employee> empsets) {
this.empsets = empsets;
}
public List<Employee> getEmpList() {
return empList;
}
public void setEmpList(List<Employee> empList) {
this.empList = empList;
}
public String[] getEmpName() {
return empName;
}
public void setEmpName(String[] empName) {
this.empName = empName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//Employee类
package com.hsp.collection; public class Employee { private String name;
private int id; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}
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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
"> <bean id="department" class="com.hsp.collection.Department">
<property name="name" value="财务部">
</property>
<property name="empName"><!-- 数组 -->
<list>
<value>小明</value>
<value>大明</value>
<value>大大明</value>
</list>
</property>
<!-- 给list注入值 -->
<property name="empList">
<list>
<ref bean="emp1"/>
<ref bean="emp2"/>
<ref bean="emp1"/>
<ref bean="emp2"/>
<ref bean="emp1"/>
<ref bean="emp2"/>
</list>
</property>
<!-- 给set注入值 -->
<property name="empsets">
<set>
<ref bean="emp1"/>
<ref bean="emp2"/>
<ref bean="emp1"/>
<ref bean="emp2"/>
<ref bean="emp1"/>
<ref bean="emp2"/>
</set>
</property>
<!-- 给map注入值 -->
<property name="empMaps">
<map>
<entry key="1" value-ref="emp1" />
<entry key="2" value-ref="emp2" />
</map>
</property>
<!-- 给属性集合配置 -->
<property name="pp">
<props>
<prop key="pp1">abcd</prop>
<prop key="pp2">1234</prop>
</props>
</property>
</bean>
<bean id="emp1" class="com.hsp.collection.Employee">
<property name="name" value="北京"/>
<property name="id" value="1"/>
</bean>
<bean id="emp2" class="com.hsp.collection.Employee">
<property name="name" value="天津"/>
<property name="id" value="2"/>
</bean>
</beans>
③内部bean
<bean id="foo" class="...Foo">
<property name="emp">
<!-- 第一方法引用 -->
<ref bean='neibu'/>
<bean id="neibu"><!-- 该bean只能在emp里面使用 --> </bean>
</property>
</bean>
④继承配置
<property name="name" value="顺平"/>
name的值是类的属性名
public class Student
public class Graduate extends Student
在beans.xml文件中体现配置
<!-- 配置一个学生对象 -->
<bean id="student" class="com.hsp.inherit.Student">
<property name="name" value="顺平"/>
<property name="age" value="30"/>
</bean>
<!-- 配置graduate对象 -->
<bean id="graduate" parent="student" class="com.hsp.inherit.Graduate">
<!-- 如果自己配置属性name,age,则会替换从父对象继承的数据 -->
<property name="name" value="小明"/>
<property name="degree" value="学士"/>
</bean>
就如同java里的子类继承父类一样,子类新增的东西会把父类的同名对象覆盖。
若bean的属性是集合类型,按如下处理
设置为null
<property name="name">
<null/>
</property>
⑤
思考:目前我们都是通过set方式给bean注入值,spring还提供了其他的方式注入值,比如通过构造函数注入值!
通过构造函数注入值
beans.xml关键代码
<bean id="employee" class="com.hsp.constructor.Employee">
<!-- 通过构造函数来注入值 -->
<constructor-arg index="0" type="java.lang.String" value="大明"/>
<!-- <constructor-arg index="1" type="int" value="123"/> --> </bean>
set注入的确定是无法清晰表达哪些属性是必须的,哪些是可选的,构造注入的优势是通过构造强制依赖关系,不可能实例化不完全的或无法使用bean。
Spring IoC容器可以自动装配相互协作bean之间的关联关系。
自动装配有5种方式
自动装配bean的属性值
自动装配的原理:
autowire=byName:根据属性名自动装配。
比如正好发现有一个bean的id名叫做dog,
原理图:
(1)byName的用法,去匹配bean的id值与类属性名相同的bean
<!-- 配置一个master对象 -->
<bean id="master" class="com.hsp.autowire.Master" autowire="byName">
<property name="name">
<value>顺平</value>
</property>
</bean>
<!-- 配置dog对象 -->
<bean id="dog" class="com.hsp.autowire.Dog">
<property name="name" value="小黄"/>
<property name="age" value="3"/>
</bean>
(2)byType
在没有匹配到的情况下才会去使用byType。寻找和属性类型相同的bean。
<!-- 配置一个master对象 -->
<bean id="master" class="com.hsp.autowire.Master" autowire="byType">
<property name="name">
<value>顺平</value>
</property>
</bean>
<!-- 配置dog对象 -->
<bean id="dog11" class="com.hsp.autowire.Dog">
<property name="name" value="小黄"/>
<property name="age" value="3"/>
</bean>
(3)constructor:查找和bean的构造参数一致的或多个bean
使用了constructor,就去寻找是否有一个构造函数把dog接收到了,如果找到了就好。
<bean id="master" class="com.hsp.autowire.Master" autowire="constructor">
<property name="name">
<value>顺平</value>
</property>
</bean>
<!-- 配置dog对象 -->
<bean id="dog11" class="com.hsp.autowire.Dog">
<property name="name" value="小黄"/>
<property name="age" value="3"/>
</bean>
但是只能有一个构造函数。
建议:能不用自动装配就不要用自动装配,set注入就可以了。
(4)autodotect:(3)和(2)之间选一个方式。不确定性的处理与(3)和(2)一致。
(5)default-autowire="byName" 是写在 xsi:schemaLocation="">中的
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
" default-autowire="no" >
当你在<beans>指定了default-autowire后,所有的bean的默认的autowire就是指定的装配方式。
default-autowire默认是no.
(6)no:这是默认值。
spring学习(4)的更多相关文章
- spring 学习之 bean 的注入方式 property和constructor-arg的使用方式
spring 学习之 bean 的注入方式 property和constructor-arg的使用方式. bean的注入方式: property 注入是: 通过setxx方法注入. construct ...
- Spring学习之AOP总结帖
AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对组件(比如类)进行开发,然后对组件进行组 ...
- Spring学习之第一个AOP程序
IOC和AOP是Spring的两大基石,AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对 ...
- MyEclipse Spring 学习总结三 SpringMVC
MyEclipse Spring 学习总结三 SpringMVC 一.SpringMVC原理 1.Springmvc 框架介绍 1)Spring 框架停工了构建Web应用程序的全功能MVC模块.Spr ...
- Spring学习 Ioc篇(一 )
一直以来忙于项目的开发,Spring虽然不用,一直想系统地学习一下,想看看它的源码,都没有时间,这段时间比较充裕,就索性先把Spring学习下,熟悉各个功能再去探究它内部的实现.就从Ioc篇开始学习. ...
- Spring学习(三)——Spring中的依赖注入的方式
[前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...
- Spring学习(二)——Spring中的AOP的初步理解[转]
[前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...
- 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展
<Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...
- Spring学习8-Spring事务管理
http://blog.sina.com.cn/s/blog_7ffb8dd501014e0f.html Spring学习8-Spring事务管理(注解式声明事务管理) 标签: spring注 ...
- Spring学习之Ioc控制反转(1)
开始之前: 1. 本博文为原创,转载请注明出处 2. 作者非计算机科班出身,如有错误,请多指正 ---------------------------------------------------- ...
随机推荐
- oracle查看用户有哪些权限和角色
select * from dba_sys_privs t where t.grantee='HR';select * from dba_role_privs t where t.grantee='H ...
- Android Studio 使用笔记:查看类结构和继承关系
选中类 ,按下F4,可以打开类的源代码 在 Eclipse 中我们可以使用 Ctrl + O 组合热键查看类的结构,Android Studio 中也可以做到. View -> Tool Win ...
- PlSql加入数据库链接
Oracle-OraDb10g_home2 -> 配置和移植工具 -> net Manager -> 本地 -> 服务命名 -> 加入(网络服务名自己命名,主机名:要 ...
- SVN分支与主干
我的理解:在svn版本库中创建两个目录,一个主干如truck,一个分支目录如branch(注:分支可以创建多个),分别在客户端中检出代码,在分支中进行bug的修复以及新模块的开发,开发完后再merge ...
- 基于jquery的bootstrap在线文本编辑器插件Summernote (转)
Summernote是一个基于jquery的bootstrap超级简单WYSIWYG在线编辑器.Summernote非常的轻量级,大小只有30KB,支持Safari,Chrome,Firefox.Op ...
- Eight(经典题,八数码)
Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- python中TCP和UDP区别
TCP(Transmission Control Protocol)可靠的.面向连接的协议(eg:打电话).传输效率低全双工通信(发送缓存&接收缓存).面向字节流.使用TCP的应用:Web浏览 ...
- WORD表格数据运算技巧
我们经常会用WORD制作表格,有时表内的数据要运算的,WORD表格数据运算能力无法与EXCEL相比.但常见的乘除加减.相邻数据累加,将金额数字自动转成大写,WORD都能在表格内自动完成.下面以一个简单 ...
- valuestack,stackContext,ActionContext.之间的关系以及action的数据在页面中取得的方法
转自:http://blog.csdn.net/quechao123/article/details/4406148 1.三者之间的关系如下图所示: 2.action的数据在页面中取得的方法 在st ...
- CentOS6安装DaoCloud加速器
天朝的网,你又不是不懂.我最爱的红杏最近也用不了了.FUCK GFW. 在这,我们使用DaoCloud的加速器,打开网址 https://dashboard.daocloud.io/mirror 找到 ...