Spring drip four: Spring bean life cycle

Source: Internet
Author: User

Spring Bean Life Cycle:

Understanding the life cycle of spring is important, and we can use the spring mechanism to customize the bean instantiation Process.

--------------------------------------------------------------------------------------------------------------- ------------------------------------

Spring-service.xml:

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "http://www.springframework.org/schema/beans"     xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"    xsi:schemalocation= "http// Www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/ Spring-beans.xsd ">    <!--define a bean--    class=" Com.test.service.impl.NarCodeServiceImpl ">    </bean>    class=" Com.test.spring.BeanLifecycle "init-method=" init ">        <property name=" name "value=" Zhang San "></property >        <property name= "sex" value= "male" ></property>    </bean></beans>

Service Class:

 packagecom.test.spring;Importorg.springframework.beans.BeansException;Importorg.springframework.beans.factory.BeanClassLoaderAware;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;Importorg.springframework.context.ApplicationContext;Importorg.springframework.context.ApplicationContextAware;Importorg.springframework.context.ApplicationEventPublisher;Importorg.springframework.context.ApplicationEventPublisherAware;Importorg.springframework.context.EnvironmentAware;Importorg.springframework.context.ResourceLoaderAware;Importorg.springframework.context.annotation.ImportAware;Importorg.springframework.core.env.Environment;Importorg.springframework.core.io.ResourceLoader;Importorg.springframework.core.type.AnnotationMetadata;Importcom.sun.org.apache.xml.internal.security.Init;/*** Testing the life cycle of spring beans *@authorZSS **/ public classBeanlifecycleImplementsinitializingbean,disposablebean,beanfactoryaware,beannameaware,beanpostprocessor{PrivateString name; PrivateString sex;  publicString getName () {returnname; }     public voidsetName (String Name) {System.out.println ("" Call Beanlifecycle Object "+ this. getName () + "property set method, set value:" +name);  this. Name =name; }     publicString getsex () {returnsex; }     public voidsetsex (String Sex) {System.out.println ("" Call Beanlifecycle Object "+ this. getsex () + "property set method, set value:" +sex);  this. Sex =sex; }     public voidinit () {System.out.println ("" The Init method is called "); }         public voidClose () {System.out.println ("" the Close method is called "); } @Override public voidAfterpropertiesset ()throwsException {System.out.println ( this); System.out.println ("" "beanlifecycle called the Initailizingbean Afterporpertiesset method ..."); } @Override public voidDestroy ()throwsException {System.out.println ("" beanlifecycle removed from the spring IOC container .... "); }//@Override//public void Setapplicationcontext (applicationcontext Paramapplicationcontext)//throws Beansexception {//System.out.print ("" "call applicationcontextaware interface Setapplicationcontext method:");//System.out.println (paramapplicationcontext); //        //    }//@Override//public void Setresourceloader (resourceloader paramresourceloader) {//System.out.print ("" "call resourceloaderaware interface Setresourceloader method:");//System.out.println (paramresourceloader); //    }//@Override//public void Setimportmetadata (annotationmetadata Paramannotationmetadata) {//System.out.println (333333); //    }//@Override//public void Setenvironment (environment Paramenvironment) {//System.out.print ("" "call environmentaware interface setenvironment method:");//System.out.println (paramenvironment); //    }@Override public voidsetbeanname (String Paramstring) {System.out.println ("" Call Beannameaware interface Setbenaname method: "+paramstring); } @Override public voidsetbeanfactory (beanfactory Parambeanfactory)throwsbeansexception {System.out.print ("" Call Beanfactoryaware interface setbeanfactory method: ");            System.out.println (parambeanfactory); }//@Override//public void Setbeanclassloader (ClassLoader paramclassloader) {//System.out.print ("" "call beanclassloaderaware interface Setbeanclassloader method:");//System.out.println (paramclassloader); //    }//@Override//public void Setapplicationeventpublisher (//Applicationeventpublisher Paramapplicationeventpublisher) {//System.out.print ("" "call applicationeventpublisheraware interface Setapplicationeventpublisher method:");//System.out.println (paramapplicationeventpublisher); //    }@Override publicString toString () {return"beanlifecycle [name=" + name + ", sex=" + sex + "]"; } @Override publicObject postprocessafterinitialization (object arg0, String arg1)throwsbeansexception {System.out.println ("the Postprocessafterinitialization Method was called"); return NULL; } @Override publicObject postprocessbeforeinitialization (object arg0, String arg1)throwsbeansexception {System.out.println ("the Postprocessbeforeinitialization Method was called"); return NULL; }}

Test:

 packagecom.test.spring;Importorg.junit.Before;Importorg.junit.Test;Importorg.springframework.context.ApplicationContext;Importorg.springframework.context.support.ClassPathXmlApplicationContext; public classT {classpathxmlapplicationcontext ApplicationContext=NULL; @Before public voidbefore () {System.out.println ("" Spring ApplicationContext container began to initialize ... "); ApplicationContext=NewClasspathxmlapplicationcontext (Newstring[]{"test1-service.xml"}); System.out.println ("" Spring ApplicationContext Container initialization is complete ... "); } @Test public voidtest () {beanlifecycle beanlifecycle=applicationcontext.getbean ("beanlifecycle", Beanlifecycle.class); //applicationcontext.close ();Applicationcontext.registershutdownhook (); }}

Test Results:

" Spring ApplicationContext container begins to initialize ...2017-03-18 20:29:20 info:classpathxmlapplicationcontext-refreshing org[email protected]6c7e6b26:startup date [Sat Mar 20:29:20 CST 2017]; Root of context Hierarchy2017-03-18 20:29:20 info:xmlbeandefinitionreader-loading XML Bean Definitions fromclassPath Resource [test1-service.xml] "call beanlifecycle object Null property set method, set Value: Thursday" "call beanlifecycle object Null property set method, Set the value is: male" " Call Beannameaware interface Setbenaname method: beanlifecycle "" call Beanfactoryaware interface setbeanfactory method: org.s[email protected] 2b763bac:defining Beans [narcodeservice,beanlifecycle]; Root of factory hierarchybeanlifecycle [name= Zhang san, sex=male] "" beanlifecycle called Initailizingbean's Afterporpertiesset method ... " Init method called Postprocessbeforeinitialization method called "Spring ApplicationContext container initialization is complete ...2017-03-18 20:29:20 info:classpathxmlapplicationcontext-closing org[email protected]6c7e6b26:startup date [Sat 18 2 0:29:20 CST 2017]; Root of context hierarchy "beanlifecycle removed from the spring IOC container ..." "close method is called

Spring drip four: Spring bean life cycle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.