You can use the Bean's Init-method and Destroy-method properties to initialize and destroy the bean.
Define a Hero class:
Packagecom.moonlit.myspring; Public classHero { Public voidborn () {System.out.println ("The hero is born."); } Public voidDefaultborn () {System.out.println ("The hero is born by default."); } Public voiddoAction () {System.out.println ("The Hero Save the world."); } Public voiddead () {System.out.println ("The hero is dead."); } Public voidDefaultdead () {System.out.println ("The hero is dead by default."); }}
To configure its bean:
<id= "Hero" class= "Com.moonlit.myspring.Hero" Init-method= "Born" destroy-method= "dead"/>
You can also use Beans's Default-init-method and Default-destroy-method properties to set the default initialization and destruction methods for all beans. (In this case, if the bean has a corresponding method, the corresponding initialization and destruction methods will be executed).
Define a Demon class:
Package com.moonlit.myspring; Public class Demon { publicvoid Defaultborn () { System.out.println ("The Demon is Born. " ); } Public void doAction () { System.out.println ("The demon Do Destroys"); } Public void Defaultdead () { System.out.println ("The demon is dead." ); }}
To configure its bean:
<id= "Demon" class= "Com.moonlit.myspring.Demon"/>
We configure the beans Default-init-method and Default-destroy-method properties as follows:
<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-3.0.xsd " default-init-method=" Defaultborn " default-destroy-method=" Defaultdead ">
The sample program Practicehero is used to test the effect:
PackageCom.moonlit.practice;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCom.moonlit.myspring.Demon;ImportCom.moonlit.myspring.Hero; Public classPracticehero { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Spring-idol.xml"); Hero Hero= (Hero) context.getbean ("Hero"); Hero.doaction (); Demon Demon= (Demon) context.getbean ("Demon"); Demon.doaction (); }}
The output results are as follows:
The hero is Born.the demon were born.the hero save the world.the demon do destroys
You cannot see the effect of the destruction, possibly because the object has not executed the destruction method at the end of the program. (I don't know how to detect destroy-method at the moment), but I can see the Init-method method. Because hero beans define Init-method and Destroy-method, the program will first look for Born () and Dead () two methods to execute But the demon Bean does not define the Init-method method and the Destroy-method method, So the program executes the Default-init-method and Default-destroy-method methods defined inside the beans, so the output works like this.
Understanding: The Init-method and destroy-method elements of a bean element can be used to define a method used by a bean when it is initialized and destroyed You can also specify the default initialization and destruction methods for all beans by beans the default-init-method and default-destroy-method elements.
Spring Learning notes-initializing and destroying beans