1

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>

user3308901
  • 31
  • 2
  • 4

2 Answers2

1

You have to depend on another interface or the implementation.

e.g.

((ConfigurableBeanFactory) factory).destroySingletons();

if you want to do it automatically, you should upgrade to any ApplicationContext, where explicitly any ConfigurableApplicationContext have a registerShutdownHook() method to automatically deregister beans on JVM exit

In your case either the ClassPathXmlApplicationContext or the FileSystemXmlApplicationContext

e.g.

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");

    ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml");
    factory.registerShutdownHooks();
    Travel tv=(Travel)factory.getBean("travel");
    tv.travelCheck(); 
}

}

Niels Bech Nielsen
  • 4,777
  • 1
  • 21
  • 44
0

You can convert your BeanFactory to Any implementation of ApplicationContext as suggested by @Niels Bech

But Your destroy method will not work until you change scope of your beans because:

Your Beans are of Prototype scope. Destroy method is not called for beans of scope prototype.

Refer following from Spring Docs. Also refer this SO post.

There is one quite important thing to be aware of when deploying a bean in the prototype scope, in that the lifecycle of the bean changes slightly. Spring does not manage the complete lifecycle of a prototype bean: the container instantiates, configures, decorates and otherwise assembles a prototype object, hands it to the client and then has no further knowledge of that prototype instance. This means that while initialization lifecycle callback methods will be called on all objects regardless of scope, in the case of prototypes, any configured destruction lifecycle callbacks will not be called. It is the responsibility of the client code to clean up prototype scoped objects and release any expensive resources that the prototype bean(s) are holding onto. (One possible way to get the Spring container to release resources used by prototype-scoped beans is through the use of a custom bean post-processor which would hold a reference to the beans that need to be cleaned up.)

Community
  • 1
  • 1
Pramod S. Nikam
  • 4,271
  • 4
  • 38
  • 62