The life cycle of the bean in spring, the life cycle understanding of the bean during the learning spring, is a great help for learning spring, and I'll explain the life cycle of the beans in ApplicationContext and beanfactory, respectively.
1, the life cycle of beans in ApplicationContext
The lifecycle execution process is as follows:
1. Need to find all the beans to instantiate the bean based on the information defined by the bean
2. Using dependency injection, spring defines the information by the bean. Configure all the properties of the bean
3. If the bean implements the Beannameaware interface, the factory calls the Bean's Setbeanname () method to pass the Bean's ID.
4. If the bean implements the Beanfactoryaware interface, the factory calls the Setbeanfactory () method to the factory itself.
5, if the Bean implements the Applicationcontextaware () interface, the Setapplicationcontext () method is called
6, if Beanpostprocessor and Bean Association,
Then their postprocessbeforeinitialization () method is called
7. If the bean specifies the init-method= "Init" method, it will be invoked.
8, if there is beanpostprocessor and Bean Association,
Then their postprocessafterinitialization () method is called
Note: At this point, the bean can be used by the system being applied and maintained in the Beanfactory factory until it is no longer needed. But we can also destroy it by 9 or 10.
9, if the bean implements the Disposablebean interface, the Distroy () method is called
10, this method is called if the destroy-method= "close" Custom Destroy method is specified.
Case study:
Create an entity bean code as follows:
Package Www.csdn.spring.dao;
Import org.springframework.beans.BeansException;
Import Org.springframework.beans.factory.BeanFactory;
Import Org.springframework.beans.factory.BeanFactoryAware;
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 Hellodaoimpl implements Beannameaware, Beanfactoryaware, Initializingbean, Applicationcontextaware,
Disposablebean {private String content;
Public Hellodaoimpl () {System.out.println ("----------Hellodaoimpl instantiation");
The contents of the public void SetContent (String content) {System.out.println ("----------through Dependency Injection are:" + content); This.content = content; @Override public void Setbeanname (String beanid) {System.out.println ("----------Output Beani
D: "+ Beanid); @Override public void Setbeanfactory (Beanfactory factory) throws Beansexception {System
. OUT.PRINTLN ("----------Factory:" + Factory.getclass ()); @Override public void Setapplicationcontext (ApplicationContext applicationcontext) throw
S beansexception {System.out.println ("----------" + applicationcontext); @Override public void Afterpropertiesset () throws Exception {System.out.println ("------
----Afterpropertiesset ");
public void init () {System.out.println ("----------initialization Method"); @Override public void Destroy () throws Exception {System.out.println ("----------Bean is destroyed
");
}public void Close () {System.out.println ('----------close '); }
}