如果在原型bean上定义了相同的方法,Spring不会调用singleton bean的destroy方法

时间:2016-04-24 13:07:34

标签: java spring

所以我有一个像这样的SingletonMessage类

 public class SingletonMessage {
    private String message;
    public void destroyBean() {
        System.out.println("Destroying bean");
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public String getMessage() {
        return this.message;
    }}

我给了这样的原型bean

  public class PrototypeMessage {

    private String message;

    public void setMessage(String message) {
        this.message = message;
    }
    public void destroyBean() {
        System.out.println("Destroying bean");
    }
    public String getMessage() {
        return this.message;
    }}

我的beans.xml文件是这样的

<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"
default-init-method="initMessage"
default-destroy-method="destroyBean">
    <bean id="smessage" class="com.spring.beans.SingletonMessage" scope="singleton">
        <property name="message" value="Hello World!"/>
    </bean>

    <bean name="pmessage" class="com.spring.beans.PrototypeMessage" scope="prototype">
        <property name="message" value="Hello"/>
    </bean>
</beans>

当我将destroyBean方法保存在PrototypeMessage类中时,我的控制台上没有一个Destroyying bean。但是当我从PrototypeBean中删除它并仅将其保存在SingletonBean中时,它会显示Destroying bean。这是否与Spring容器不处理原型范围bean的生命周期

有关

0 个答案:

没有答案