How to use Initialization callback methods while creating Spring bean?

SHARE & COMMENT :

Initialization callback methods

Springframework provides flexibility to initialize its Beans using the user defined methods. There is some scenario where application developer want to initialize the beans properties after setting all the values. The following example program demonstrates by defining a custom method to initialize the calues.
Spring’s managed bean has to implement InitializingBean from the springframework. This interface declares one method afterPropertiesSet() which has to be overridden by the bean. So, when the bean instance is created, this method also will be called.

also read:

Though this technique is not recommended. It is easy to directly configure in the xml configuration file with the initialization method name.use init-method=”initialize” to set the value. Look into the following code for more details on implementation.

InitCallBackExample.java

package javabeat.net.spring.ioc;

import org.springframework.beans.factory.InitializingBean;

/**
 * Source : http://www.javabeat.net
 */

public class Address implements InitializingBean {
    public Address(){

    }
    private String street;
    private String city;
    private String pincode;

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getPincode() {
        return pincode;
    }

    public void setPincode(String pincode) {
        this.pincode = pincode;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public void afterPropertiesSet() throws Exception {
        System.out.println("After Properties Set Called");
    }

}

Address.java

package javabeat.net.spring.ioc;

import org.springframework.beans.factory.InitializingBean;

/**
 * Source : http://www.javabeat.net
 */

public class Address implements InitializingBean {
    public Address(){

    }
    private String street;
    private String city;
    private String pincode;

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getPincode() {
        return pincode;
    }

    public void setPincode(String pincode) {
        this.pincode = pincode;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public void afterPropertiesSet() throws Exception {
        System.out.println("After Properties Set Called");
    }

}

also read:

applicationContext.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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
       <bean id="addressBean" class="javabeat.net.spring.ioc.Address"/>
       <!--<bean id="addressBean" class="javabeat.net.spring.ioc.Address" init-method="initialize"/>-->
</beans>

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better