java destory_java – Destroy方法在spring框架中不起作用[...

参见英文答案 > when is a spring beans destroy-method called?????????????????????????????????????6个

编辑:

这个问题与When does destroy method is called不同,因为我正在调用context.registerShutdownHook并且我的bean正在变得如此,你可以从日志中看到.我的问题是春天不是在调用我的方法.在问这里之前我已经检查了这个问题.

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

Bean.xml

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">

scope="prototype" init-method="init" destroy-method="destroy">

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()来关闭上下文,但没有一个工作.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值