If you need to perform specific initialization and cleanup operations when the Bean is created or destroyed, there are two options in Spring.
1. Define the init-method and destroy-method attributes of bean in XML.
Execute the init and destroy Methods unforcibly, and configure them freely in xml. The dependency with the Spring framework is low.
2. Enable the Bean initialization and destruction operation interfaces InitializingBean and DisposableBean, such:
[Java]
Public class Message implements InitializingBean, DisposableBean
Implement the corresponding interface to enforce the initialization or destruction method, but it will increase the dependency between the Code and the Spring framework.
Note that the destroy method is executed only when the scope attribute of bean is singleton and the bean is created in singleton mode. The destroy of bean is managed by SpringContainer.
XML configuration example
Spring-config.xml // The destroy method is not executed if scope = "prototype" is added to the bean: messageloaders definition in the following configuration, that is, the destroy method is not created using Singleton mode.
[Html]
<? 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-3.0.xsd>
<Bean id = "messagelow.class =" initanddestroy. messagelow." init-method = "init" destroy-method = "destroy"/>
</Beans>
Messagelotify. java // class with the init and destroy Methods
[Java]
Package initanddestroy;
Public class messageloaders
{
// Destruction method
Public void destroy ()
{
System. out. println ("destroy executed ");
}
// Initialization Method
Public void init ()
{
System. out. println ("init executed ");
}
}
Main method class:
InitAndDestroyMain. java
[Java]
Package initanddestroy;
Import org. springframework. context. ApplicationContext;
Import org. springframework. context. support. ClassPathXmlApplicationContext;
Import initanddestroy. messagelotify;
Public class InitAndDestroyMain
{
/**
*
* @ Comment [comment]
* @ Author Rong lei
*
* @ Param args
*/
Public static void main (String [] args)
{
String configString = "initanddestroy/spring-config.xml ";
ApplicationContext context = new ClassPathXmlApplicationContext (configString );
Messageloaders = context. getBean ("messageloaders", messageloaders. class );
// Disable context to trigger the bean destruction method
(ClassPathXmlApplicationContext) context). close ();
}
}
Output:
Example of InitializingBean and DisposableBean Interface Configuration
Spring-config.xml // Similarly, if messageloaders defines the scope = "" value at bean timing and is not singleton, the destroy method will not be executed.
[Html]
<? 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-3.0.xsd>
<Bean id = "messageloaders" class = "initanddestroy. messageloaders"/>
</Beans>
Messagelotify. java code:
[Java]
Package initanddestroy;
Import org. springframework. beans. factory. DisposableBean;
Import org. springframework. beans. factory. InitializingBean;
Public class messagelow.implements InitializingBean, DisposableBean
{
@ Override
Public void destroy () throws Exception
{
System. out. println ("destroy executed ");
}
@ Override
Public void afterPropertiesSet () throws Exception
{
System. out. println ("init executed ");
}
}
Main method class: // In fact, this main method class is the same as the code in the preceding xml example.
[Java]
Package initanddestroy;
Import org. springframework. context. ApplicationContext;
Import org. springframework. context. support. ClassPathXmlApplicationContext;
Import initanddestroy. messagelotify;
Public class InitAndDestroyMain
{
/**
*
* @ Comment [comment]
* @ Author Rong lei
*
* @ Param args
*/
Public static void main (String [] args)
{
String configString = "initanddestroy/spring-config.xml ";
ApplicationContext context = new ClassPathXmlApplicationContext (configString );
Messageloaders = context. getBean ("messageloaders", messageloaders. class );
// Disable context to trigger the bean destruction method
(ClassPathXmlApplicationContext) context). close ();
}
}
Author: arvinrong