• 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

Configuring Spring Lifecycle Callbacks with java

June 8, 2017 By j2eereference Leave a Comment

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 .

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

Below is the Bean class which has two methods

init() and cleanUp ().

Java
1
2
3
4
5
6
7
8
public class MyClass {
public void init(){
System.out.println("Inside init");
}
public void cleanUp(){
System.out.println("Inside cleanUp");
}
}

Lets create a class to configure the bean and its callback methods. initMethod is the attribute to configure the initialization method and destroyMethod is the attribute to configure the destroy method.

 

Java
1
2
3
4
5
6
7
8
9
10
11
12
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class mySpringConf {
 
    @Bean(initMethod = "init", destroyMethod = "cleanUp" )
     public MyClass myClass() {
        return new MyClass();
    }
 
}

Below class SpringConfDemo is to test the initMethod and destryMethod configuration.

Java
1
2
3
4
5
6
7
8
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 
public class SpringConfDemo {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(mySpringConf.class);
ctx.close();
}
}

Output:

Jun 06, 2017 1:45:42 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2c634b: startup date [Tue Jun 06 13:45:42 IST 2017]; root of context hierarchy
Inside init
Jun 06, 2017 1:45:42 PM org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
INFO: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@2c634b: startup date [Tue Jun 06 13:45:42 IST 2017]; root of context hierarchy
Inside cleanUp

Here you can notice that:

  • init method of the bean is getting called as soon as we create the context
  • destroy method is getting called when we close the context.

 

 

Related Posts

  • Spring dependency injection with Java configuration
  • What is Spring Boot?
  • @import annotation in Spring
  • 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
  • Spring JSR-250 Annotations

Filed Under: Spring

Reader Interactions

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.