Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Life Cycle and Callback Methods in Spring Bean

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 428
    Comment on it

    Spring Framework defines some callback methods at the time of bean life cycle. These callback methods are used to perform some useful operations between the bean initialization to bean destroy. Spring provide two interface InitializingBean and DisposableBean, which provide two methods afterPropertiesSet() to allow initialization and destroy() for the destruction of bean respectively.

    Example of Spring bean callback methods: Bean initialization callback:

    public class Person implements InitializingBean  
    {  
         @Override  
         public void afterPropertiesSet() throws Exception  
         {  
           //Do some initialization activity here  
           System.out.println("InitializingBean init method is called for Person");  
         }  
    }  
    

    Bean destruction callback:

    public class Person implements DisposableBean   
    {  
         @Override  
         public void destroy() throws Exception  
         {  
           //Do some Destruction works here  
           System.out.println("DisposableBean destroy method is called for Person");  
         }  
    }  
    

    But basically, we do not use above callback methods or interfaces, for best practice use the custom init and destroy methods. These methods configured with the bean in bean configuration file.These methods also called spring bean life cycle callback methods.

    For Example:

    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"
      xmlns:c="http://www.springframework.org/schema/c"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd">
    
      <bean id="person" class="com.evon.example.callbackMethodExample.Person" init-method="init" destroy-method="destroy">
      </bean>   
    </beans>
    

    Person.java

    package com.evon.example.callbackMethodExample;
    public class Person {
        private String name;
        private String city;
    
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getCity() {
            return city;
        }
        public void setCity(String city) {
            this.city = city;
        } 
    
        //post-init method
        public void init() throws Exception {
            System.out.println("Person initializing.");
            if(getName() == null){
                setName("Pankaj");
            }
            if(getCity() == null){
                setCity("Dehradun");
            }
        }
    
        //pre-destroy method
        public void destroy() throws Exception {
            System.out.println("Person destroy.");
        }
    }
    

    Main.java

    package com.evon.example.callbackMethodExample;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;
    public class Main {
        public static void main(String[] args) {
            ApplicationContext context = new FileSystemXmlApplicationContext("/src/main/java/com/evon/example/callbackMethodExample/bean.xml");
            Person person = (Person) context.getBean("person");
                System.out.println("Person name : "+person.getName());
                System.out.println("City name : "+person.getCity());
                ((FileSystemXmlApplicationContext)context).close(); 
        }
    }
    

    Output :

        Person initializing.
        Person name : Pankaj
        City name : Dehradun
        Person destroy.
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: