Spring Bean Life Cycle methods, Spring Bean Life Cycle
In this tutorial you will see an example of spring bean life cycle.
In this tutorial you will see an example of spring bean life cycle.
Spring Bean Life Cycle methods
In this example you will know about spring bean life cycle and how a bean is
initialized in the program. The bean factory container search for bean
definition and also instantiates the bean.
BeanMain.java
package net.roseindia;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BeanMain {
public static void main(String[] args) {
BeanFactory beanfactory = new ClassPathXmlApplicationContext(
"context.xml");
BeanFirst beanObj1 = (BeanFirst) beanfactory.getBean("bean1");
}
}
|
BeanFirst.java
package net.roseindia;
public class BeanFirst {
private BeanSecond bean2;
private BeanThird bean3;
public void setBean3(BeanThird bean3) {
this.bean3 = bean3;
System.out.println("Setter method called for BeanThird");
}
public BeanFirst(BeanSecond bean2) {
super();
this.bean2 = bean2;
System.out.println("BeanFirst constructor is called");
}
public void init() {
System.out.println("Init of BeanFirst method called");
}
}
|
BeanSecond.java
package net.roseindia;
public class BeanSecond {
private BeanFirst bean1;
public BeanSecond() {
super();
System.out.println("BeanSecond constructor is called");
}
public void setBean1(BeanFirst bean1) {
this.bean1 = bean1;
System.out.println("Setter method is called");
}
public void init() {
System.out.println("Init of BeanSecond method called");
}
}
|
BeanThird.java
package net.roseindia;
public class BeanThird {
private BeanFirst bean1;
public BeanThird() {
super();
System.out.println("Construictor Called for BeanThird");
}
public void setBean1(BeanFirst bean1) {
this.bean1 = bean1;
System.out.println("Setter method for BeanFirst called in BeanThird");
}
public void init() {
System.out.println("Init Called for BeanThird");
}
}
|
context.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="bean1" class="net.roseindia.BeanFirst" init-method="init">
<constructor-arg ref="bean2"></constructor-arg>
<property name="bean3" ref="bean3"></property>
</bean>
<bean id="bean2" class="net.roseindia.BeanSecond" init-method="init">
</bean>
<bean id="bean3" class="net.roseindia.BeanThird" init-method="init">
<property name="bean1" ref="bean1"></property>
</bean>
</beans>
|
When you run this application it will display output as shown below:
BeanSecond constructior is called
Init of BeanSecond method called
BeanFirst constructor is called
Construictor Called for BeanThird
Setter method for BeanFirst called in BeanThird
Init Called for BeanThird
Setter method called for the BeanThird
Init of BeanFirst method called |
Download this example code