Tuesday, 1 February 2011

Bean-Life Cycle :- InitializingBean, DisposableBean, BeanPostProcessor - BeanNameAware



Spring InitializationBean Marker Interface

There are two ways you can achive the initialization work after all necessary properties on the bean are set by the container.
1) use 'init-method' in XML configuration 
You can do initialization work in init() method.
<bean id="testInitBean" class="com.techfaq.TestBean" init-method="init"/>
public class TestBean {

public void init() {
// do some initialization work 
}
}

2) Implementing the org.springframework.beans.factory.InitializingBean interface 
You can do initialization work in afterPropertiesSet() method.
void afterPropertiesSet() throws Exception;
<bean id="testInitBean" class="com.techfaq.TestBean" />
public class TestBean {

public void afterPropertiesSet() {
// do some initialization work 
}
}


Spring DisposableBean Marker Interface

Implementing the org.springframework.beans.factory.DisposableBean interface allows a bean to get a callback when the container containing it is destroyed.
There are two ways you can achive the destroy.
1) use 'destroy-method' in XML configuration 
You can do cleanup work in cleanup() method.
<bean id="testDestBean" class="com.techfaq.TestBean" destroy-method="cleanup"/>
public class TestBean {

public void cleanup() {
// do some destruction work (like releasing pooled connections) 
}
}

2) Implementing the org.springframework.beans.factory.DisposableBean interface allows a bean to get a callback when the container containing it is destroyed 
void destroy() throws Exception;
<bean id="testDestBean" class="com.techfaq.TestBean" />
public class TestBean {

public void destroy() {
// do some destruction work (like releasing pooled connections)
}
}

Spring BeanPostProcessor

The interface BeanPostProcessor allows custom modification of all new bean instance like for example making for marker interfaces or wrapping them with all proxies. The advance of interface BeanPostProcessor is that it auto-detect BeanPostProcessor beans in their bean definations and apply all beans before any others get created.

StudentBean.java
package com.roseindia.common;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
class StudentBean implements BeanPostProcessor {

    public Object postProcessBeforeInitializtion(Object bean, String beanName)
                        throws BeansException {
               return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName)
                        throws BeansException {
               System.out.println("Initialized Bean : " + beanName);
               return bean;
    }
}
AppMain.java
package com.roseindia.common;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.ApplicationContext;
public class AppMain {

        public static void main(String[] args) {
                ApplicationContext context = new ClassPathXmlApplicationContext(
                                "context.xml");
                context.getBean("student");
        }
}
context.xml
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  
  <bean id="student" class="java.lang.String">
  </bean>
  
  <bean id="studentBean" class="com.mayank.common.StudentBean" />
</beans>

When you run this application it will display message as shown below:


Initialized Bean : student


Spring BeanNameAware

The Interface BeanNameAware is implemented by beans that help to aware of their bean name in bean factory. The setBeanName method set the name for the bean in the bean factory. In this example you will see how to implement BeanNameAware in your bean class.
StudentBean.java

package com.roseindia.common;
import org.springframework.beans.factory.BeanNameAware;
class StudentBean implements BeanNameAware {
        private String name;

        public void setBeanName(String name) {
                this.name = name;
        }

        public void showBean() {
                System.out.println("Bean name : " + this.name);
        }
}
AppMain.java

package com.roseindia.common;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.ApplicationContext;
public class AppMain {

        public static void main(String[] args) {
                ApplicationContext context = new ClassPathXmlApplicationContext(
                                "context.xml");
                StudentBean bean = (StudentBean) context.getBean("studentBean");
                System.out.println(context);
                bean.setBeanName("satya");
                bean.showBean();
        }
}
context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  <bean id="studentBean" class="com.roseindia.common.StudentBean" />
</beans>

When you run this application it will display message as shown below:


Bean name : satya

No comments:

Post a Comment