In traditional Java applications, the Bean life cycle is very simple. Java's keyword new is used to instantiate a bean (perhaps he is a non-serialized). That's enough. Instead, the Bean's life cycle is more granular in the spring container. Understanding the life cycle of the spring bean is important because you might want to take advantage of the opportunity provided by spring to customize the bean creation process.
1. The container looks for the bean's definition information and instantiates it.
2. With dependency injection, spring configures all of the bean's properties according to the bean definition information.
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 into the factory itself.
5. If beanpostprocessor is associated with a bean, then their postprocessbeforeinitialzation () method is called.
6. If the bean specifies the Init-method method, it will be called.
7. Finally, if there are beanpsotprocessor and bean associations, then their postprocessafterinitialization () method will be called.
By this time, the bean can already be used by the application system and will be kept in the bean factory to know it is no longer needed. There are two ways to remove it from the Bean factory.
1. If the bean implements the Disposablebean interface, the Destory () method is called.
2. If a custom destroy method is specified, this method is called.
The lifetime of the bean in the spring application context is only a little different from the life cycle in the Bean factory, except that if the bean implements the Applicationcontextawre interface, Setapplicationcontext () method is called.
1 Packagecom.spring.lifecycle; 2 3 Importorg.springframework.beans.BeansException; 4 Importorg.springframework.beans.factory.BeanFactory; 5 ImportOrg.springframework.beans.factory.BeanFactoryAware; 6 ImportOrg.springframework.beans.factory.BeanNameAware; 7 ImportOrg.springframework.beans.factory.DisposableBean; 8 ImportOrg.springframework.beans.factory.InitializingBean; 9 ImportOrg.springframework.beans.factory.config.BeanPostProcessor; Ten One Public classHelloWorldImplementsBeannameaware,beanfactoryaware, beanpostprocessor,initializingbean,disposablebean{ A - PrivateString Hello; - the Public voidsetbeanname (String arg0) { -System.out.println ("Call Beannameaware's Setbeanname () ..." ); - } - + PublicString Gethello () { - returnHello; + } A at Public voidSethello (String hello) { - This. Hello =Hello; -System.out.println ("Call Sethello () ..." ); - } - - Public voidCustominit () { inSystem.out.println ("Call Custominit () ..."); - } to + Public voidCustomdestroy () { -System.out.println ("Call Customdestroy () ..."); the } * $ PublicObject Postprocessafterinitialization (Object arg0, String arg1)Panax Notoginseng throwsbeansexception { -System.out.println ("Call Beanpostprocessor's Postprocessafterinitialization () ..."); the return NULL; + } A the PublicObject Postprocessbeforeinitialization (Object arg0, String arg1) + throwsbeansexception { -System.out.println ("Call Beanpostprocessor's Postprocessbeforeinitialization () ..."); $ return NULL; $ } - - Public voidDestroy ()throwsException { theSystem.out.println ("Call Disposablebean's Destory () ..."); - } Wuyi the Public voidAfterpropertiesset ()throwsException { -System.out.println ("Call Initializingbean's Afterpropertiesset () ..."); Wu } - About Public voidSetbeanfactory (Beanfactory arg0)throwsbeansexception { $System.out.println ("Call Beanfactoryaware's Setbeanfactory () ..."); - } -}
1 Packagecom.spring.springtest; 2 3 Importjunit.framework.TestCase; 4 5 Importorg.springframework.beans.factory.BeanFactory; 6 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; 7 8 ImportCom.spring.lifecycle.HelloWorld; 9 Ten Public classTestlifecycleextendsTestCase { One PrivateBeanfactory BF; A - protected voidSetUp ()throwsException { -BF =NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); the } - - Public voidTestlifecycle ()throwsException { -HelloWorld Hello = (HelloWorld) bf.getbean ("HelloWorld"); +Assertequals ("Hello world!", Hello.gethello ()); - Hello.destroy (); + } A}
1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <! DOCTYPE beans Public "-//spring//dtd bean//en" "Http://www.springframework.org/dtd/spring-beans.dtd" > 3 <beans> 4 class= "Com.spring.lifecycle.HelloWorld" 5 init-method= "Custominit" destroy-method= "Customdestroy" > 6 <property name= "Hello" value= "Hello world!" ></property> 7 </bean> 8 </beans>
Call Sethello () ...
Call Beannameaware's Setbeanname () ...
Call Beanfactoryaware's Setbeanfactory () ...
Call Initializingbean's Afterpropertiesset () ...
Call Custominit () ...
Call Disposablebean's Destory () ...
The reason to know this content, because see Li Gang's ssh book is wrong.
P583 writes that when spring completes the initialization of the bean, the next callback is to implement the Setbeanname (String) method in the Beannameaware interface.
And I run the opposite effect on the internet Baidu down.
Spring's bean initialization behavior