spring中Bean的作用域以及生命周期

Bean的作用域
在bean声明时它有一个scope属性,它是用于描述bean的作用域。
可取值有:
singleton:单例 代表在spring ioc容器中只有一个Bean实例 (默认的scope)
prototype多例 每一次从spring容器中获取时,都会返回一个新的实例
request 用在web开发中,将bean对象request.setAttribute()存储到request域中
session 用在web开发中,将bean对象session.setAttribute()存储到session域中

在开发中常用的值是singleton与prototype
spring中Bean的作用域以及生命周期

Bean的生命周期
spring中Bean的作用域以及生命周期

  1. instantiate bean对象实例化
  2. populate properties 封装属性
  3. 如果Bean实现BeanNameAware执行setBeanName
  4. 如果Bean实现BeanFactoryAwar或ApplicationContextAwar设置工厂setBeanFactory或上下文对象setApplicationContext
  5. 如果存在类实现BeanPostProcessor(后处理Bean),执行postProcessBeforeInitialization
  6. 如果Bean实现InitializingBean执行afterPropertiesSet
  7. 调用自定义的init-method方法
  8. 如果存在类实现BeanPostProcessor(处理Bean),执行postProcessAfterInitialization
  9. 执行业务处理
  10. 如果Bean实现DisposableBean执行destroy
  11. 调用自定义的destroy-method
package cn.itheima.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class BeanLifeCycle implements BeanNameAware, ApplicationContextAware, InitializingBean, DisposableBean {

	private String name;

	public BeanLifeCycle() {
		System.out.println("第一步:实例化BeanLifeCycle对象");
	}

	// 自定义功能
	public void add() {
		System.out.println("第九步:自定义功能add.....");
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		System.out.println("第二步:属性name的注入" + name);
		this.name = name;
	}

	@Override
	public void setBeanName(String name) {
		System.out.println("第三步:得到bean的id或name值:" + name);
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

		System.out.println("第四步:得到ApplicationContext对象:" + applicationContext);

	}

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("第六步:属性注入完成后");
	}

	public void myInit() {
		System.out.println("第七步:自定义的init方法");
	}

	@Override
	public void destroy() throws Exception {

		System.out.println("第十步:执行destroy方法");
	}

	public void myDestroy() {
		System.out.println("第十一步:执行自定义的销毁方法");
	}

}

applicationContext.xml配置文件

	<bean id="beanLifeCycle" class="cn.itheima.bean.BeanLifeCycle" init-method="myInit" 
		destroy-method="myDestroy"> 
		<property name="name" value="itcast"></property> 
	</bean> 
	<bean class="cn.itheima.bean.MyBeanPostProcess"></bean>

测试:

// 测试bean的生命周期
	@Test
	public void test7() {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		BeanLifeCycle beanLife = (BeanLifeCycle) applicationContext.getBean("beanLifeCycle");
		beanLife.add();
		applicationContext.close();
	}

对于bean的生命周期方法:
第三步与第四步是让Bean了解spring容器。

第五步与第八步 可以针对指定的Bean进行功能增强,这时一般会使用动态代理.

第六步与第十步:通过实现指定的接口来完成init与destroy操作
但是在开发中一般不使用第6步与第10步,原因是我们可以使用第7步与第11步来完成。
第7步与第11步的初始化与销毁操作它无耦合,推荐使用的。但是必须在配置文件中指定初始化与销毁的方法
spring中Bean的作用域以及生命周期
总结:
对于bean的生命周期,我们需要关注的主要有两个方法:

  1. 增强bean的功能可以使用后处理Bean, BeanPostProcessor
  2. 如果需要初始化或销毁操作我们可以使用init-method destroy-method

注意:destroy-method只对scope=singleton有效果。