is there something like @predestroy in the spring as in the castle windsor
Asked Answered
H

4

5

Anything like @PreDestroy in the spring-framework?

Hoagland answered 7/5, 2011 at 8:35 Comment(1)
C
17

If you define a bean that implements the DisposableBean interface then Spring will call the

void destroy() throws Exception;

method before destrying the bean.

That's one way, the other is when your bean doesn't have to implement the given interface. In one of yours ConfigurationSupport classes your bean has to be defined as as pulic method with the @Bean annotation.

   @Bean (destroyMethod="yourDestroyMethod")
   public YourBean yourBean() {
      YourBean yourBean = new YourBean();

      return yourBean;
   }

The method "yourDestroyMethod" has to be defined in YourBean.class and then Spring will call it before destroying the bean.

For more info see the Spring documentation: Destruction callbacks

UPDATE

The third way... I would even say the better way would be to specifiy "init-method" and "destroy-method" of your bean... like this: mkyong.com/spring/spring-init-method-and-destroy-method-example

This solves the problem ot third-party dependency beans, and liberates the the code unnecessary Spring interfaces..

Choroid answered 9/5, 2011 at 8:44 Comment(4)
I didn't know but I've found that there are @PostConstruct and @PreDestroy annotations. See: static.springsource.org/spring/docs/3.0.x/reference/…Choroid
I find @PostConstruct and @PreDestroy to be much clearer than the other ways of doing this sort of thing; it minimizes magic.Munroe
This is what I was looking for! The documentation talks about DisposableBean and @PreDestroy but I was wondering how to specify this for third-party classes such as org.apache.commons.dbcp.BasicDataSource where one cannot touch their code. Thanks very much!Zia
The other way... I would even say the better way would be to specifiy "init-method" and "destroy-method" of your bean... like this: mkyong.com/spring/spring-init-method-and-destroy-method-example This solves the problem ot third-party dependency beans, and liberates the the code unnecessary Spring interfaces...Choroid
N
7

There are 3 ways to do that.

@PreDestroy tag

destroy-method in xml

DisposableBean interface as stated above

My favorite is the @PreDestroy method.

To do that u need:

In application-context.xml add the following schema:

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="shutDownBean" class="spring.ShutDownBean" />
    <context:annotation-config/>
</beans>

The context:annotation-config/ makes the @PreDestroy and the @PostDestroy tags available. Now lets say that you have the ShutDownBean that you want to run some code when the shutdown callback is called. The <bean id="shutDownBean" class="spring.ShutDownBean" /> part registers the bean.

import javax.annotation.PreDestroy;

public final  class ShutDownBean {

    @PreDestroy
    public static void shutDownMethod() {
        System.out.println("Shutting down!");
    }

}

Now you are done.

If you have a desktop application then to use the @PreDestroy annotation you need to close it like this:

AbstractApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("application-context.xml");
applicationContext.registerShutdownHook();

note: AbstractApplicationContext has the implementation of registerShutdownHook() so this is the minimum class you can use.

Also you can use the destroy-method tag for classes you do not control their implementation. For example you can add this in your applcation-context.xml:

<bean id = "dataSource" 
      class = "org.apache.commons.dbcp.BasicDataSrouce"
      destroy-method = "close">

The destroy-method value can have any visibility but needs to have no arguments.

Hope this helps!

Nicotiana answered 6/6, 2011 at 16:1 Comment(0)
M
3

You mean like annotating a method with the standard JDK @PreDestroy? That's common enough in Spring, and usually better than using a destroy-method attribute on the bean declaration in XML. All you have to do is include

<context:annotation-config/> 

In your configuration file and Spring handles the rest.

Munroe answered 7/5, 2011 at 9:26 Comment(0)
J
0

there's standard .NET IDisposable.Dispose() method. I don't know Spring but from quick googling it seems that @predestroy is pretty much the same concept.

Jp answered 7/5, 2011 at 13:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.