生命周期

这个总结的比较好!推荐一下。
官方链接

  1. [1]___Person()
  2. [2]___Person -- setName()
  3. [3]______BeanNameAware接口中的setBeanName被调用 = person
  4. [4]______ApplicationContextAware接口中的setApplicationContext被调用 = org.springframework.context.support.ClassPathXmlApplicationContext@35d176f7, started on Sun Nov 07 22:27:11 CST 2021
  5. [5]______BeanPostProcessor接口中的postProcessBeforeInitialization被调用 = person
  6. [6]______InitializingBean接口中的afterPropertiesSet被调用
  7. [7]______Person--XML指定的--init-method被调用
  8. [8]______BeanPostProcessor接口中的postProcessAfterInitialization被调用person
  9. [9]______Person--run()
  10. [10]______DisposableBean接口中的destroy()被调用
  11. [11]______Person--XML指定的--destroy-method被调用

测试类

  1. package com.lff;
  2. import org.springframework.beans.BeansException;
  3. import org.springframework.beans.factory.BeanNameAware;
  4. import org.springframework.beans.factory.DisposableBean;
  5. import org.springframework.beans.factory.InitializingBean;
  6. import org.springframework.context.ApplicationContext;
  7. import org.springframework.context.ApplicationContextAware;
  8. public class Person implements BeanNameAware, ApplicationContextAware , InitializingBean , DisposableBean {
  9. private String name;
  10. public Person() {
  11. System.out.println("[1]___Person()");
  12. }
  13. public void setName(String name) {
  14. this.name = name;
  15. System.out.println("[2]___Person -- setName()");
  16. }
  17. /**
  18. * s为在xml配置Bean的id
  19. * */
  20. @Override
  21. public void setBeanName(String s) {
  22. System.out.println("[3]______BeanNameAware接口中的setBeanName被调用 = " + s);
  23. }
  24. /**
  25. * 能够拿到容器对象,让我们知道我们是在哪个容器里面
  26. * */
  27. @Override
  28. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  29. System.out.println("[4]______ApplicationContextAware接口中的setApplicationContext被调用 = " + applicationContext);
  30. }
  31. @Override
  32. public void afterPropertiesSet() throws Exception {
  33. System.out.println("[6]______InitializingBean接口中的afterPropertiesSet被调用" );
  34. }
  35. public void initMethod(){
  36. System.out.println("[7]______Person--XML指定的--init-method被调用");
  37. }
  38. public void run(){
  39. System.out.println("[9]______Person--run()");
  40. }
  41. @Override
  42. public void destroy() throws Exception {
  43. System.out.println("[10]______DisposableBean接口中的destroy()被调用" );
  44. }
  45. public void destroyMethod(){
  46. System.out.println("[11]______Person--XML指定的--destroy-method被调用");
  47. }
  48. }
  1. package com.lff;
  2. import org.springframework.beans.BeansException;
  3. import org.springframework.beans.factory.config.BeanPostProcessor;
  4. import org.springframework.lang.Nullable;
  5. public class CommonBean implements BeanPostProcessor {
  6. @Nullable
  7. public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
  8. System.out.println("[5]______BeanPostProcessor接口中的postProcessBeforeInitialization被调用 = " + beanName);
  9. return bean;
  10. }
  11. @Nullable
  12. public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
  13. System.out.println("[8]______BeanPostProcessor接口中的postProcessAfterInitialization被调用" + beanName);
  14. return bean;
  15. }
  16. }

XML配置

  1. <bean id="person" class="com.lff.Person" init-method="initMethod" destroy-method="destroyMethod">
  2. <property name="name" value="lifufa" />
  3. </bean>
  4. <bean class="com.lff.CommonBean" />

详细解释

Bean完整的生命周期会经过很多方法的调用。划分为如下几类

  • Bean自己的生命周期方法:构造方法、setter方法、通过配置文件指定的init-method、destroy-method的方法
  • BeanNameAware的接口

    1. /**
    2. * s为在xml配置Bean的id
    3. * */
    4. @Override
    5. public void setBeanName(String s) {
    6. System.out.println("[3]______BeanNameAware接口中的setBeanName被调用 = " + s);
    7. }
  • ApplicationContextAware的接口

    1. /**
    2. * 能够拿到容器对象,让我们知道我们是在哪个容器里面
    3. * */
    4. @Override
    5. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    6. System.out.println("[4]______ApplicationContextAware接口中的setApplicationContext被调用 = " + applicationContext);
    7. }
  • InitializingBean接口

    1. @Override
    2. public void afterPropertiesSet() throws Exception {
    3. System.out.println("[6]______InitializingBean接口中的afterPropertiesSet被调用" );
    4. }
  • DisposableBean接口

    1. @Override
    2. public void destroy() throws Exception {
    3. System.out.println("[10]______DisposableBean接口中的destroy()被调用" );
    4. }
  • BeanPostProcessor接口,可以做拦截器

    1. @Nullable
    2. public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    3. System.out.println("[5]______BeanPostProcessor接口中的postProcessBeforeInitialization被调用 = " + beanName);
    4. return bean;
    5. }
    6. @Nullable
    7. public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    8. System.out.println("[8]______BeanPostProcessor接口中的postProcessAfterInitialization被调用" + beanName);
    9. return bean;
    10. }