spring bean life cycle analysis with examples

1: write in front

This article only involves flow charts and specific examples, and does not involve source code level analysis. Friends who are interested in source code can move here .

2: Flowchart

insert image description here

3: Examples

3.1: Code

public  class  MyLifeCycleBean  implements  BeanNameAware , 
        BeanFactoryAware , 
        BeanClassLoaderAware , 
        BeanPostProcessor , 
        InitializingBean , 
        DisposableBean { 
    private String test ;

    // Ordinary method 
    public  void  display ( ) { 
        System . out . println ( "Ordinary class instance method call..." ) ; 
    }

    public String getTest ( )  { 
        return test ; 
    }

    public  void  setTest ( String test )  { 
        this . test = test ; 
    }

    @Override 
    public  void  setBeanClassLoader ( ClassLoader classLoader )  { 
        System . out . println ( "BeanClassLoaderAware was called..." ) ; 
    }

    @Override 
    public  void  setBeanFactory ( BeanFactory beanFactory )  throws BeansException { 
        System . out . println ( "BeanFactoryAware was called..." ) ; 
    }

    @Override 
    public  void  setBeanName ( String name )  { 
        System . out . println ( "BeanNameAware was called..." ) ; 
    }

    @Override 
    public  void  destroy ( )  throws Exception { 
        System . out . println ( "DisposableBean#destroy was called..." ) ; 
    }

    @Override 
    public  void  afterPropertiesSet ( )  throws Exception { 
        System . out . println ( "InitializingBean#afterPropertiesSet was called..." ) ; 
    }

    @Override 
    public Object postProcessBeforeInitialization ( Object bean , String beanName )  throws BeansException { 
        System . out . println ( "BeanPostProcessor#postProcessBeforeInitialization was called..." ) ; 
        return bean ; 
    }

    @Override 
    public Object postProcessAfterInitialization ( Object bean , String beanName )  throws BeansException { 
        System . out . println ( "BeanPostProcessor#postProcessAfterInitialization was called..." ) ; 
        return bean ; 
    }

    public  void  initMethod ( ) { 
        System . out . println ( "init-method was called..." ) ; 
    }

    public  void  destroyMethdo ( ) { 
        System . out . println ( "destroy-method was called..." ) ; 
    } 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66

3.2: 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 " > _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    < bean id = "lifeCycle"  class = "yudaosourcecode.studyhelp.MyLifeCycleBean" 
          init - method = "initMethod" destroy - method = "destroyMethdo" > 
        < property name = "test" value = "test" / > 
    < / bean > 
< / beans >
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

3.3: Testing

@Test 
public  void  mylifecyclebeantest ( )  {
    ClassPathResource resource
            =  new  ClassPathResource ( "mylifecyclebeantest.xml" ) ; 
    XmlBeanFactory xbf =  new  XmlBeanFactory ( resource ) ; 
    // Manually add post bean processor 
    xbf . addBeanPostProcessor ( new  MyLifeCycleBean ( ) ) ;
    MyLifeCycleBean lifeCycle
            = xbf . getBean ( "lifeCycle" , MyLifeCycleBean . class ) ; 
    lifeCycle . display ( ) ; 
    // destroy the container 
    xbf . destroySingletons ( ) ; 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

run:

BeanNameAware is called. . .
BeanClassLoaderAware is called. . .
BeanFactoryAware is called. . .
BeanPostProcessor#postProcessBeforeInitialization is called. . .
InitializingBean#afterPropertiesSet is called. . .
init-method is called...
BeanPostProcessor#postProcessAfterInitialization is called. . .
Ordinary class instance method call...
DisposableBean#destroy is called. . .
destroy-method is called...

Process finished with exit code 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

Tags: spring bean life cycle analysis with examples

spring spring java

Related: spring bean life cycle analysis with examples