Spring Bean lifecycle in practice

spring-framework-core-27-728

If you are looking for some Spring lifecycle Bean information or if you want to understand how the Spring create the instances under the their scope here I describe step by step and you can download one project in this link with some classes what fi you run them you will see in the log the steps clearly:

  – The principal steps awares.
 – BeanPostProcessor, Before initialization.
 – PostConstruct.
 – InitializingBean.
 – init a Person (a Object).
 – BeanPostProcessor After Initialization.
 – PreDestroy.
 – Destroy.

slcb2

slcb1

When you run the main method in SpringTest1 class you will see this on log:

1 – BeanNameAware: Object Person has been Initialized. Makes the object aware of their bean name in a bean factory.

2 – ClassLoaderAware, parent class loader gives the class loader used by the present bean factory to load bean classes.

3 – BeanFactoryAware: Be aware of their owning BeanFactory.

** BeanPostProcessor, Before Initialization person one per Bean, Spring container init instantiating, configuring, and initializing Bean.

4 – @PostConstruct: Init method after properties are set

5 – InitializingBean: afterPropertiesSet

6 – Init a Person: initPerson, init a Person by init-method method in Bean by  default-init-method or

6 – Init a Person: default-init, Initializing bean: person Default called to initialize Bean. If init-method is setted in beans.xml  this method will be executed.

       <bean id=“person”
             class=“br.com.springframework.test.Person”
             init-method=“initPerson”>
             <property name=“name” value=“Felipe” />
       </bean>

** BeanPostProcessor After Initialization person one per Bean, Spring container finish instantiating, configuring, and initializing Bean
Method talk is executed: ** I said hello! **

Calling ConfigurableApplicationContext close()…
ov 10, 2015 4:14:14 PM org.springframework.context.support.ClassPathXmlApplicationContext doClose
INFORMAÇÕES: Closing org.springframework.context.support.ClassPathXmlApplicationContext@1f8302d: startup date [Tue Nov 10 16:14:13 BRST 2015]; root of context hierarchy

7 @PreDestroy: Spring Clean Up! Person is cleaned up now. (named via beans.xml)
8- DisposableBean destroy(): Released the bean: person Called after Spring container is released the bean.

Enjoy!