Spring bean destroy-method,singleton和prototype范围 [英] Spring bean destroy-method , singleton and prototype scopes
问题描述
我是Spring框架的新手,从一些教程开始学习它。
I am new to the spring framework, started with some tutorials to learn it.
我有以下文件,
#MainProgram.java
# MainProgram.java
package test.spring;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainProgram {
public static void main(String[] args) {
AbstractApplicationContext context =
new ClassPathXmlApplicationContext("Bean.xml");
HelloSpring obj = (HelloSpring) context.getBean("helloSpring");
obj.setMessage("My message");
obj.getMessage();
context.registerShutdownHook();
}
}
#HelloSpring.java
# HelloSpring.java
package test.spring;
public class HelloSpring {
private String message;
public void setMessage(String message){
this.message = message;
System.out.println("Inside setMessage");
}
public void getMessage(){
System.out.println("Your Message : " + this.message);
}
public void xmlInit() {
System.out.println("xml configured initialize");
}
public void xmlDestroy() {
System.out.println("xml configured destroy");
}
}
#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-3.0.xsd">
<bean id="helloSpring" class="test.spring.HelloSpring"
scope="prototype" init-method="xmlInit" destroy-method="xmlDestroy">
</bean>
</beans>
当我拿 scope =singleton
我的输出是:
xml configured initialize
Inside setMessage
Your Message : My message
xml configured destroy
当我拿 scope =prototype
我的输出是:
xml configured initialize
Inside setMessage
Your Message : My message
xmlDestroy()
使用<$调用方法c $ c> singleton 范围bean但没有原型
请帮助我以下,
xmlDestroy()
method is called with singleton
scope bean but not with prototype
kindly help me for the following ,
这是对的吗?如果是这样,可能的原因是什么?
Is this correct? if so, what would be possible reasons?
我也有一些问题,如
有什么区别或者
之间的关系ApplicationContext,AbstractApplicationContext和ClassPathXmlApplicationContext
推荐答案
xmlDestroy()
使用单例范围bean调用方法但不使用原型调用方法,因为
xmlDestroy()
method is called with singleton scope bean but not with prototype because
Spring无法管理整个生命周期原型bean:容器实例化,配置,装饰和组装原型对象,将其交给客户端,然后不再了解该原型实例。为了释放资源,尝试实现自定义bean后处理器。
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. For releasing resources try to implement a custom bean post processor.
与弹簧容器管理完整生命周期的单件bean不同
Unlike singleton beans where the spring container manages the complete life-cycle
你可以看看这个关于不同背景之间差异的基本教程
You can have a look at this basic tutorial for differences between different contexts
参考文档
这篇关于Spring bean destroy-method,singleton和prototype范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!