Редактировать: Этот вопрос не такой, как When does destroy method is called, потому что я правильно звоню context.registerShutdownHook
, и мой боб становится страшным, как вы можете видеть из журналов. Моя проблема в том, что весна не вызывает мой метод. Я проверил этот вопрос, прежде чем спрашивать здесь.Метод разрушения не работает в весеннем каркасе
Я настраиваю изящное уничтожение в своем приложении с использованием Spring Framework. Когда я запускаю программу, он не вызывает метод destory, указанный в bean.xml. Пожалуйста, помогите мне, что я делаю неправильно.
здесь 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[email protected]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.s[email protected]63390b47: defining beans [helloworld]; root of factory hierarchy
May 27, 2013 11:59:14 PM org.springframework.context.support.ClassPathXmlApplicationContext doClose
INFO: Closing org[email protected]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.s[email protected]63390b47: defining beans [helloworld]; root of factory hierarchy
Поправка: Я попытался close
и registerShutdownHook()
закрыть контекст, и ни один из них не работает.
@MadProgrammer Я думаю, что мы можем настроить метод с любым именем в bean.xml, если оба имеют одинаковое имя. Хотя я и пытался с уничтожением, и он не работает – Ashish
@MadProgrammer Я думаю, вы имеете в виду 'destroy', а не' destory';) – Vulcan
@ Vulcan Я думаю, что я имею в виду пиво;) – MadProgrammer