• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

LifeCycle call backs in Spring

May 21, 2015 By j2eereference 1 Comment

LifeCycle callback in spring – Spring provides call back methods for the life cycle of the beans. We can have a method in our bean that gets run when the bean is created and we can have a method which will run when the bean is about destroy. Hence we can use this methods for some initialization activity and for clean up activity as well .

Lets see how the lifecycle call back works in Spring with the below example code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.j2eereference.spring;
 
public class Person {
 
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void display(){
System.out.println("Name is  "+name);
System.out.println("Age is  "+age);
}
public void myInit(){
System.out.println("This is from myInit Method");
}
public void myDestroy(){
System.out.println("This is from myDestroy Method");
}
}

Spring.xml

XHTML
1
2
3
4
5
6
7
8
9
10
<?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="personBean" class="com.j2eereference.spring.Person" init-method="myInit" destroy-method="myDestroy">
    <property name="name" value="Leo"/>
    <property name="age" value="30"/>
    </bean>    
</beans>

Now lets test this configuration with the below code:

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.j2eereference.spring;
 
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class SpringDemo {
public static void main(String[] args) {
AbstractApplicationContext context= new ClassPathXmlApplicationContext("spring.xml");
context.registerShutdownHook();
Person person= (Person)context.getBean("personBean");
person.display();
}
}

context.registerShutdownHook(); – This is required only in case of desktop application, the ShutdownHook is called when main method exit.
If you have a web application you don’t have to do this, rather Spring context will know when to end.

Output:

This is from myInit Method
Name is Leo
Age is 30
This is from myDestroy Method

Here you can notice that the myInit  method is getting called before executing the display method and myDestroy method is getting called at the end.

If you have scenario where in you want to call the same init and destroy method for all the defined beans . We can set it as default-init-method and default-destroy-method as below.

 

XHTML
1
2
3
4
5
6
7
8
9
10
11
<?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" <strong>default-init-method</strong>="myInit" <strong>default-destroy-method</strong>="myDestroy">
  
     <bean id="personBean" class="com.j2eereference.spring.Person">
    <property name="name" value="Leo"/>
    <property name="age" value="30"/>
    </bean>    
</beans>

 

Related Posts

  • Spring dependency injection with Java configuration
  • What is Spring Boot?
  • @import annotation in Spring
  • Configuring Spring Lifecycle Callbacks with java
  • Configuring Spring Bean Scope with java
  • Spring configuration using java
  • @Required annotation in spring
  • Event handling in Spring
  • Using MessageSource in Spring
  • Component annotation in spring

Filed Under: Spring

Reader Interactions

Trackbacks

  1. Configuring Spring Lifecycle Callbacks with java - J2EE Reference says:
    June 8, 2017 at 12:11 am

    […] know how to configure lifecycle callbacks using configuration xml file. Here lets find out how to configure the same without using the configuration xml […]

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.