45

I have my own bean:

@Bean
public MyBean myBean(){...

following spring documentation to release its own resources I should specify destroyMethod. I've not found any default destroy methods called by spring in case if destroyMethod is not specified directly.

I used

@Bean(destroyMethod = "close")
public MyBean myBean(){...

but think about possibility to do not specify destroy method directly if it has value by default.


Does spring try something by default like destroy, close, release? If spring tries some methods by default to release resources - which ones?

1
  • I think no. It would be very unclear, you need an annotation like @PreDestroy or @Bean(destroyMethod = "..."), or implement DisposableBean.
    – rxn1d
    Commented Jun 26, 2017 at 9:49

4 Answers 4

83

As documented in Bean.destroyMethod:

As a convenience to the user, the container will attempt to infer a destroy method against an object returned from the @Bean method. For example, given an @Bean method returning an Apache Commons DBCP BasicDataSource, the container will notice the close() method available on that object and automatically register it as the destroyMethod. This 'destroy method inference' is currently limited to detecting only public, no-arg methods named 'close' or 'shutdown'.

In other words, if you don't specify destroyMethod, but the bean has a public close() or shutdown() method, it will be automatically used as the destroy-method.

To disable this inference, use @Bean(destroyMethod = "").

2
  • Besides the public close() method, there is also @PreDestroy annotation and DisposableBean interface. Is there any recommended or generally preferred way to go?
    – dpelisek
    Commented Jul 12, 2023 at 10:43
  • 1
    @dpelisek @Bean with a destroyMethod will work for any class used as a bean, even if you can't modify the class. Using @PreDestroy means you need to have access to the source of that class to be able to add that annotation to the right method, similar for DisposableBean: you need to be able to implement that interface. So, if it is your own code, by all means use @PreDestroy or DisposableBean (personally I would prefer the annotation over the interface), but for third-party classes you usually don't have the choice. Commented Jul 12, 2023 at 10:55
22

You can implement a method which will be executed before destroying and annotate it with @PreDestroy

@PreDestroy
public void methodName() {
    //Your code..
}
10

The org.springframework.beans.factory.DisposableBean interface specifies a single method −

void destroy() throws Exception;

Simply implement it −

public class ExampleBean implements DisposableBean {
   public void destroy() {
      // do some destruction work
   }
}

for XML-based configuration

<bean id = "exampleBean" class = "examples.ExampleBean" destroy-method = "destroy"/>

and in the bean

public class ExampleBean {
   public void destroy() {
      // do some destruction work
   }
}

or annotate with @PreDestroy

4

You can extend DisposableBeanAdapter class. One of the methods it provides is the destroy method being called by Spring. This way you don't have to provide any implementation while it is required when you're using DisposableBean interface.

Not the answer you're looking for? Browse other questions tagged or ask your own question.