Bean装配
Bean作用域
常用配置项
Id bean的唯一引用
Class 需要创建实例的全限定类名
Scope bean作用域
Constructor arguments 构造注入
Properties 属性注入
Autowiring mode 自动装配
lazy-initialization mode 懒加载
Initialization/destruction method 初始化/销毁
scope配置
singleton: 单例,指一个Bean容器中只存在一份(默认值)
prototype: 每次请求(每次使用)创建新的实例,destroy方式不生效
request: 每次http请求创建一个实例且仅在当前request内有效
session: 同上,每次http请求创建,当前session内有效
global session: 基于portlet的web中有效( portlet定义了global session),如果是在web中,同session
scope示例
对于prototype,两次打印的hash值不同,说明创建了两次对象,对于默认的singleton则相同
public class BeanScope {
public void say() {
System.out.println("BeanScope say : " + this.hashCode());
}
}
@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanScope extends UnitTestBase {
public TestBeanScope() {
super("classpath*:spring-beanscope.xml");
}
@Test
public void testSay() {
BeanScope beanScope = super.getBean("beanScope");
beanScope.say();
BeanScope beanScope2 = super.getBean("beanScope");
beanScope2.say();
}
//每个单元测试方法都会启动一个spring容器
@Test
public void testSay2() {
BeanScope beanScope = super.getBean("beanScope");
beanScope.say();
}
}
<!--spring配置项-->
<bean id="beanScope" class="com.rxy.bean.BeanScope" scope="prototype"></bean>
Bean生命周期
定义 <bean id="" class="">
初始化 spring容器运行时加载bean实例
使用 applicationContext.getBean()
销毁 applicationContext.destroy()
初始化:
1.实现org.springframework.beans.factory.InitializingBean接口,重写afterPropertiesSet方法
2.init-method <bean id="" class="" init-method="xxx"></bean>
销毁:
当bean在被spring容器销毁时,会执行配置的销毁回调方法,用于一些如连接池的释放等操作
1.实现org.springframework.beans.factory.DisposableBean接口,重写destroy方法
2.destroy-method <bean id="" class="" destroy-method="xxx"></bean>
全局配置:
在xml的beans根标签中配置全局默认初始化、销毁回调方法
<beans xmlns="" default-init-method="defautInit" default-destroy-method="defaultDestroy">
示例:
当bean在反射调用构造方法初始化时,会执行配置的初始化回调方法
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class BeanLifeCycle implements InitializingBean, DisposableBean {
public void defautInit() {
System.out.println("Bean defautInit.");
}
public void defaultDestroy() {
System.out.println("Bean defaultDestroy.");
}
@Override
public void destroy() throws Exception {
System.out.println("Bean destroy.");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Bean afterPropertiesSet.");
}
public void start() {
System.out.println("Bean start .");
}
public void stop() {
System.out.println("Bean stop.");
}
}
@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanLifecycle extends UnitTestBase {
public TestBeanLifecycle() {
super("classpath:spring-lifecycle.xml");
}
@Test
public void test1() {
super.getBean("beanLifeCycle");
}
}
<?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"
default-init-method="defautInit" default-destroy-method="defaultDestroy">
<bean id="beanLifeCycle" class="com.rxy.lifecycle.BeanLifeCycle" init-method="start" destroy-method="stop"></bean>
</beans>
运行结果:
1.只配置init-method,destroy-method: spring容器启动-->Bean start .-->spring容器销毁-->Bean stop.其他两个同理
2.同时配置bean标签和实现接口:
spring容器启动-->Bean afterPropertiesSet.-->Bean start .-->spring容器销毁-->Bean destroy.-->Bean stop.
3.三种方式同时配置: 效果同2,配置全局和bean: 效果同1
4.配置全局,删除全局方法defautInit,程序正常,配置bean的init-method,删除start方法,程序报错
分析总结:
1.实现接口方式将会优于bean标签的配置,一旦单独配置了bean的初始化和销毁,全局默认配置将不会执行
2.不管bean有没有单独配置初始化和销毁,全局配置方法都可以不存在,程序正常运行,而bean标签的配置方式
则要求方法必须存在