Java 弹簧芯。默认@Bean 销毁方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44756872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:42:59  来源:igfitidea点击:

Spring core. Default @Bean destroy method

javaspringrelease

提问by Sergii

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 destroyMethodis not specified directly.

按照 spring 文档发布自己的资源,我应该指定destroyMethod。如果destroyMethod没有直接指定,我没有找到任何由 spring 调用的默认销毁方法。

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.

但是请考虑如果默认情况下具有值,则不直接指定 destroy 方法的可能性。



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

spring 是否默认尝试诸如destroy, close, 之类的东西release?如果 spring 默认尝试一些方法来释放资源 - 哪些?

采纳答案by Mark Rotteveel

As documented in Bean.destroyMethod:

如记录在Bean.destroyMethod

As a convenience to the user, the container will attempt to infer a destroy method against an object returned from the @Beanmethod. For example, given an @Beanmethod 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'.

为方便用户,容器将尝试针对从该@Bean方法返回的对象推断一个 destroy方法。例如,给定一个@Bean返回 Apache Commons DBCP BasicDataSourceclose()方法,容器将注意到该对象上可用的方法并自动将其注册为 destroyMethod. 这种“破坏方法推断”目前仅限于检测名为“close”或“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.

换句话说,如果你没有指定destroyMethod,但是 bean 有一个 publicclose()shutdown()方法,它会被自动用作 destroy-method。

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

要禁用此推理,请使用@Bean(destroyMethod = "").

回答by Dmitry Senkovich

You can extend DisposableBeanAdapterclass. One of the methods it provides is the destroymethod being called by Spring. This way you don't have to provide any implementation while it is required when you're using DisposableBeaninterface.

你可以扩展DisposableBeanAdapter类。它提供的destroy方法之一是 Spring 调用的方法。这样你就不必提供任何实现,而在使用DisposableBean接口时需要它。

回答by kk.

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

您可以实现一个方法,该方法将在销毁之前执行并使用 @PreDestroy

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

回答by Ofer Skulsky

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

org.springframework.beans.factory.DisposableBean 接口指定了一个方法?

void destroy() throws Exception;

Simply implement it ?

简单地执行吗?

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

for XML-based configuration

用于基于 XML 的配置

<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

或使用@PreDestroy 进行注释