java – Destroy方法在spring框架中不起作用[复制]
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – Destroy方法在spring框架中不起作用[复制],小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3982字,纯文字阅读大概需要6分钟。
内容图文
![java – Destroy方法在spring框架中不起作用[复制]](/upload/InfoBanner/zyjiaocheng/700/5efe03b325404fdb8e71e8700d83c916.jpg)
参见英文答案 > when is a spring beans destroy-method called? 6个
编辑:
这个问题与When does destroy method is called不同,因为我正在调用context.registerShutdownHook并且我的bean正在变得如此,你可以从日志中看到.我的问题是春天不是在调用我的方法.在问这里之前我已经检查了这个问题.
我正在使用spring框架在我的应用程序中配置优雅的销毁.当我运行程序时,它不会调用bean.xml中指定的destory方法.请帮帮我,我做错了什么.
这是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
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();
}
}
产量
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
修订:
我已经尝试了close和registerShutdownHook()来关闭上下文,但没有一个工作.
解决方法:
对于范围原型的bean,不调用Destroy方法.这是因为上下文不跟踪原型范围对象(如果是这样,它将导致内存泄漏,因为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.
内容总结
以上是互联网集市为您收集整理的java – Destroy方法在spring框架中不起作用[复制]全部内容,希望文章能够帮你解决java – Destroy方法在spring框架中不起作用[复制]所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。