什么时候会在春天调用bean的destroy-method?

时间:2015-03-02 11:02:48

标签: java spring

这是用于存储特定用户详细信息的bean类。它有两个名为init和destroy的方法。当bean被启动和销毁时需要调用它。

package org.springframework.HelloWorld.beans;

public class User {
    private int userId;
    private String userName;
    private String address;
    private String mobileNumber;

    public User() {
        super();
    }

    public User(int userId, String userName, String address, String mobileNumber) {
        super();
        this.userId = userId;
        this.userName = userName;
        this.address = address;
        this.mobileNumber = mobileNumber;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getMobileNumber() {
        return mobileNumber;
    }

    public void setMobileNumber(String mobileNumber) {
        this.mobileNumber = mobileNumber;
    }

    @Override
    public String toString() {
        return "User [userId=" + userId + ", userName=" + userName
                + ", address=" + address + ", mobileNumber=" + mobileNumber
                + "]";
    }

    public void init(){
        System.out.println("Initialized");
    }

    public void destroy(){
        System.out.println("Destroyed");
    }
}

这是我使用的主要方法。在这里我从Beans.xml文件中获取User对象。

public static void main(String[] args) {
        @SuppressWarnings("resource")
        ApplicationContext ctx = new ClassPathXmlApplicationContext("Beans.xml");
        User user = (User) ctx.getBean("user");
        System.out.println(user);
        user.setUserName("Gnanadurai Asudoss");
        user.setAddress("Chxxxxx");
        System.out.println(user);
        user = (User) ctx.getBean("user");
        System.out.println(user);
        user=null;
    }

下面是Beans.xml文件,我在这里提到了bean,init方法和destroy方法。



<bean id="user" class="org.springframework.HelloWorld.beans.User" init-method="init" destroy-method="destroy">
		<property name="userId" value="1"></property>
		<property name="userName" value="Gnanadurai"></property>
		<property name="address" value="Saxxxx"></property>
		<property name="mobileNumber" value="xxxxxxxx49"></property>
	</bean>
&#13;
&#13;
&#13;

这是我得到的输出。在这里你可以看到已经调用了Initialized方法,并且没有调用Destroy方法。

Initialized
User [userId=1, userName=Gnanadurai, address=Saxxx, mobileNumber=xxxxxxxx49]
User [userId=1, userName=Gnanadurai Asudoss, address=Chxxxx, mobileNumber=xxxxxxxx49]
User [userId=17505, userName=Gnanadurai Asudoss, address=Chxxxx, mobileNumber=xxxxxxxx49]

所以我不知道什么时候会调用destroy方法..你能告诉我什么时候会调用它。举个例子。

...谢谢

2 个答案:

答案 0 :(得分:1)

根据这篇文章,你需要close你的applicationContext,在这个动作之后你的毁灭将被召唤。 Related SO question

//Getting application context
ApplicationContext context = new ClassPathXmlApplicationContext(beansXML); 

//cleaning context
((ClassPathXmlApplicationContext) context).close(); 

答案 1 :(得分:1)

您需要在JVM上为应用程序上下文注册shutdown或关闭应用程序上下文。您可以使用close()registerShutdownHook()方法实现此目的,如下所示。有关更多详细信息,请参阅Javadoc here

ctx.close();
ctx.registerShutdownHook();