In traditional Java applications, bean lifecycle is very simple. Java keyword new is used to instantiate Bean (maybe non-serialized ). This is enough. On the contrary, bean lifecycle is more detailed in spring containers. Understanding the lifecycle of spring bean is very important, because you may need to use the opportunity provided by spring to customize the bean creation process.
1. The Container looks for bean definition information and instantiates it.
2. Use dependency injection. Spring configures all bean attributes according to bean definition information.
3. If the bean implements the beannameaware interface, the factory calls the setbeanname () method of bean to pass the bean ID.
4. If bean implements the beanfactoryaware interface, the factory calls the setbeanfactory () method to pass it to the factory itself.
5. If beanpostprocessor is associated with bean, their postprocessbeforeinitialation () method will be called.
6. If the bean specifies the init-method, it will be called.
7. Finally, if beanpsotprocessor is associated with bean, their postprocessafterinitialization () method will be called.
At this time, the bean can be used by the application system and will be kept in the Bean factory to know that it is no longer needed. There are two ways to delete it from Bean factory.
1. If bean implements the disposablebean interface, the destory () method is called.
2. If a custom destruction method is specified, call this method.
The life cycle of bean in spring application context is only different from that in Bean factory. The only difference is that if bean implements the applicationcontextawre interface, the setapplicationcontext () method is called.
1. The Container looks for bean definition information and instantiates it.
2. Use dependency injection. Spring configures all bean attributes according to bean definition information.
3. If the bean implements the beannameaware interface, the factory calls the setbeanname () method of bean to pass the bean ID.
4. If bean implements the beanfactoryaware interface, the factory calls the setbeanfactory () method to pass it to the factory itself.
5. If beanpostprocessor is associated with bean, their postprocessbeforeinitialation () method will be called.
6. If the bean specifies the init-method, it will be called.
7. Finally, if beanpsotprocessor is associated with bean, their postprocessafterinitialization () method will be called.
At this time, the bean can be used by the application system and will be kept in the Bean factory to know that it is no longer needed. There are two ways to delete it from Bean factory.
1. If bean implements the disposablebean interface, the destory () method is called.
2. If a custom destruction method is specified, call this method.
The life cycle of bean in spring application context is only different from that in Bean factory. The only difference is that if bean implements the applicationcontextawre interface, the setapplicationcontext () method is called.
Example: (here is only an example of Bean Factory)
1. helloworld. Java
Java code
- PackageCom. Spring. lifecycle;
- ImportOrg. springframework. Beans. beansexception;
- ImportOrg. springframework. Beans. Factory. beanfactory;
- ImportOrg. springframework. Beans. Factory. beanfactoryaware;
- ImportOrg. springframework. Beans. Factory. beannameaware;
- ImportOrg. springframework. Beans. Factory. disposablebean;
- ImportOrg. springframework. Beans. Factory. initializingbean;
- ImportOrg. springframework. Beans. Factory. config. beanpostprocessor;
- Public ClassHelloworldImplementsBeannameaware, beanfactoryaware, beanpostprocessor, initializingbean, disposablebean {
- PrivateString hello;
- Public VoidSetbeanname (string arg0 ){
- System. Out. println ("Call beannameaware's setbeanname ()...");
- }
- PublicString gethello (){
- ReturnHello;
- }
- Public VoidSethello (string Hello ){
- This. Hello = Hello;
- System. Out. println ("Call sethello ()...");
- }
- Public VoidCustominit (){
- System. Out. println ("Call custominit ()...");
- }
- Public VoidCustomdestroy (){
- System. Out. println ("Call customdestroy ()...");
- }
- PublicObject postprocessafterinitialization (Object arg0, string arg1)
- ThrowsBeansexception {
- System. Out. println ("Call beanpostprocessor's postprocessafterinitialization ()...");
- Return Null;
- }
- PublicObject postprocessbeforeinitialization (Object arg0, string arg1)
- ThrowsBeansexception {
- System. Out. println ("Call beanpostprocessor's postprocessbeforeinitialization ()...");
- Return Null;
- }
- Public VoidDestroy ()ThrowsException {
- System. Out. println ("Call destory ()..." of disposablebean ()...");
- }
- Public VoidAfterpropertiesset ()ThrowsException {
- System. Out. println ("Call the afterpropertiesset ()..." of initializingbean ()...");
- }
- Public VoidSetbeanfactory (beanfactory arg0)ThrowsBeansexception {
- System. Out. println ("Call beanfactoryaware's setbeanfactory ()...");
- }
- }
2. Test Procedures
Java code
- PackageCom. Spring. springtest;
- ImportJUnit. Framework. testcase;
- ImportOrg. springframework. Beans. Factory. beanfactory;
- ImportOrg. springframework. Context. Support. classpathxmlapplicationcontext;
- ImportCom. Spring. lifecycle. helloworld;
- Public ClassTestlifecycleExtendsTestcase {
- PrivateBeanfactory BF;
- Protected VoidSetup ()ThrowsException {
- BF =NewClasspathxmlapplicationcontext ("applicationcontext. xml ");
- }
- Public VoidTestlifecycle ()ThrowsException {
- Helloworld Hello = (helloworld) BF. getbean ("helloworld ");
- Assertequals ("Hello world! ", Hello. gethello ());
- Hello. Destroy ();
- }
- }
3. applicationcontext. xml
XML Code
- <? XMLVersion = "1.0" encoding = "UTF-8"?>
- <! Doctype beans public "-// spring // DTD bean // en" http://www.springframework.org/dtd/spring-beans.dtd">
- <Beans>
- <BeanId = "helloworld" class = "com. Spring. lifecycle. helloworld"
- Init-method = "custominit" Destroy-method = "customdestroy">
- <PropertyName = "hello" value = "Hello world! "> </Property>
- </Bean>
- </Beans>
4. Running result:
Reference call sethello ()...
Call beannameaware's setbeanname ()...
Call beanfactoryaware's setbeanfactory ()...
Call the afterpropertiesset () of initializingbean ()...
Call custominit ()...
Call destory () of disposablebean ()...