Featured Post
Spring Bean Life Cycle
- Get link
- X
- Other Apps
As soon as you define all beans and its injections in your XML
that will be loaded using XmlBeanFactory class
the code snippet will be look like as followes
XmlBeanFactory factory = new XmlBeanFactory(new
ClassPathResource("/context.xml"));
System.out.println(factory.getBean("Mybean"));
Now the Life Cycle of Spring Bean starts
1)If the Bean is instance of BeanNameAware setBeanName method will be
called with necessary params
2)If Bean is instance of BeanClassLoaderAware setBeanClassLoader() method
will be called with necessary params
3)if it is BeanFactoryAware so setBeanFactory() method will be called with
necessary params
4)If beans implements BeanPostProcessor then
postProcessBeforeInitialization() and postProcessAfterInitialization()
methods will be called
so you need to add your postProcessor here to your xmlBeanFactory
5)If Bean Implements InitializingBean then afterPropertiesSet() method will
be called
6)If Bean is associated with any init-method that will be resolved
7)If Bean Implements DisposableBean destroy() method will be called
8)If any destroy-method property is associated with that bean that will be
resolved
Please let me know Any comments
Sample code as follows
package com.randd;
import java.beans.PropertyDescriptor;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import
org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor
;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Main {
public static void main(String[] args) throws Exception {
XmlBeanFactory factory = new XmlBeanFactory(new
ClassPathResource("/context.xml"));
System.out.println(factory.getBean("Mybean"));
factory.addBeanPostProcessor((BeanPostProcessor)factory.getBean(
"Mybean"));
}
}
class Bean extends BeanSupport {
private String company;
public static void doThis(){
System.out.println("Dothis Method has been called ");
}
public void setCompany(String company) {
this.company = company;
}
@Override
public String toString() {
return String.format("%s : \"%s\"", this.company, getValue());
}
}
class BeanSupport implements
InitializingBean,BeanNameAware,BeanFactoryAware,BeanPostProcessor,DisposableBean
{
private String value;
public final void afterPropertiesSet() throws Exception {
System.out.println("After Properties Set method has been called");
}
public final void setValue(String value) {
this.value = value;
}
protected final String getValue() {
return this.value;
}
@Override
public void setBeanName(String arg0) {
// TODO Auto-generated method stub
System.out.println("SetBeanName method has been called"+arg0);
}
@Override
public void setBeanFactory(BeanFactory arg0) throws BeansException {
// TODO Auto-generated method stub
System.out.println("setBeanFactory method has been called"
+arg0);
}
@Override
public Object postProcessAfterInitialization(Object arg0, String
arg1)
throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessAfterInitialization method has
been called "+arg0+":"+arg1);
return null;
}
@Override
public Object postProcessBeforeInitialization(Object arg0, String
arg1)
throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessBeforeInitialization method has
been called "+arg0+":"+arg1);
return null;
}
@Override
public void destroy() throws Exception {
// TODO Auto-generated method stub
System.out.println("Destroy Method has been called");
}
}
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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="Mybean" class="com.randd.Bean" init-method="doThis" >
<property name="company" value="Name"/>
<property name="value" value="Roseindia.net"/>
</bean>
</beans>
Thanks
Sitaram pv
This e-Mail may contain proprietary and confidential information and is sent for the intended recipient(s) only. If by an addressing or transmission error this mail has been misdirected to you, you are requested to delete this mail immediately. You are also hereby notified that any use, any form of reproduction, dissemination, copying, disclosure, modification, distribution and/or publication of this e-mail message, contents or its attachment other than by its intended recipient/s is strictly prohibited.
Visit us at http://www.polaris.co.in
Comments