Detailed life cycle of Spring beans
The essence of the Spring IOC container is to manage the bean, and for the bean it has its life cycle in the container, its initialization and destruction also requires a process, and the following is a detailed explanation of its life cycle. The lifecycle is primarily to understand the process by which the spring IOC container initializes and destroys the bean, through which the spring IOC container initializes and destroys the bean.
Through, we can first see the steps of the life cycle.
1) If the bean implements the Setbeanname method of the interface Beannameaware, then it will call this method.
2) If the bean implements the Setbeanfactory method of the interface Beanfactoryaware, then it will call this method.
3) If the bean implements the Setapplicationcontext method of the interface Applicationcontextaware, and the Spring IOC container must also be an implementation class for the ApplicationContext interface, Then this method is called, otherwise it is not called.
4) If the Bean implements the Postprocessbeforeinitialization method of the interface Beanpostprocessor, then it will call this method.
5) If the Bean implements the Afterpropertiesset method of the interface Beanfactorypostprocessor, then it will call this method.
6) If the Bean customizes the initialization method, it invokes the defined initialization method.
7) If the Bean implements the Postprocessafterinitialization method of the interface Beanpostprocessor, and the bean completes the initialization, then the Bean will live in the Spring IOC container. The user can then obtain the service from the bean.
When the server shuts down gracefully, or encounters other events that close the spring IOC container, it calls the corresponding method to complete the bean's destruction, with the following steps:
1) First, if the bean implements the Destory method of the interface Disposablebean, it will be called.
2) If a custom destroy method is defined, it is called.
The main requirement is that the above-mentioned lifecycle interface is mostly for a single bean, and the Beanpostprocessor interface is for all beans, and when a bean implements the interface described above, it needs to be defined in the spring IOC container. The Spring IOC container is automatically recognized and executed sequentially, and the Beanpostprocessor interface is implemented and tested below.
Beanpostprocessor implementation class that handles all the beans in the spring IOC container
PackageCom.cnblogs.demrystv.bean;Importorg.springframework.beans.BeansException;ImportOrg.springframework.beans.factory.config.BeanPostProcessor; Public classBeanpostprocessorimplImplementsBeanpostprocessor {@Override PublicObject Postprocessbeforeinitialization (Object bean, String beanname)throwsbeansexception {System.out.println ("" "+ Bean.getclass (). Getsimplename () +" Object "+ Beanname +" Start instantiation "); returnBean; } @Override PublicObject Postprocessafterinitialization (Object bean, String beanname)throwsbeansexception {System.out.println ("" "+ Bean.getclass (). Getsimplename () +" Object "+ Beanname +" instantiation Complete "); returnBean; }}
Test life cycle
PackageCom.cnblogs.demrystv.pojo;Importorg.springframework.beans.BeansException;Importorg.springframework.beans.factory.BeanFactory;ImportOrg.springframework.beans.factory.BeanFactoryAware;ImportOrg.springframework.beans.factory.BeanNameAware;ImportOrg.springframework.beans.factory.InitializingBean;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.ApplicationContextAware; Public classJuicemakerImplementsBeannameaware, Beanfactoryaware, Applicationcontextaware, Initializingbean {PrivateString Beverageshop =NULL; PrivateSOURCE Source =NULL; PublicString Getbeverageshop () {returnBeverageshop; } Public voidsetbeverageshop (String beverageshop) { This. Beverageshop =Beverageshop; } PublicSource GetSource () {returnsource; } Public voidSetSource (source source) { This. Source =source; } Publicstring Makejuice () {string Juice= "This is a cup by" + Beverageshop + "Beverage shop, provided" + source.getsize () + Source.getsugar () +Source.getfruit (); returnJuice; } Public voidinit () {System.out.println ("" "+ This. GetClass (). Getsimplename () + "Execute Custom initialization Method"); } Public voiddestroy () {System.out.println ("" "+ This. GetClass (). Getsimplename () + "Execute custom Destroy Method"); } @Override Public voidsetbeanname (String arg0) {System.out.println ("" "+ This. GetClass (). Getsimplename () + "Call Setbeanname method for Beannameaware interface"); } @Override Public voidSetbeanfactory (Beanfactory arg0)throwsbeansexception {System.out.println ("" "+ This. GetClass (). Getsimplename () + "Call Setbeanfactory method for Beanfactoryaware interface"); } @Override Public voidSetapplicationcontext (ApplicationContext arg0)throwsbeansexception {System.out.println ("" "+ This. GetClass (). Getsimplename () + "Call Setapplicationcontext method for Applicationcontextaware interface"); } @Override Public voidAfterpropertiesset ()throwsException {System.out.println ("" "+ This. GetClass (). Getsimplename () + "Call Afterpropertiesset method for Initializingbean interface"); }}
At the same time, in order for the custom method to take effect, the bean that should declare the custom initialization and destroy method in XML
<!--beanpostprocessor definition-- class= "Com.cnblogs.demrystv.bean.BeanPostProcessorImpl"/> Class= "Com.cnblogs.demrystv.pojo.Source" > <property name= "fruit" value= "orange juice"/> <property name= "Sugar" value= "less sugar"/> <property name= "size" value= "Big Cup"/> </bean> Class= "Com.cnblogs.demrystv.pojo.JuiceMaker2" destroy-method= "destroy" init-method= "Init" > <property name= "Beverageshop" value= "Gong Cha"/> <property name= "source" ref= "source"/> </bean>
After the configuration is complete, test the life cycle of the Spring bean
Classpathxmlapplicationcontext CTX = new classpathxmlapplicationcontext ("Spring-cfg.xml"); = (juicemaker) ctx.getbean ("Juicemaker"); System.out.println (Juicemaker.makejuice ()); Ctx.close ();
Run it and you can get the following log:
"Source" object source begins instantiating "source" object source instantiation Complete "Juicemaker" Call Beannameaware interface Setbeanname method "Juicemaker" Call the Setbeanfactory method of the Beanfactoryaware interface "Juicemaker" to invoke the Setapplicationcontext method of the Applicationcontextaware interface " Juicemaker "Object Juicemaker begins instantiating" Juicemaker "calls the Afterpropertiesset method" Juicemaker "of the Initializingbean interface to perform a custom initialization method" Juicemaker "Object Juicemaker instantiation Complete This is a glass of small sugar orange juice provided by Gong cha Drink Shop," Juicemaker "call Disposablebean method of Destory interface" Juicemaker "to perform a custom destroy method
All the life-cycle methods from the log can be found to have been executed, from the printed log to see that beanpostprocessor for all beans, so that the lifecycle can be used to accomplish some of the needs to customize the initialization and destruction of the bean behavior.
Detailed life cycle of Spring beans