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