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

beanfactory在spring中关闭以调用destroy()方法,spring,Spring,我使用beanfactory容器来获取bean对象,并编写了两个方法init()和destroy(),以在bean初始化和销毁时实现一些逻辑。我在springconfiguration文件中进行了配置init(),但是当调用destroy()方法时,如何关闭beanfactory。在某个地方,我发现当应用程序上下文关闭时,调用了destroy(),然后在beanfactory的情况下调用了它 java package sringcoreexamples; public class Car {

我使用
beanfactory
容器来获取bean对象,并编写了两个方法
init()
destroy()
,以在bean初始化和销毁时实现一些逻辑。我在
springconfiguration
文件中进行了配置<设置依赖项后会调用code>init(),但是当调用
destroy()
方法时,如何关闭
beanfactory
。在某个地方,我发现当应用程序上下文关闭时,调用了
destroy()
,然后在
beanfactory的情况下调用了它

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...............");
    }
}
java(主类)

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>


您必须依赖于另一个接口或实现

e、 g

如果要自动执行此操作,应该升级到任何
ApplicationContext
,其中任何
ConfigurableApplicationContext
都有一个
RegisterShotDownhook()
方法,可以在JVM退出时自动注销bean

在您的情况下,可以使用
ClassPathXmlApplicationContext
FileSystemXmlApplicationContext

e、 g


}

您可以将BeanFactory转换为

但除非您更改bean的范围,否则销毁方法将无法工作,因为:

您的bean属于原型范围范围bean原型不调用销毁方法。

请参阅以下内容。也请参阅此

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


请出示您的密码。阅读。我分享了我的代码。请帮助我
<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>
((ConfigurableBeanFactory) factory).destroySingletons();
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(); 
}