二、控制反转(inverse of control IOC )
1. Bean 工厂(BeanFactory)的功能和配置
I、 ClassPathResource
l 在类路径下查找资源
l 另有 FileSystemResource
II、 XmlBeanFactory
l 读取 xml 文件中的配置信息
l 生产bean
l 解决bean 依赖
2. Bean 的创建
spring可调用bean的构造方法,或通过工厂方法生产 bean对象
I、 利用 bean 的构造方法创建bean
l 无参的构造方法(通过set方法设置,一定要有set方法)
<bean id="refName" class="com.xasxt.UserDao">
<property name="username" value="root"/>
<property name="password" value="123"/>
</bean>
l 有参的构造方法,需指明构造方法的参数列表,通过:
<bean id="refName" class=" com.xasxt.UserDao ">
<construct-arg><value>root</value></construct-arg>
<construct-arg><value>123</value></construct-arg>
</bean>
表明调用了有两个参数的构造方法
II、 通过工厂方法获得 bean对象
bean 没有公共的构造方法,需通过工厂类或工厂实例来创建
分为静态工厂和实例工厂方法两种情况
l 静态工厂方法:
class 指明工厂类,factory-method 指明工厂方法如:
Connection con=DriverManager.getConnection(url,user,pwd);
对应的配置:
<beanid="con" class="java.sql.DriverManager" factory-method="getConnection"> <construct-arg><value>jdbc:mysql://localhost/test</value></construct-arg>
<construct-arg><value>zhangsan</value></construct-arg>
<construct-arg><value>pwd</value></construct-arg>
</bean>
工厂方法参数和构造方法参数设置的形式相同
l 实例工厂方法:
要先创建工厂实例,然后再由工厂实例创建产品
如: Statement stmt = con.createStatement();
con 对象是工厂
stmt 对象是产品
配置为:
<bean id="con" class="java.sql.DriverManager" factory-method="getConnection">
……
</bean>
<bean id="stmt" factory-bean="con" factory-method="createStatement"/>
3. Bean的作用域
Bean作用域(scope属性)
作用域
描述
singleton
在每个Spring IoC容器中一个bean定义对应一个对象实例。
prototype
一个bean定义对应多个对象实例。
request
在一次HTTP请求中,一个bean定义对应一个实例;即每次HTTP请求将会有各自的bean实例,它们依据某个bean定义创建而成。该作用域仅在基于web的Spring ApplicationContext情形下有效。
session
在一个HTTP Session中,一个bean定义对应一个实例。该作用域仅在基于web的Spring ApplicationContext情形下有效。
global session
在一个全局的HTTP Session中,一个bean定义对应一个实例。典型情况下,仅在使用portlet context的时候有效。该作用域仅在基于web的Spring ApplicationContext情形下有效。
bean工厂生产bean 缺省都是单例的(singleton)
后两者用于web应用中
4. 通过注入解决bean的依赖
I、 依赖注入的两种方式
l set 注入
spring 直接利用了bean的set或构造方法
避免了使用接口注入的侵入性
<bean id="person" class="com.xasxt.spring.Person">
<!-- set方法设置注入 -->
<property name="id" value="001"/>
<property name="name" value="zhangsan"/>
<property name="sex" value="M"/>
<property name="mari" value="false"/>
<property name="salary" value="4500"/>
</bean>
l 构造方法注入
<bean id="person" class="com.xasxt.spring.Person">
<!-- 构造方法的方式注入 -->
<constructor-arg>
<value>002</value>
</constructor-arg>
<constructor-arg>
<value>lisi</value>
</constructor-arg>
<constructor-arg>
<value>false</value>
</constructor-arg>
<constructor-arg>
<value>M</value>
</constructor-arg>
<constructor-arg>
<value>5000</value>
</constructor-arg>
</bean>
II、 依赖的目标类型分成三种形式:
l 基本类型+String
<value>data</value>
类型自动转化
l 对其他bean的引用
<bean id="prop" abstract="true"><!—只作为模板,不能被实例化-->
<property name="address" value="beijing"></property>
</bean>
<!—上面是公共属性注入,下面可以引用-->
<bean id="stu" class="com.xasxt.dependency.Student" parent="prop"><!—可以引用公共属性-->
<property name="name" value="zhangsan"></property>
<property name="code" value="01101"></property>
</bean>
<bean id="tea" class="com.xasxt.dependency.Teacher" parent="prop"><!—可以引用公共属性-->
<property name="name" value="Li Sir"></property>
<property name="job" value="java teacher"></property>
</bean>
<!-- 普通装配 -->
<bean id="c" class="com.xasxt.dependency.Class">
<property name="name" value="class One"></property>
<property name="teacher" ref="tea"></property>
<property name="stus">
<list>
<ref bean="stu"/>
</list>
</property>
</bean>
l 集合类型
Set
<property name="sets">
<set>
<value>wangso</value>
<value>haoren</value>
<value>ganka</value>
</set>
</property>
Array和List同样设置
List
Map
<property name="map">
<map>
<entry key="aaa">
<value>7688</value>
</entry>
<entry key="bbb">
<value>9788</value>
</entry>
<entry key="ccc">
<value>8769</value>
</entry>
</map>
</property>
III、 让spring 自动装配bean,解决依赖
autowire属性指明自动装配的依据:
byName--- id的值=bean中的property名
byType--- 属性的类型=当前工厂中bean的类型
constructor--构造方法参数的类型=当前工厂中bean的类型
autodetect-- constructor ---> byType
autowire-candidate属性
指明是否作为 autowire 的候选对象(true或false)
解决多个候选对象的冲突

IV、 让容器检测 bean 所有的依赖是否都已经满足
防止bean的遗漏装配,在运行后出现莫名奇妙的情况,可以设置下列属性
dependency-check属性指明应检查的目标类型
simple -- 基本类型+字符串+集合
objects --- 对其他bean的依赖
all---对各类型都检查
none -- 默认值
V、 通过classpath自动扫描方式把组件纳入spring容器中管理
1、需要在文件中加入下面一句配置:
<context:component-scan base-package="com.xasxt.spring"/>
黄色是包名
2、还需要在各个类中加入下列注解之一:
@Service @Controller @Repository @Component

约定优于配置