728x90 AdSpace

Tuesday, 18 March 2014

Spring Default initialization and destroy callback methods

In this tutorial we are going to see about Spring Default (Life Cycle) initialization and destroy method with example program.

When you write initialization and destroy method callbacks that do not use the Spring-specific InitializingBean and DisposableBean callback interfaces, you typically write methods with names such as init(), initialize(), dispose(), and so on. Ideally, the names of such lifecycle callback methods are standardized across a project so that all developers use the same method names and ensure consistency.

You can configure the Spring container to look for named initialization and destroy callback method names on every bean. This means that you, as an application developer, can write your application classes and use an initialization callback called init(), without having to configure an init-method="init" attribute with each bean definition. The Spring IoC container calls that method when the bean is created. This feature also enforces a consistent naming convention for initialization and destroy method callbacks.

The presence of the default-init-method attribute on the top-level <beans/> element attribute causes the Spring IoC container to recognize a method called init on beans as the initialization method callback. When a bean is created and assembled, if the bean class has such a method, it is invoked at the appropriate time.
You configure destroy method callbacks similarly (in XML, that is) by using the default-destroy-method attribute on the top-level <beans/> element.

Where existing bean classes already have callback methods that are named at variance with the convention, you can override the default by specifying (in XML, that is) the method name using the init-method and destroy-method attributes of the <bean/> itself.

The Spring container guarantees that a configured initialization callback is called immediately after a bean is supplied with all dependencies. Thus the initialization callback is called on the raw bean reference, which means that AOP interceptors and so forth are not yet applied to the bean. A target bean is fully created first, then an AOP proxy (for example) with its interceptor chain is applied. If the target bean and the proxy are defined separately, your code can even interact with the raw target bean, bypassing the proxy. Hence, it would be inconsistent to apply the interceptors to the init method, because doing so would couple the lifecycle of the target bean with its proxy/interceptors and leave strange semantics when your code interacts directly to the raw target bean.

Now see this with example program.
Step by step procedure to create Spring Program using Eclipse given below.
Follow the Simple steps.
1. Select File –> New –> Java Project from your Eclipse IDE.
2.Create project called SpringDefaultLifeCycle.
3. Add the following jars into your build path.

4. Now create package com.javatutorialscorner.spring under SpringDefaultLifeCycle Project
5. Next create DefaultLifeCycleBean class under com.javatutorialscorner.spring package
package com.javatutorialscorner.spring;

public class DefaultLifeCycleBean {
private String sayHello;

public void init() throws Exception {
// TODO Auto-generated method stub
System.out.println("DefaultLifeCycleBean Inside init() method "
+ sayHello);

public void getSayHello() {
System.out.println("Java Tutorials Corner " + sayHello);

public void setSayHello(String sayHello) {
this.sayHello = sayHello;

public void destroy() throws Exception {
// TODO Auto-generated method stub
.println("Inside DefaultLifeCycleBean - destroy() method - bean will destroy now");

6. Now Create RunApp class which contains Bean configuration path to load configuration from Bean.xml file


package com.javatutorialscorner.spring;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class RunApp {

* @param args
public static void main(String[] args) {
// TODO Auto-generated method stub

AbstractApplicationContext context = new ClassPathXmlApplicationContext(
DefaultLifeCycleBean defaultLifeCycleBean = (DefaultLifeCycleBean) context




In above program

First step I created Application context using ClassPathXmlApplicationContext  which load configuration from bean file  which located in class path of application, It take care of creating and initializing all the objects (Beans) declared in bean.xml

Next getBean() method used to get particular bean from created context. This method used bean name as parameter and returns generic object. We can caste to actual object.

Once Object Created you can access any method from that class.

The registerShutdownHook() method declared in AbstractApplicationContext class, which ensure the graceful shutdown and class relevant destroy method.

7. Create Bean.xml at where your class files created (ClassPathXmlApplicationContext tries to load bean file from class path).


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
default-init-method="init" default-destroy-method="destroy">

<bean id="defaultLifeCycleBean" class="com.javatutorialscorner.spring.DefaultLifeCycleBean">
<property name="sayHello" value="Default Life Cycle Bean"></property>

If you have too many beans with same initialization and destroy method names, you don’t need to declare init-method and destroy-method for each bean.In this case spring provides way to configure default declaration for initialization and destroy methods in <beans> element itself, i.e default-init-method and default-destroy-method

Bean.xml is default name given to bean configuration file. You can choose any name for your bean.xml but you can use file name in main application to create context and your file available in your class path must be same.

Bean.xml is used to assign unique ID to different beans and controls the creation object with different values, using above file you can pass any value to   variable sayHello without changing the class file.

Now you can run the program see the following output in console.

DefaultLifeCycleBean Inside init() method Default Life Cycle Bean

Java Tutorials Corner Default Life Cycle Bean

Inside DefaultLifeCycleBean - destroy() method - bean will destroy now
Annamalai Thangaraj

Annamalai is a Sr.Software Engineer with 3+ years experience in Java, Spring, Struts, Hibernate, IDM/IAM, and Enterprise Web Application Development.

Website: Java Tutorials Corner

  • Blogger Comments
  • Facebook Comments
  • Disqus Comments


Post a Comment

Item Reviewed: Spring Default initialization and destroy callback methods Rating: 5 Reviewed By: Annamalai Thangaraj