The life cycle of spring beans

Source: Internet
Author: User

Bean's life cycle:

Definitions are defined in XML files <bean> sub-tags, and objects that define a class need to use beans for automatic management injection

Initialization is the use of getting a bean container context and then initializing using the context call Start method.

Use is to get the instance in the corresponding bean

Destroy is the bean does not need to be used when the bean-related resource information to destroy

Initialization

Method One


  

Case (tested with JUNIT4)

 

 Package org.bean.example;  Public class beanlifecycle {        publicvoid  start () {        System.out.println ("Bean Start" );    }      Public void Stop () {        System.out.println ("Bean Stop");}    }
 Packageorg.bean.example;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;ImportOrg.junit.runners.BlockJUnit4ClassRunner;Importorg.util.test.UnitTestBase; the @RunWith (Blockjunit4classrunner.class) Public classTestbeanlifecycleextendsunittestbase{ Publictestbeanlifecycle () {Super("Classpath*:spring-life.xml"); } @Test Public voidTest () {Super. Getbean ("Beanlifecycle"); }}
<?xml version= "1.0" encoding= "UTF-8"?><beans    xmlns= "http://www.springframework.org/schema/ Beans "    xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance "    xmlns:p="/http/ www.springframework.org/schema/p "    xsi:schemalocation=" Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-3.1.xsd Http://www.springframework.org/schema/tx http:/ /www.springframework.org/schema/tx/spring-tx.xsd "xmlns:tx=" Http://www.springframework.org/schema/tx ">    class= "Org.bean.example.BeanLifeCycle" init-method= "Start" destroy-method= "Stop" ></bean></ Beans>

 

Method Two


Case

  

  

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.