销毁方法在弹簧框架中不起作用

2022-09-03 03:19:36

编辑:这个问题与何时调用销毁方法不同,因为我正在正确调用,并且我的Bean正在变得令人沮丧,正如您从日志中看到的那样。我的问题是春天没有调用我的方法。在问这里之前,我已经检查了这个问题。context.registerShutdownHook

我正在使用spring框架在我的应用程序中配置优雅的销毁。当我运行程序时,它不会调用bean中指定的destory方法.xml。请帮帮我做错了什么。

这里是SSCCE

豆.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="helloworld" class="com.hello.pojo.HelloWorld" 
          scope="prototype" init-method="init" destroy-method="destroy">
    </bean>

</beans>

你好词.java

package com.hello.pojo;

public class HelloWorld {


    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void init(){
        System.out.println("Bean initializating is in progress");
    }

    public void printMessage(){
        System.out.println("Your message: "+getMessage());
    }
    public void destroy(){
        System.out.println("Bean is being destroyed");
    }

}

主应用程序.java

package com.main;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hello.pojo.HelloWorld;


public class MainApp {

    public static void main(String[]args){
        AbstractApplicationContext context = new ClassPathXmlApplicationContext("Bean.xml");
        HelloWorld objA = (HelloWorld) context.getBean("helloworld");
        objA.setMessage("I am Object A");
        objA.printMessage();
        context.registerShutdownHook();
    }

}

输出

May 27, 2013 11:59:14 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@e9028874: startup date [Mon May 27 23:59:14 EDT 2013]; root of context hierarchy
May 27, 2013 11:59:14 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Bean.xml]
Bean initializating is in progress
Your message: I am Object A
May 27, 2013 11:59:14 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@63390b47: defining beans [helloworld]; root of factory hierarchy
May 27, 2013 11:59:14 PM org.springframework.context.support.ClassPathXmlApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@e9028874: startup date [Mon May 27 23:59:14 EDT 2013]; root of context hierarchy
May 27, 2013 11:59:14 PM org.springframework.beans.factory.support.DefaultListableBeanFactory destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@63390b47: defining beans [helloworld]; root of factory hierarchy

修正:我试图关闭上下文,但没有一个起作用。closeregisterShutdownHook()


答案 1

销毁方法不要求用于范围原型的bean。这是因为上下文不会跟踪原型作用域对象(如果是这样,它将导致内存泄漏,因为 spring 不知道何时处置它)。

详细信息来自春季文档。

弹簧参考文档

在原型范围内部署 Bean 时,有一件非常重要的事情需要注意,即 Bean 的生命周期略有变化。Spring不管理原型bean的整个生命周期:容器实例化,配置,装饰和以其他方式组装原型对象,将其交给客户端,然后对该原型实例没有进一步的了解。这意味着,虽然初始化生命周期回调方法将在所有对象上调用,而不管范围如何,但对于原型,则不会调用任何配置的销毁生命周期回调。


答案 2

推荐