We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 16 March 2014

Spring Initialization Callbacks

In this tutorial we are going to see Spring Life cycle - Initialization callbacks (init(),InitializingBean ) with example program.
The org.springframework.beans.factory.InitializingBean interface allows a bean to perform initialization work after all necessary properties on the bean have been set by the container. The InitializingBean interface specifies a single method:
void afterPropertiesSet() throws Exception;

It is recommended that you do not use the InitializingBean interface because it unnecessarily couples the code to Spring. Alternatively, use the @PostConstruct annotation or specify a POJO initialization method. In the case of XML-based configuration metadata, you use the init-method attribute to specify the name of the method that has a void no-argument signature. For example, the following definition:

<bean id="initBean" class="com.javatutorialscorner.spring.SpringInitializingBean" init-method="init"/>

public class SpringInitializingBean {

public void init() {
// do some initialization work
}
}

...is exactly the same as...

<bean id="initializingBean" class="com.javatutorialscorner.spring.SpringInitializingBean1"/>

public class SpringInitializingBean1 implements InitializingBean {

public void afterPropertiesSet() {
// do some initialization work
}
}

... but does not couple the code to Spring. We will see both types with example.

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 SpringInitializingBean.

3. Add the following jars into your build path.

commons-logging-1.1.3.jar

spring-beans-3.2.6.RELEASE.jar

spring-context-3.2.6.RELEASE.jar

spring-context-support-3.2.6.RELEASE.jar

spring-core-3.2.6.RELEASE.jar

spring-expression-3.2.6.RELEASE.jar

4. Now create package com.javatutorialscorner.spring under SpringInitializingBean Project

5. Next create SpringInitializingBean class under com.javatutorialscorner.spring package. This class using init() method for initialization work.

SpringInitializingBean.java

package com.javatutorialscorner.spring;

public class SpringInitializingBean {
private String sayHello;


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

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

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

6. Next create SpringInitializingBean1 class under com.javatutorialscorner.spring package. This class implements InitializingBean for initialization work.

SpringInitializingBean1.java

package com.javatutorialscorner.spring;

import org.springframework.beans.factory.InitializingBean;

public class SpringInitializingBean1 implements InitializingBean {

private String sayHello;

@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("SpringInitializingBean1 Inside afterPropertiesSet() method "+sayHello);
}

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

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

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

RunApp.java

package com.javatutorialscorner.spring;

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

public class RunApp {

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

ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
SpringInitializingBean initializingBean = (SpringInitializingBean)context.getBean("initBean");
initializingBean.getSayHello();

SpringInitializingBean1 initializingBean1 = (SpringInitializingBean1)context.getBean("initializingBean");
initializingBean1.getSayHello();
}

}

In above program

First step I created Application context using ClassPathXmlApplicationContext  which load configuration from bean file  which located in class path of apllication, 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.

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

Bean.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.xsd">

<bean id="initializingBean" class="com.javatutorialscorner.spring.SpringInitializingBean1">
<property name="sayHello" value="initializingBean"></property>
</bean>
<bean id="initBean" class="com.javatutorialscorner.spring.SpringInitializingBean" init-method="init">
<property name="sayHello" value="initBean"></property>
</bean>
</beans>

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.

SpringInitializingBean1 Inside afterPropertiesSet() method initializingBean

SpringInitializingBean Inside init() method initBean

Java Tutorials Corner initBean

Java Tutorials Corner initializingBean

Shop and help us

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

0 comments:

Post a Comment

Item Reviewed: Spring Initialization Callbacks Rating: 5 Reviewed By: eHowToNow