java 销毁方法在 spring 框架中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16783552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Destroy method is not working in spring framework
提问by Ashish
Edit:
This question is not same as When does destroy method is calledbecause 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.
编辑:这个问题与何时调用 destroy 方法不同,因为我正在正确调用context.registerShutdownHook
并且我的 bean 正在销毁,正如您从日志中看到的那样。我的问题是 spring 没有调用我的方法。在问这里之前,我已经检查过这个问题。
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.
我正在使用 spring 框架在我的应用程序中配置优雅的销毁。当我运行程序时,它没有调用 bean.xml 中指定的 destory 方法。请帮助我我做错了什么。
here is SSCCE
这是SSCCE
Bean.xml
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
你好字
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
主应用程序
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();
}
}
Output
输出
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
Amendment:
I have tried close
and registerShutdownHook()
to close the context and none of them works.
修订:我曾尝试close
和registerShutdownHook()
关闭的背景下,没有他们的工作。
回答by gkamal
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).
不为范围原型的 bean 调用销毁方法。这是因为上下文不跟踪原型范围对象(如果这样做,它将导致内存泄漏,因为 spring 不知道何时处置它)。
Details from the spring documentation.
来自 spring 文档的详细信息。
Spring reference documentation
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.
在原型范围内部署 bean 时需要注意一件非常重要的事情,即 bean 的生命周期略有变化。Spring 不管理原型 bean 的完整生命周期:容器实例化、配置、装饰和以其他方式组装原型对象,将其交给客户端,然后不再了解该原型实例。这意味着虽然初始化生命周期回调方法将在所有对象上调用,而不管范围如何,在原型的情况下,任何配置的销毁生命周期回调都不会被调用。