Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when is a spring beans destroy-method called?

Tags:

I have put a sysout statement in the "destroy-method" for a bean. When i run a sample code, the sysout is not getting output. Does that mean the destroy-method is not getting called ?

The Test Class:

  package spring.test;    import org.springframework.context.ApplicationContext;   import org.springframework.context.support.ClassPathXmlApplicationContext;    public class InitTest {     public static void main(String[] args) {         ApplicationContext ctx = new ClassPathXmlApplicationContext("InitTestContext.xml");         InitTestBean bean = (InitTestBean)ctx.getBean("InitTestBean");         bean.display();     }   } 

The Bean

  package spring.test;    public class InitTestBean {     private String prop1;     private String prop2;      public InitTestBean(String prop1, String prop2) {         System.out.println("Instantiating InitTestBean");         this.prop1 = prop1;         this.prop2 = prop2;     }      public void setProp1(String prop1) {         System.out.println("In setProp1");         this.prop1 = prop1;     }      public void setProp2(String prop2) {         System.out.println("In setProp2");         this.prop2 = prop2;     }      public String getProp1() {         return prop1;     }      public String getProp2() {         return prop2;     }      public void display() {         System.out.println("Prop1 is " + prop1);         System.out.println("Prop2 is " + prop2);     }      public void initialize(){         System.out.println("In initialize");         this.prop1 = "init-prop1";         this.prop2 = "init-prop2";     }      public void teardown() {         System.out.println("In teardown");         this.prop1 = null;         this.prop2 = null;     }   } 

The Config file:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:aop="http://www.springframework.org/schema/aop"     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">      <bean id="InitTestBean" class="spring.test.InitTestBean" init-method="initialize" destroy-method="teardown">         <constructor-arg value="Prop1" />         <constructor-arg value="Prop2" />         <property name="prop1" value="setProp1"/>         <property name="prop2" value="setProp2"/>     </bean>  </beans> 
like image 477
java_geek Avatar asked Dec 16 '10 11:12

java_geek


People also ask

When bean destroy-method is called?

display() as destroy-method is valued in the bean definition. The default scope with which bean is created is singleton hence, invoking the factory. destroySingletons() will call the method mentioned in the destroy-method . Follow this answer to receive notifications.

How can call destroy-method in spring bean?

destroy-method is bean attribute using which we can assign a custom bean method that will be called just before the bean is destroyed. To use the destroy-method attribute, we do as follows. Here myPreDestroy() method will be defined in the Student class. Spring will call this method just before destroying the bean.

What is destroy-method?

Destroy () method, is Called to allow your servlet to clean up any resources. destroy. Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. This method is only called once all threads within the servlet's service method have exited or after a timeout period has passed ...

What is the purpose of init-method and destroy-method in spring bean?

The init-method attribute specifies a method that is to be called on the bean immediately upon instantiation. Similarly, destroymethod specifies a method that is called just before a bean is removed from the container.


2 Answers

Your example doesn't work because you're not shutting down the appcontext, you're just letting the program terminate.

Call close() on the context, and you'll see the bean destroy-methods being called.

like image 99
skaffman Avatar answered Sep 29 '22 19:09

skaffman


It may be too late for the OP, but if someone is still looking for it...

The close method is in AbstractApplicationContext and not ApplicationContext, also another way is to use ctx.registerShutdownHook() instead of ctx.close() for obvious reasons that while running Junits you might want to close the context but not while in production environment so let Spring decide on when to close it.

like image 43
Life Avatar answered Sep 29 '22 20:09

Life