Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Destroy method is not working in spring framework [duplicate]

Tags:

java

spring

Edit: This question is not same as When does destroy method is called because I am properly calling context.registerShutdownHook and my bean is getting destory as you can see from the logs. My problem is spring is not calling my method. I have checked this question before asking here.

I am configuring the graceful destroy in my application using spring framework. When I am running the program it is not calling the destory method specified in the bean.xml. Please help me what am I doing wrong.

here is SSCCE

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.xsd">

    <bean id="helloworld" class="com.hello.pojo.HelloWorld" 
          scope="prototype" init-method="init" destroy-method="destroy">
    </bean>

</beans>

HelloWord.java

package com.hello.pojo;

public class HelloWorld {


    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void init(){
        System.out.println("Bean initializating is in progress");
    }

    public void printMessage(){
        System.out.println("Your message: "+getMessage());
    }
    public void destroy(){
        System.out.println("Bean is being destroyed");
    }

}

MainApp.java

package com.main;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hello.pojo.HelloWorld;


public class MainApp {

    public static void main(String[]args){
        AbstractApplicationContext context = new ClassPathXmlApplicationContext("Bean.xml");
        HelloWorld objA = (HelloWorld) context.getBean("helloworld");
        objA.setMessage("I am Object A");
        objA.printMessage();
        context.registerShutdownHook();
    }

}

Output

May 27, 2013 11:59:14 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@e9028874: startup date [Mon May 27 23:59:14 EDT 2013]; root of context hierarchy
May 27, 2013 11:59:14 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Bean.xml]
Bean initializating is in progress
Your message: I am Object A
May 27, 2013 11:59:14 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@63390b47: defining beans [helloworld]; root of factory hierarchy
May 27, 2013 11:59:14 PM org.springframework.context.support.ClassPathXmlApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@e9028874: startup date [Mon May 27 23:59:14 EDT 2013]; root of context hierarchy
May 27, 2013 11:59:14 PM org.springframework.beans.factory.support.DefaultListableBeanFactory destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@63390b47: defining beans [helloworld]; root of factory hierarchy

Amendment: I have tried close and registerShutdownHook() to close the context and none of them works.

like image 771
Ashish Avatar asked May 28 '13 03:05

Ashish


1 Answers

Destroy method is not called for beans of scope prototype. This is because the context doesn't keep track of the prototype scope objects (if it does, it will cause a memory leak as spring doesn't know when to dispose it).

Details from the spring documentation.

Spring reference documentation

There is one quite important thing to be aware of when deploying a bean in the prototype scope, in that the lifecycle of the bean changes slightly. 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. This means that while initialization lifecycle callback methods will be called on all objects regardless of scope, in the case of prototypes, any configured destruction lifecycle callbacks will not be called.

like image 107
gkamal Avatar answered Oct 03 '22 00:10

gkamal