我的bean上没有调用Destroy函数,即使我认为我已经正确配置了所有内容。请在下面找到相关代码:
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
// Create first instane of HelloWorld
HelloWorld obj = (HelloWorld) context.getBean("helloWorldclass");
obj.getMessage();
obj.setMessage("Changed text");
obj.getMessage();
// Create another instance
HelloWorld objB = (HelloWorld) context.getBean("helloWorldclass");
objB.getMessage();
context.registerShutdownHook();
}
}
和
package com.tutorialspoint;
public class HelloWorld {
// Field
private String message;
// Message setter
public void setMessage(String message) {
this.message = message;
}
// Message getter
public void getMessage() {
if (message != null && !message.isEmpty()) {
System.out.println("Your Message : " + message);
} else {
System.out.println("Your Message is empty");
}
}
// Initialize
public void init() {
System.out.println("Bean is going through init.");
}
// destroy
public void destroy() {
System.out.println("Bean will destroy now.");
}
}
和
<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-3.0.xsd">
<bean id="helloWorldclass" class="com.tutorialspoint.HelloWorld"
scope="prototype" init-method="init" destroy-method="destroy">
</bean>
</beans>
我做错了什么?我没有看到来自destroy方法的消息。