There is something like @predestroy in spring, like in windsor castle

Something like @PreDestroyin spring-framework?

+3
source share
4 answers

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

void destroy() throws Exception;

before you destroy the bean.

This is one of the ways, the other is when your bean should not implement this interface. In one of your ConfigurationSupport classes, your bean must be defined as a pulsed method with @Bean annotation.

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

      return yourBean;
   }

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

See Spring's documentation for more details: Destruction callbacks

UPDATE

... , - "init-method" "destroy-method" bean... : mkyong.com/spring/spring-init-method-and-destroy-method-example

beans Spring.

+14

3 .

@PreDestroy

destroy-method xml

DisposableBean,

faivorite - @PreDestroy.

:

application-context.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"
       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>

@PreDestroy @PostDestroy. , ShutDownBean, callback callback.   bean.

import javax.annotation.PreDestroy;

public final  class ShutDownBean {

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

}

.

, @PreDestroy :

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

: AbstractApplicationContext registerShutdownHook(), , .

, destroy-method , . , applcation-context.xml:

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

destroy , .

, !

+6

Do you mean method annotation with standard JDK @PreDestroy? This is fairly common in Spring and is usually better than using an attribute destroy-methodin a bean declaration in XML. All you have to do is enable

<context:annotation-config/> 

Your configuration file and Spring handle the rest.

+3
source

There is a standard .NET method IDisposable.Dispose(). I don't know Spring, but from quick googling it seems that @predestroy is almost the same concept.

0
source

All Articles