Destroy方法在spring框架中不起作用 [英] Destroy method is not working in spring framework

查看:291
本文介绍了Destroy方法在spring框架中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:
此问题与何时不同是否会调用destroy方法因为我正在调用 context.registerShutdownHook 并且我的bean正在从日志中看到。我的问题是春天不是在调用我的方法。我在问这里之前已经检查了这个问题。

This question is not same as When does destroy method is called because I am properly calling context.registerShutdownHook and my bean is getting destory as you can see from the logs. My problem is spring is not calling my method. I have checked this question before asking here.

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

I am configuring the graceful destroy in my application using spring framework. When I am running the program it is not calling the destory method specified in the bean.xml. Please help me what am I doing wrong.

这里是 SSCCE

Bean.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>

HelloWord.java

HelloWord.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");
    }

}

MainApp.java

MainApp.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

修正案:
我试过关闭 registerShutdownHook()关闭上下文,但没有一个工作。

Amendment: I have tried close and registerShutdownHook() to close the context and none of them works.

推荐答案

不为范围原型的bean调用Destroy方法。这是因为上下文没有跟踪原型范围对象(如果是这样,它会导致内存泄漏,因为spring不知道何时处理它)。

Destroy method is not called for beans of scope prototype. This is because the context doesn't keep track of the prototype scope objects (if it does, it will cause a memory leak as spring doesn't know when to dispose it).

spring文档中的详细信息。

Details from the spring documentation.

春天参考文档


有一个非常重要在原型范围内部署
bean时需要注意的事项是,bean的生命周期会稍微改变
。 Spring不管理原型
bean的完整生命周期:容器实例化,配置,装饰,否则
组装原型对象,交给客户端,然后没有
进一步了解那个原型实例。这意味着虽然
初始化生命周期回调方法将在所有
对象上调用,而不考虑范围,但在原型的情况下,将不会调用任何已配置的
销毁生命周期回调。

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.

这篇关于Destroy方法在spring框架中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆