目录
1 Bean的作用域
在Spring中默认情况下,所管理的Bean都是单例的。可以通过bean标签的scope属性设置Bean的作用域。
1.1 作用域范围
scope的取值范围
singleton:在整个IOC容器中,只会创建一个Bean的实例。(默认)
prototype:在整个IOC容器中,每次获取Bean的对象时,都会创建一个Bean的实例。
request:在Web项目中,每一次HTTP请求,都会创建一个Bean的实例。
session:在Web项目中,每一个会话,都会创建一个Bean的实例。
1.2 简单案例
默认情况下,Spring中的Bean作为范围都是单例的(singleton),对象会在IOC容器初始化完成时就创建完毕;如果将Bean的作用域修改为非单例的,对象将会在使用时创建。
Address.java
public class Address {
private String province;
private String city;
public Address() {
System.out.println("对象已被创建");
}
}
applicationContext.xml
<bean id="s8" class="com.wyl.spring.Address" scope="prototype">
<property name="province" value="辽宁"></property>
<property name="city" value="沈阳"></property>
</bean>
SpringBeanTest.java
public class SpringBeanTest {
public static void main(String[] args) {
// 1.创建IOC容器,并指定配置文件位置
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
}
}
控制台没有任务输出
如果使用默认的作用域
<bean id="s8" class="com.wyl.spring.Address" scope="singleton">
<property name="province" value="辽宁"></property>
<property name="city" value="沈阳"></property>
</bean>
控制台输出
对象已被创建
2 Bean的生命周期
Spring IOC容器可以管理Bean的生命周期,即在某个时间点可以执行某种方法。
2.1 验证生命周期
BeanLife.java
public class BeanLife {
private Integer id;
public BeanLife() {
System.out.println("对象被创建");
}
public void init(){
System.out.println("初始化方法在执行");
}
public void destroy(){
System.out.println("对象被销毁");
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
System.out.println("为属性赋值");
this.id = id;
}
@Override
public String toString() {
return "BeanLife{" +
"id=" + id +
'}';
}
}
beanLife.xml(Spring配置文件)
<bean id="beanLife" class="com.wyl.spring.BeanLife" init-method="init" destroy-method="destroy">
<property name="id" value="23"></property>
</bean>
将创建好的Bean交给Spring管理,使用标签的init-method属性指定Bean初始化时执行的方法,通过destroy-method属性指定Bean销毁时执行的方法。
BeanLifeTest.java 测试类
public class BeanLifeTest {
public static void main(String[] args) {
// 1.创建IOC容器,并指定配置文件位置
ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext("beanLife.xml");
// 2.从IOC容器中获取管理的对象
BeanLife beanLife = ac.getBean("beanLife",BeanLife.class);
// 3.输出验证
System.out.println(beanLife);
// 4.销毁容器
ac.close();
}
}
控制台输出
对象被创建
为属性赋值
初始化方法在执行
BeanLife{id=23}
对象被销毁
2.2 初步总结
从上述的执行过程可以看出,Spring创建一个Bean的过程为
- 调用构造方法创建Bean的实例。
- 为Bean的属性赋值。
- 调用Bean的初始化方法。
- 使用Bean。
- 容器关闭,调用Bean的销毁方法。
2.3 Bean的后置处理器
- Bean的后置处理器可以在Bean的初始化方法前后对Bean进行一些额外的处理。
- Bean的后置处理器并不是针对某一个Bean进行处理,而是针对Spring管理的所有Bean进行额外的处理。
2.3.1 创建后置处理器
想要在Spring容器中使用后置处理器,需创建一个类来实现BeanPostprocessor接口
2.3.1.1 BeanPostprocessor接口
package org.springframework.beans.factory.config;
import org.springframework.beans.BeansException;
public interface BeanPostProcessor {
Object postProcessBeforeInitialization(Object var1, String var2) throws BeansException;
Object postProcessAfterInitialization(Object var1, String var2) throws BeansException;
}
1. postProcessBeforeInitialization方法:在Bean的初始化方法之前执行
2. postProcessAfterInitialization方法:在Bean的初始化方法之后执行
2.3.1.2 创建后置处理器
BeanProcessor.java
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class BeanProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
System.out.println("后置处理器的postProcessBeforeInitialization方法被执行");
return o;
}
@Override
public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
System.out.println("后置处理器的postProcessAfterInitialization方法被执行" + s ) ;
return o;
}
}
方法中的两个形参
- Object o:当前正在初始化的Bean实例。
- String s:当前Bean的id。
注意:自定义的类实现BeanPostProcessor接口的两个方法后,方法的返回值不能为null,否则后续将获取不到Bean的实例。
2.3.1.3 将自定义的后置处理器加入到Spring中管理
<bean class="com.wyl.spring.BeanProcessor"></bean>
2.3.1.4 测试
public class BeanLifeTest {
public static void main(String[] args) {
// 1.创建IOC容器,并指定配置文件位置
ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext("beanLife.xml");
// 2.从IOC容器中获取管理的对象
BeanLife beanLife = ac.getBean("beanLife",BeanLife.class);
// 3.输出验证
System.out.println(beanLife);
// 4.销毁容器
ac.close();
}
}
控制台输出
对象被创建
为属性赋值
后置处理器的postProcessBeforeInitialization方法被执行
初始化方法在执行
后置处理器的postProcessAfterInitialization方法被执行
BeanLife{id=23}
对象被销毁
2.3.1.4 总结
在加入Spring的后置处理器后,Bean的生命周期流程是:
- 调用构造方法创建Bean的实例。
- 为Bean的属性赋值。
- 调用后置处理器的postProcessBeforeInitialization方法
- 调用Bean的初始化方法。
- 调用后置处理器的postProcessAfterInitialization方法
- 使用Bean。
- 容器关闭,调用Bean的销毁方法。