开发者

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

开发者 https://www.devze.com 2023-03-04 03:24 出处:网络
An开发者_如何学Goything like @PreDestroy in the spring-framework?If you define a bean that implements the DisposableBean interface then Spring will call the

An开发者_如何学Goything like @PreDestroy in the spring-framework?


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


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!


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号