I am using beanfactory
container to get the bean objects, And I wrote two methods init()
and destroy()
to implement some logic at the time of bean initialization and destruction. And I configured in springconfiguration
file. init()
is called after dependencies are set, but when destroy()
method is called, how can I close the beanfactory
. Somewhere I find when application context is closed then destroy()
is called, then in case of beanfactory
when it is called?
Car.java
package sringcoreexamples;
public class Car {
public void start(){
System.out.println(" car started.....");
}
}
Travel.java
package sringcoreexamples;
public class Travel {
public Car car;
public void init(){
System.out.println("this is bean initialization method.......");
}
public void setCar(Car car) {
this.car = car;
}
public void travelCheck(){
car.start();
}
public void destroy(){
System.out.println("this is bean destroy method...............");
}
}
SringCoreExamples.java(Main class)
public class SringCoreExamples {
public static void main(String[] args) {
// TODO code application logic here
System.out.println("this is main method............");
Resource resource=new ClassPathResource("applicationContext.xml");
BeanFactory factory=new XmlBeanFactory(resource);
Travel tv=(Travel)factory.getBean("travel");
tv.travelCheck();
}
}
applicationContext.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-3.2.xsd">
<bean id="car" class="sringcoreexamples.Car"/>
<bean id="travel" class="sringcoreexamples.Travel" init-method="init" destroy-method="destroy" scope="prototype">
<property name="car" ref="car"/>
</bean>