Spring core. The default method is @Bean destroy

I have my own bean:

@Bean
public MyBean myBean(){...

after spring documentation for releasing my own resources, I have to specify destroyMethod. I did not find the default destruction methods called by spring unless destroyMethodspecified directly.

I used

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

but consider not specifying the destroy method directly if it has a default value.


Does the spring is the default, for example destroy, close, release? If spring tries to use some methods to release resources by default, which ones?

+4
source share
4 answers

As described in Bean.destroyMethod:

, @Bean. , @Bean, DBCP Apache Commons BasicDataSource, close() destroyMethod. " " no-arg "close" "shutdown".

, destroyMethod, bean close() shutdown(), destroy.

, @Bean(destroyMethod = "").

+8

org.springframework.beans.factory.DisposableBean -

void destroy() throws Exception;

-

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

XML

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

bean

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

@PreDestroy

+2

DisposableBeanAdapter . , , - destroy, Spring. , - , , DisposableBean.

+1

, @PreDestroy

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

Source: https://habr.com/ru/post/1680103/


All Articles