Bean Life Cycle using xml attribute of bean
Step1. Create pojo class for bean, create two method, init method and destroy method.
package com.programmingsifts;
public class Faculty {
private int facultyId;
private String facultyName;
public int getFacultyId() {
return facultyId;
}
public void setFacultyId(int facultyId) {
this.facultyId = facultyId;
}
public String getFacultyName() {
return facultyName;
}
public void setFacultyName(String facultyName) {
this.facultyName = facultyName;
}
public Faculty() {
System.out.println("*****Faculty Object creating*****");
}
public void init()
{
System.out.println("*****Bean object is creating through init method********");
}
public void destroy()
{
System.out.println("*****Bean object is creating through destroy method********");
}
public void FacultyInfo()
{
System.out.println("Faculty ID: "+this.getFacultyId()+" Faculty Name: "+this.getFacultyName());
}
}
Step 2. Create MySpringFile.xml file that will contains single bean for the above pojo class Faculty, inside the bean tag, place two attribute named as init-method=”initMethodNameInBeanClass” and destroy-method=”destroyMethodNameInBeanClass”.
<?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 definitions here -->
<bean id="fact" class="com.programmingsifts.Faculty" init-method="init" destroy-method="destroy"></bean>
</beans>
Step3. Create main class, in which call registerShutdownHook() method to call destroy method before destroying the bean by spring container on the end of the application process.
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.programmingsifts.Faculty;
public class TestSpringApp {
public static void main(String[] args) {
// create spring ctx
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("MySpringFile.xml");
((AbstractApplicationContext)applicationContext).registerShutdownHook();
Faculty faculty=(Faculty)applicationContext.getBean("fact");
faculty.setFacultyId(1001);
faculty.setFacultyName("Heera Babu");
faculty.FacultyInfo();
}
}
Sample output:
*****Faculty Object creating*****
*****Bean object is creating through init method********
Faculty ID: 1001 Faculty Name: Heera Babu
*****Bean object is creating through destroy method********
Note:- without registerShutdownHook() init method will be invoked, but not destroy method.
We can write the same attribute as below.
<?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"
default-init-method="init" default-destroy-method="destroy">
<bean id="fact" class="com.programmingsifts.Faculty"></bean>
</beans>
We can write the same attribute as above, but when we need to called int and destroy method for all beans defined in the bean context xml file.
So that this is called life-cycle of bean define by init method and destroy method.
It is also called callback method in spring framework.
Bean Life Cycle using Annotations and InitializingBean,DisposableBean interface
Bean Life Cycle using Annotations
@PostConstruct and @PreDestroy annotation will be used from javax.annotation package.
The bean class will use these annotations in xml.
package com.programmingsifts;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class Faculty {
private int facultyId;
private String facultyName;
public int getFacultyId() {
return facultyId;
}
public void setFacultyId(int facultyId) {
this.facultyId = facultyId;
}
public String getFacultyName() {
return facultyName;
}
public void setFacultyName(String facultyName) {
this.facultyName = facultyName;
}
public Faculty() {
System.out.println("*****Faculty Object creating*****");
}
@PostConstruct
public void init()
{
System.out.println("*****Bean object is creating through init method********");
}
@PreDestroy
public void destroy()
{
System.out.println("*****Bean object is creating through destroy method********");
}
public void FacultyInfo()
{
System.out.println("Faculty ID: "+this.getFacultyId()+" Faculty Name: "+this.getFacultyName());
}
}
If we are using @PostConstruct and @PreDestroy annotations, then it is compulsory that we need to define preprocessor tag in spring.xml where all beans defined:-
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
It will look like as below:
<?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="fact" class="com.programmingsifts.Faculty"></bean>
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
</beans>
The main class will be no change:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.programmingsifts.Faculty;
public class TestSpringApp {
public static void main(String[] args) {
// create spring ctx
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("MySpringFile.xml");
((AbstractApplicationContext)applicationContext).registerShutdownHook();
Faculty faculty=(Faculty)applicationContext.getBean("fact");
faculty.setFacultyId(1001);
faculty.setFacultyName("Heera Babu");
faculty.FacultyInfo();
}
}
Output:
*****Faculty Object creating*****
*****Bean object is creating through init method********
Faculty ID: 1001 Faculty Name: Heera Babu
*****Bean object is creating through destroy method********
Bean Life Cycle using InitializingBean,DisposableBean interface
This is the last method, from which we can use callback method.
In this way, bean class need to implements the InitializingBean and DisposableBean interface, So that like afterPropertiesSet() method and destroy() method will be override and automatically invoked by the spring framework.
package com.programmingsifts;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class Faculty implements InitializingBean, DisposableBean {
private int facultyId;
private String facultyName;
public int getFacultyId() {
return facultyId;
}
public void setFacultyId(int facultyId) {
this.facultyId = facultyId;
}
public String getFacultyName() {
return facultyName;
}
public void setFacultyName(String facultyName) {
this.facultyName = facultyName;
}
public Faculty() {
System.out.println("*****Faculty Object creating*****");
}
public void FacultyInfo()
{
System.out.println("Faculty ID: "+this.getFacultyId()+" Faculty Name: "+this.getFacultyName());
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("***before creating bean --> the afterPropertiesSet will execute***");
}
@Override
public void destroy() throws Exception {
System.out.println("***the destroy method running***");
}
}
Sample OUTPUT:
*****Faculty Object creating*****
***before creating bean --> the afterPropertiesSet will execute***
Faculty ID: 1001 Faculty Name: Heera Babu
***the destroy method running***