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. 作者非计算机科班出身,如有错误,请多指正 ---------------------------------------------------- ...
随机推荐
- 自动化测试工具 - Unified Functional Testing
这几天跟自动化测试工具UFT耗上了... 网罗了下,居然有不少自动化测试工具,像Selenium,QTP(UFT前身),LoadRunner,真是只有想不到,没有人家办不到. 言归正传,记录下小白使用 ...
- Ubuntu 18.04 初始化(server版本 )
系统安装 ubuntu 18.04 英文版,创建个人用户 初始系统 a.修改ip shell> vim /etc/network/interfaces auto ens33 iface ens3 ...
- 好文章收藏--五分钟理解一致性哈希算法(consistent hashing)
一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT)实现算法,设计目标是为了解决因特网中的热点(Hot spot)问题,初衷和CARP十分类似.一致性哈希修正了CARP使用的简 单哈 ...
- android Notification 的使用(锁定通知栏)
近期一直在研究 android .并一边研究一边做应用.当中遇到了把程序通知常驻在 Notification 栏,而且不能被 clear 掉(就像android QQ一样)的问题.经过研究实现了其功能 ...
- Android 更改项目包名的方法
修改APP包名,即APP的唯一标识. 1.在项目上右键,选择android tools->rename application package,输入需要改为的名称,然后选择需要改的包,有部分包可 ...
- iOS - 富文本
iOS--NSAttributedString超全属性详解及应用(富文本.图文混排) ios项目中经常需要显示一些带有特殊样式的文本,比如说带有下划线.删除线.斜体.空心字体.背景色.阴影以及图文 ...
- Dire Wolf(区间DP)
Dire Wolf Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others)Total ...
- 基于python实现简单web服务器
做web开发的你,真的熟悉web服务器处理机制吗? 分析请求数据 下面是一段原始的请求数据: b'GET / HTTP/1.1\r\nHost: 127.0.0.1:8000\r\nConnectio ...
- 第三课 nodejs读取文件
//引入文件操作模块var fs = require('fs'); //读取文件 使用 回调函数 utf-8编码读取 a.txt在当前文件目录fs.readFile('a.txt','UTF-8',f ...
- elasticsearch从入门到出门-02-简单的CRUD
操作背景: 电商网站上面的一个商品的增删改查: es 能接受的都是JSON格式的数据 Es 提供了一套简单的集群信息健康监控的api GET /_cat/health?v epoch t ...