在spring中关闭beanfactory,调用destroy()方法

时间:2014-08-13 13:23:29

标签: spring

我正在使用beanfactory容器来获取bean对象,并且我编写了两个方法init()destroy()来在bean初始化和销毁​​时实现一些逻辑。我在springconfiguration文件中配置了。设置依赖项后会调用init(),但调用destroy()方法时,如何关闭beanfactory。我在应用程序上下文关闭时找到的地方然后调用destroy(),然后在beanfactory调用时调用它?

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(主类)

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>

2 个答案:

答案 0 :(得分:1)

您必须依赖其他界面或实施。

e.g。

((ConfigurableBeanFactory) factory).destroySingletons();

如果您想自动执行此操作,则应升级到任何ApplicationContext,其中显式任何ConfigurableApplicationContext都有registerShutdownHook()方法在JVM出口处自动取消注册Bean

在您的情况下,ClassPathXmlApplicationContextFileSystemXmlApplicationContext

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(); 
}

}

答案 1 :(得分:0)

您可以按照 @Niels Bech

的建议将BeanFactory转换为ApplicationContext的Any实现

但是,在更改bean的范围之前,您的destroy方法将无效,因为:

你的豆是原型范围。 范围原型的bean不会调用Destroy方法。

请参阅Spring Docs中的以下内容。另请参阅此SO post

  

在部署时需要注意一件非常重要的事情   bean在原型范围内,因为bean的生命周期发生了变化   略。 Spring不管理原型的整个生命周期   bean:容器实例化,配置,装饰等   组装原型对象,将其交给客户端,然后没有   进一步了解该原型实例。这意味着,虽然   初始化生命周期回调方法将被调用   对象无论范围如何,在原型的情况下,任何配置   不会调用销毁生命周期回调。它是   客户端代码负责清理原型范围对象   并释放原型bean所有的昂贵资源   坚持。 (一种可能的方式来释放Spring容器   原型范围bean使用的资源是通过使用a   自定义bean后处理器,它将保存对bean的引用   需要清理的。)