当前位置:   article > 正文

Spring Bean的生命周期详解

bean的生命周期

(1)Spring Bean的生命周期介绍

Bean的生命周期简单分为4个阶段:实例化、属性赋值、初始化和销毁。实例化和属性赋值对应构造方法和setter方法的注入。

Spring之所以强大的原因是易扩展,生命周期相关的常用扩展点非常多。扩展点分2类:

1. 作用于多个Bean的增强扩展

InstantiationAwareBeanPostProcessor 作用于实例化阶段前后

BeanPostProcessor 作用于初始化阶段前后

2. 作用于单个bean的增强扩展

初始化阶段:Aware接口(BeanNameAware BeanClassLoaderAware BeanFactoryAware)

(2)Spring Bean的生命周期流程图

(3)代码演示

自定义一个InstantiationAwareBeanPostProcessor

  1. package com.learn.blog.demo;
  2. import org.springframework.beans.BeansException;
  3. import org.springframework.beans.PropertyValues;
  4. import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
  5. public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {
  6. /**
  7. * 在目标对象实例化之前调用,该方法的返回值类型是Object,我们可以返回任何类型的值。
  8. * 由于这个时候目标对象还未实例化,所以这个返回值可以用来代替原本该生成的目标对象的实例(比如代理对象)。
  9. * 如果该方法的返回值代替原本该生成的目标对象,后续只有postProcessAfterInitialization方法会调用,其它方法不再调用;否则按照正常的流程走
  10. */
  11. @Override
  12. public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
  13. if ("demo6".equals(beanName)) {
  14. System.out.println("执行InstantiationAwareBeanPostProcessor的postProcessBeforeInstantiation方法");
  15. }
  16. return InstantiationAwareBeanPostProcessor.super.postProcessBeforeInstantiation(beanClass, beanName);
  17. }
  18. /**
  19. * 在目标对象实例化之后调用,但是该实例的属性还未被设置。
  20. * 它的返回值决定要不要调用postProcessProperties方法的其中一个因素(另一个因素是mbd.getDependencyCheck());
  21. * 如果该方法返回false,并且不需要check,那么postProcessProperties就会被忽略不执行;
  22. * 如果返回true,postProcessProperties就会被执行
  23. */
  24. @Override
  25. public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
  26. if ("demo6".equals(beanName)) {
  27. System.out.println("执行InstantiationAwareBeanPostProcessor的postProcessAfterInstantiation方法");
  28. }
  29. return InstantiationAwareBeanPostProcessor.super.postProcessAfterInstantiation(bean, beanName);
  30. }
  31. /**
  32. * 对属性值进行修改,这个时候属性值还未赋值给目标对象
  33. */
  34. @Override
  35. public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
  36. if ("demo6".equals(beanName)) {
  37. System.out.println("执行InstantiationAwareBeanPostProcessor的postProcessProperties方法");
  38. }
  39. return InstantiationAwareBeanPostProcessor.super.postProcessProperties(pvs, bean, beanName);
  40. }
  41. }

自定义一个BeanPostProcessor

  1. package com.learn.blog.demo;
  2. import org.springframework.beans.BeansException;
  3. import org.springframework.beans.factory.config.BeanPostProcessor;
  4. public class MyBeanPostProcessor implements BeanPostProcessor {
  5. @Override
  6. public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
  7. if ("demo6".equals(beanName)) {
  8. System.out.println("执行BeanPostProcessor的postProcessBeforeInitialization方法");
  9. }
  10. return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
  11. }
  12. @Override
  13. public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
  14. if ("demo6".equals(beanName)) {
  15. System.out.println("执行BeanPostProcessor的postProcessAfterInitialization方法");
  16. }
  17. return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
  18. }
  19. }

自定义一个bean

  1. package com.learn.blog.demo;
  2. import org.springframework.beans.BeansException;
  3. import org.springframework.beans.factory.BeanFactory;
  4. import org.springframework.beans.factory.BeanFactoryAware;
  5. import org.springframework.beans.factory.BeanNameAware;
  6. import javax.annotation.PostConstruct;
  7. import javax.annotation.PreDestroy;
  8. // 实现BeanNameAware, BeanFactoryAware接口
  9. public class Demo6 implements BeanNameAware, BeanFactoryAware {
  10. private String name;
  11. public Demo6() {
  12. System.out.println("执行构造函数实例化");
  13. }
  14. // 重写BeanNameAware的方法
  15. @Override
  16. public void setBeanName(String s) {
  17. System.out.println("执行BeanNameAware的setBeanName方法");
  18. }
  19. // 重写BeanFactoryAware的方法
  20. @Override
  21. public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
  22. System.out.println("执行BeanFactoryAware的setBeanFactory方法");
  23. }
  24. // init-method
  25. private void init() {
  26. System.out.println("初始化方法");
  27. }
  28. // destory-method
  29. private void destroy() {
  30. System.out.println("销毁方法");
  31. }
  32. public String getName() {
  33. return name;
  34. }
  35. public void setName(String name) {
  36. System.out.println("注入属性name");
  37. this.name = name;
  38. }
  39. }

spring的配置文件BeanLifecycle.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <bean id="demo6" class="com.learn.blog.demo.Demo6"
  7. init-method="init" destroy-method="destroy">
  8. <property name="name" value="文大奇Quiin"></property>
  9. </bean>
  10. <bean id="myBeanPostProcessor" class="com.learn.blog.demo.MyBeanPostProcessor">
  11. </bean>
  12. <bean id="myInstantiationAwareBeanPostProcessor" class="com.learn.blog.demo.MyInstantiationAwareBeanPostProcessor">
  13. </bean>
  14. </beans>

启动类

  1. public class BlogDemoApplication {
  2. public static void main(String[] args) {
  3. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("BeanLifecycle.xml");
  4. applicationContext.getBean("demo6");
  5. ((ClassPathXmlApplicationContext) applicationContext).close();
  6. }
  7. }

执行结果

  1. 执行InstantiationAwareBeanPostProcessor的postProcessBeforeInstantiation方法
  2. 执行构造函数实例化
  3. 执行InstantiationAwareBeanPostProcessor的postProcessAfterInstantiation方法
  4. 执行InstantiationAwareBeanPostProcessor的postProcessProperties方法
  5. 注入属性name
  6. 执行BeanNameAware的setBeanName方法
  7. 执行BeanFactoryAware的setBeanFactory方法
  8. 执行BeanPostProcessor的postProcessBeforeInitialization方法
  9. 初始化方法
  10. 执行BeanPostProcessor的postProcessAfterInitialization方法
  11. 销毁方法

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号