Gnanadurai Asudoss
Gnanadurai Asudoss

Reputation: 287

When does bean's destroy-method will be called in spring?

This is the bean class it is used to store the details about the particular user. It has two method named init and destroy. which needs to be called when bean is initilized and destroyed.

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");
    }
}

This is the main method i have used. here i'm getting the User object from Beans.xml file.

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;
    }

Below is the Beans.xml file here i have mentioned bean, init method and destroy method.

<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>

This is the output i'm getting. Here you can see Initialized method is beeing called and Destroy method is not called.

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]

So I dont know when destroy method will be called.. Could you tell me when it will be called. Give me an example.

Thanks...

Upvotes: 1

Views: 2178

Answers (2)

madteapot
madteapot

Reputation: 2294

You need to register shutdown for the application context on JVM or close the application context. You can use close() or registerShutdownHook() method for that purpose as shown below. refer to Javadoc here for more details.

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

Upvotes: 1

erhun
erhun

Reputation: 3647

According to this post you need to close your applicationContext, after this action your destroy will be called. Related SO question

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

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

Upvotes: 1

Related Questions