How to use Initialization callback methods while creating Spring bean?

July 24, 2008

Spring

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.

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

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
36
37
38
39
40
41
42
43
44
45
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

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
36
37
38
39
40
41
42
43
44
45
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");
    }
 
}

applicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
<?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>
email

Comments

comments

Related posts:

  1. How to read xml file and inject bean reference using Spring Framework?
  2. Inner beans in Spring IOC
  3. Setter Injection in Spring IOC
  4. How to lazy initialize Spring beans?
  5. Constructor Injection in Spring IOC

No comments yet.

Leave a Reply