The life cycle of spring beans

Source: Internet
Author: User

First, Initialize and destroy

1, the project structure is as follows:

2. New Replyservice class

 package   hjp.spring.beanlifecycle;  public  class   Replyservice { void   Addreply () {System.out.println (" Add Replay ");  public  void   Myinit () {System.out.println ( "Init method" );  public  void   Mydestroy () {System.out.println ( "Destroy Method" ); }}

3. Create a new Beans.xml document, Init-method specify the execution method at initialization, Destroy-method specify the method to execute when the bean is destroyed

Note: The Destruction method requires: (1), must be a singleton (2), must close the container

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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/bean S/spring-beans.xsd ">    <BeanID= "Replyserviceid"class= "Hjp.spring.beanlifecycle.ReplyService"Init-method= "Myinit"Destroy-method= "Mydestroy"></Bean></Beans>

4. New Test class

 Packagehjp.spring.beanlifecycle;Importorg.junit.Test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classTestApp {@Test Public voiddemo1 () {Classpathxmlapplicationcontext ApplicationContext=NewClasspathxmlapplicationcontext ("Hjp/spring/beanlifecycle/beans.xml"); Replyservice Replyservice= Applicationcontext.getbean ("Replyserviceid", Replyservice.class);        Replyservice.addreply ();    Applicationcontext.close (); }}

Second, post-processing beans

1. The spring post-processing bean provides a mechanism to modify the instance object

2. Post-processing bean usage

(1), write implementation class, implement Interface Beanpostprocessor

 Packagehjp.spring.beanlifecycle;Importorg.springframework.beans.BeansException;ImportOrg.springframework.beans.factory.config.BeanPostProcessor; Public classMybeanpostprocessorImplementsBeanpostprocessor {/*** The Postprocessafterinitialization initialization method (that is, the Myinit method in the example) executes the * Postprocessbeforeinitialization initialization method (that is, in the example Myin It method) after executing the * Bean refers to the instance object created by spring, which is equivalent to the attribute class instantiation object of the configuration node bean in beans * beanname refers to spring to create the instance name, which is the attribute ID of the configuration node bean in beans; Beanname The bean by filtering * If no action is made, the bean must be returned or the null pointer error*/@Override PublicObject Postprocessbeforeinitialization (Object bean, String beanname)throwsbeansexception {//TODO auto-generated Method StubSystem.out.println ("Pre-initialization"); returnBean; } @Override PublicObject Postprocessafterinitialization (Object bean, String beanname)throwsbeansexception {//TODO auto-generated Method StubSYSTEM.OUT.PRINTLN ("After initialization"); returnBean; }}

(2), the implementation class is configured to the Spring container, the spring container when the bean is created, is automatically used after processing the bean

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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/bean S/spring-beans.xsd ">    <!--To create a target class -    <BeanID= "Replyserviceid"class= "Hjp.spring.beanlifecycle.ReplyService"Init-method= "Myinit"Destroy-method= "Mydestroy"></Bean>    <!--Configure post-processing beans, just configure class,spring to automatically use -    <Beanclass= "Hjp.spring.beanlifecycle.MyBeanPostProcessor"></Bean></Beans>

The life cycle of spring beans

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.