Sunday 30 March 2014

Life Cycle of Spring Bean (init & destroy)

When a bean is instantiated, it may be required to perform some initialization to get it into a usable state. Similarly, when the bean is no longer required and is removed from the container, some cleanup may be required.

To define setup and teardown for a bean, we simply declare the <bean> with init-method and/or destroy-method parameters. 
  • The init-method attribute specifies a method that is to be called on the bean immediately upon instantiation. 
  • Similarly, destroy-method specifies a method that is called just before a bean is removed from the container.

In the case of XML-based configuration metadata, you can use the init-method attribute to specify the name of the method that has a void no-argument signature. For example:

<bean id="exampleBean" class="examples.ExampleBean" init-method="init"/>

Example

spring-config.xml
<?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="helloWorld" class="org.techmight.HelloWorld" scope="prototype"> 
  <property name="message" value="Hello World from TechMight Solutions!"/>
 </bean>
 
 <bean id="user" class="org.techmight.User" init-method="init" destroy-method="destroy"> 
  <property name="hello">
   <ref local="helloWorld"/>
  </property>
 </bean>
 
</beans>

HelloWorld.java
package org.techmight;

/**
 * @author TechMight Solutions
 */
public class HelloWorld {

 private String message;
 
 public String getMessage() {
  return ("Your Message: " + message);
 }

 public void setMessage(String message) {
  this.message = message;
 }
}

User.java
package org.techmight;

/**
 * @author TechMight Solutions
 */

public class User {

 HelloWorld hello;

 public HelloWorld getHello() {
  return hello;
 }

 public void setHello(HelloWorld hello) {
  this.hello = hello;
 }

 public void init() {
  System.out.println("User Bean is going through init.");
 }

 public void destroy() {
  System.out.println("User Bean will be destroyed now.");
 }

 public void execute() {
  String response = getHello().getMessage();
  System.out.println(response + " (From User class) ");
 }
}

MainApp.java
package org.techmight;

/**
 * @author TechMight Solutions
 */
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {

 public static void main(String[] args) {
  AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");

  User user = (User) context.getBean("user");
  user.execute();

  context.registerShutdownHook();
 }
}

If you are using Spring's IoC container in a non-web application environment; for example, in a rich client desktop environment; you register a shutdown hook with the JVM. Doing so ensures a graceful shutdown and calls the relevant destroy methods on your singleton beans so that all resources are released.

Note that here we have registered a shutdown hook registerShutdownHook() method declared on the AbstractApplicationContext class. This will ensures a graceful shutdown and calls the relevant destroy methods.

Output

User Bean is going through init.
Your Message : Hello World from TechMight Solutions! (From User class)
User Bean will be destroyed now.

No comments:

Post a Comment

Your comments are very much valuable for us. Thanks for giving your precious time.

Do you like this article?