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 : https://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 : https://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>