1.Bean的生命周期
Spring 容器可以管理 singleton 作用域 Bean 的生命周期,在此作用域下,Spring 能够精确地知道该 Bean 何时被创建,何时初始化完成,以及何时被销毁。而对于 prototype 作用域的 Bean,Spring 只负责创建,当容器创建了 Bean 的实例后,Bean 的实例就交给客户端代码管理,Spring 容器将不再跟踪其生命周期。每次客户端请求prototype 作用域的 Bean 时,Spring 容器都会创建一个新的实例,并且不会管那些被配置成 prototype 作用域的 Bean 的生命周期。
了解 Spring 生命周期的意义就在于,可以利用 Bean 在其存活期间的指定时刻完成一些相关操作。这种时刻可能有很多,但一般情况下,会在 Bean 被初始化后和被销毁前执行一些相关操作。
在 Spring 中,Bean 的生命周期是一个很复杂的执行过程,我们可以利用 Spring 提供的方法定制 Bean 的创建过程。当一个 Bean 被加载到 Spring 容器时,它就具有了生命,而 Spring 容器在保证一个 Bean 能够使用之前,会进行很多工作。
Spring 容器中 Bean 的生命周期流程如下图所示。
- 1.生命周期图
- 2.步骤描述
1.调用User的构造方法,实例化bean;
2.调用User的set方法,设置属性值;
3.如果 Bean 实现了 BeanNameAware 接口,则 Spring将调用该接口setBeanName方法, 将xml配置文件中bean的id设置为beanName;
4.如果 Bean 实现了BeanFactoryAware 接口,则 Spring将调用该接口的setBeanFactory方法传入当前工厂实例的引用;
5.如果 Bean 实现了 ApplicationContextAware 接口,则 Spring将调用该接口的setApplicationContext方法传入当前 ApplicationContext 实例的引用;
6.如果 BeanPostProcessor 和 Bean 关联,则 Spring将调用该接口的预初始化方法 postProcessBeforeInitialization对 Bean 进行加工操作, Spring 的 AOP 就是利用它实现的(此处非常重要);
7.如果 Bean 实现了 InitializingBean 接口,则 Spring将调用该接口的afterPropertiesSet方法;
8.如果bean在xml文件中配置了init-method对应的方法, 则 Spring将调用init-method配置的初始化方法;
9.如果 BeanPostProcessor 和 Bean 关联,则 Spring 将调用该接口的初始化方法postProcessAfterInitialization, 此时Bean 已经可以被系统使用了;
10.如果 Bean 实现了 DisposableBean 接口,则 Spring 会调用 destory() 方法将 Spring 中的 Bean 销毁;如果bean在xml文件中配置了destory-method对应的方法,则 Spring 将调用该方法对 Bean 进行销毁。
注意:
如果在 中指定了该 Bean 的作用范围为 scope=“singleton”,则将该 Bean 放入 Spring IoC 的缓存池中,将触发 Spring 对该 Bean 的生命周期管理;如果在 中指定了该 Bean 的作用范围为 scope=“prototype”,则将该 Bean 交给调用者,调用者管理该 Bean 的生命周期,Spring 不再管理该 Bean。
2.实例测试
User类
package com.limi.test;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
//为了直观的观察生命周期, 继承BeanNameAware, BeanFactoryAware,ApplicationContextAware 重写下面几个方法来进行测试
public class User implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean {
private String name;
public User(){
System.out.println("1.调用User的构造方法,实例化bean");
}
public void setName(String name) {
this.name = name;
System.out.println("2.调用User的set方法,设置属性值");
}
@Override
public void setBeanName(String s) {
System.out.println("3.调用BeanNameAware的setBeanName方法, BeanName="+s);
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("4.调用BeanFactoryAware的setBeanFactory方法");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("5.调用ApplicationContextAware的setApplicationContext方法");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("7.调用InitializingBean的afterPropertiesSet方法");
}
//自定义的初始化方法
public void myInitMethod(){
System.out.println("8.调用init-method配置的初始化方法");
}
//自定义的销毁方法
public void myDestoryMethod(){
System.out.println("10.调用destory-method配置的销毁方法");
}
}
MyBeanPostProcessor类
package com.limi.test;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
//为了直观的观察生命周期, 继承BeanPostProcessor,重写下面两个方法来进行测试
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("6.调用BeanPostProcessor的预初始化方法postProcessBeforeInitialization");
return null;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("9.调用BeanPostProcessor的初始化方法postProcessAfterInitialization");
return null;
}
}
bean1.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--id是唯一标识, class是全类名 -->
<!--init-method="myInitMethod" destroy-method="myDestoryMethod"是配置自定义的初始化方法和销毁方法-->
<bean id="User" class="com.limi.test.User" init-method="myInitMethod" destroy-method="myDestoryMethod">
<property name="name" value="andy"></property>
</bean>
<!--把myBeanPostProcessor加入容器-->
<bean id="myBeanPostProcessor" class="com.limi.test.MyBeanPostProcessor"></bean>
</beans>
测试类MyTest
package com.limi.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test1(){
//1.加载bean的xml文件, 以src为根目录
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
//2.获取配置的对象, 参数1:bean的id值, 参数2: 类名.class
User user1 = context.getBean("User", User.class);
//3.使用对象
System.out.println("user1:"+user1);
//手动让bean实例销毁
((ClassPathXmlApplicationContext)(context)).close();
}
}
测试结果