Spring beans 销毁方法什么时候被调用?
我已经在 bean 的“destroy-method”中放置了一条 sysout 语句。当我运行示例代码时,系统输出没有获得输出。这是否意味着销毁方法没有被调用?
测试类:
package spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class InitTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("InitTestContext.xml");
InitTestBean bean = (InitTestBean)ctx.getBean("InitTestBean");
bean.display();
}
}
Bean
package spring.test;
public class InitTestBean {
private String prop1;
private String prop2;
public InitTestBean(String prop1, String prop2) {
System.out.println("Instantiating InitTestBean");
this.prop1 = prop1;
this.prop2 = prop2;
}
public void setProp1(String prop1) {
System.out.println("In setProp1");
this.prop1 = prop1;
}
public void setProp2(String prop2) {
System.out.println("In setProp2");
this.prop2 = prop2;
}
public String getProp1() {
return prop1;
}
public String getProp2() {
return prop2;
}
public void display() {
System.out.println("Prop1 is " + prop1);
System.out.println("Prop2 is " + prop2);
}
public void initialize(){
System.out.println("In initialize");
this.prop1 = "init-prop1";
this.prop2 = "init-prop2";
}
public void teardown() {
System.out.println("In teardown");
this.prop1 = null;
this.prop2 = null;
}
}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="InitTestBean" class="spring.test.InitTestBean" init-method="initialize" destroy-method="teardown">
<constructor-arg value="Prop1" />
<constructor-arg value="Prop2" />
<property name="prop1" value="setProp1"/>
<property name="prop2" value="setProp2"/>
</bean>
</beans>
I have put a sysout statement in the "destroy-method" for a bean. When i run a sample code, the sysout is not getting output. Does that mean the destroy-method is not getting called ?
The Test Class:
package spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class InitTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("InitTestContext.xml");
InitTestBean bean = (InitTestBean)ctx.getBean("InitTestBean");
bean.display();
}
}
The Bean
package spring.test;
public class InitTestBean {
private String prop1;
private String prop2;
public InitTestBean(String prop1, String prop2) {
System.out.println("Instantiating InitTestBean");
this.prop1 = prop1;
this.prop2 = prop2;
}
public void setProp1(String prop1) {
System.out.println("In setProp1");
this.prop1 = prop1;
}
public void setProp2(String prop2) {
System.out.println("In setProp2");
this.prop2 = prop2;
}
public String getProp1() {
return prop1;
}
public String getProp2() {
return prop2;
}
public void display() {
System.out.println("Prop1 is " + prop1);
System.out.println("Prop2 is " + prop2);
}
public void initialize(){
System.out.println("In initialize");
this.prop1 = "init-prop1";
this.prop2 = "init-prop2";
}
public void teardown() {
System.out.println("In teardown");
this.prop1 = null;
this.prop2 = null;
}
}
The Config file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="InitTestBean" class="spring.test.InitTestBean" init-method="initialize" destroy-method="teardown">
<constructor-arg value="Prop1" />
<constructor-arg value="Prop2" />
<property name="prop1" value="setProp1"/>
<property name="prop2" value="setProp2"/>
</bean>
</beans>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您的示例不起作用,因为您没有关闭应用程序上下文,而只是让程序终止。
在上下文中调用
close()
,您将看到正在调用 Bean 销毁方法。Your example doesn't work because you're not shutting down the appcontext, you're just letting the program terminate.
Call
close()
on the context, and you'll see the bean destroy-methods being called.对于OP来说可能为时已晚,但如果有人仍在寻找它...
close 方法位于
AbstractApplicationContext
而不是ApplicationContext
中,另一种方法是使用ctx.registerShutdownHook()
而不是 ctx.close()
明显的原因是,在运行Junit
时,您可能想要关闭上下文,但又不想关闭在生产环境中,让 Spring 决定何时关闭它。It may be too late for the OP, but if someone is still looking for it...
The close method is in
AbstractApplicationContext
and notApplicationContext
, also another way is to usectx.registerShutdownHook()
instead of ctx.close()
for obvious reasons that while runningJunit
s you might want to close the context but not while in production environment so let Spring decide on when to close it.仅当且仅当bean是单例实例时才调用“destroy-method”
请参阅IOC容器
INFO的日志输出:org.springframework.beans.factory.support.DefaultListableBeanFactory中的销毁单例 @1a0ce4c:定义bean [book1];工厂层次结构的根
The "destroy-method" is only called if and only if the bean is a singleton instance
See the log output of the IOC container
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1a0ce4c: defining beans [book1]; root of factory hierarchy
您好,您需要将
ApplicationContext
更改为AbstractApplicationContext
,然后注册到ShutDownhook
,这将销毁您的 bean 并实现 DisposableBean 接口,例如:实现DisposableBean接口
hi you need to change
ApplicationContext
toAbstractApplicationContext
and then register to aShutDownhook
which will destroy your bean and also implement the DisposableBean interface eg:and now implemnt the DisposableBean interface
factory.destroySingletons();
在您的bean.display()
之后,作为destroy-method
在 bean 定义中被赋值。创建 bean 的默认范围是单例,因此调用factory.destroySingletons() 将调用destroy-method 中提到的方法。factory.destroySingletons();
after yourbean.display()
asdestroy-method
is valued in the bean definition. The default scope with which bean is created is singleton hence, invoking thefactory.destroySingletons()
will call the method mentioned in thedestroy-method
.