728x90 AdSpace

Wednesday, 19 March 2014

Spring ConfigurableApplicationContext for Initialization and Destruction Callback

In this tutorial we are going to see about Spring Initialization Destruction callback using ConfigurableApplicationContext  with example program.
Configuration and lifecycle methods are encapsulated here to avoid making them obvious to ApplicationContext client code. The present methods should only be used by startup and shutdown code. See Initialization Callback and Destruction Callbacks for detail. It is not recommended to use InitializingBean and DisposableBean interfaces because it unnecessarily couples the code to Spring. Alternatively, use the @PostConstruct (for initialization) ,@PreDestroy ( for Destroy) annotation or specify a generic method that is supported by bean definitions. With XML-based configuration metadata, you use the init-method (for initialization) destroy-method ( for Destroy) attribute on the <bean/>.
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 ConfigurableApplicationContext.
3. Add the following jars into your build path.
4. Now create package com.javatutorialscorner.spring under ConfigurableApplicationContext.
5. Next create SpringConfigurableApplicationContext class under com.javatutorialscorner.spring package
package com.javatutorialscorner.spring;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class SpringConfigurableApplicationContext implements InitializingBean,
DisposableBean {
private String 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("SpringConfigurableApplicationContext Inside destroy() method "
+ sayHello);


public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub

.println("SpringConfigurableApplicationContext Inside afterPropertiesSet() method "
+ sayHello);



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.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class RunApp {

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

ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
SpringConfigurableApplicationContext defaultLifeCycleBean = (SpringConfigurableApplicationContext) context

// ConfigurableApplicationContext also contain registerShutdownhook() method
// context.registerShutdownHook();



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.


Close this application context, releasing all resources and locks that the implementation might hold. This includes destroying all cached singleton beans.

Note: Does not invoke close on a parent context; parent contexts have their own, independent lifecycle.

This method can be called multiple times without side effects: Subsequent close calls on an already closed context will be ignored.


Register a shutdown hook with the JVM runtime, closing this context on JVM shutdown unless it has already been closed at that time.

This method can be called multiple times. Only one shutdown hook (at max) will be registered for each context instance.

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">

<bean id="configurableApplicationContext"
<property name="sayHello" value="say Hello"></property>

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.

SpringConfigurableApplicationContext Inside afterPropertiesSet() method say Hello
Java Tutorials Corner say Hello
SpringConfigurableApplicationContext Inside destroy() method say Hello
Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments


Post a Comment

Item Reviewed: Spring ConfigurableApplicationContext for Initialization and Destruction Callback Rating: 5 Reviewed By: Annamalai Thangaraj