弹簧芯.默认的@Bean销毁方法 [英] Spring core. Default @Bean destroy method

查看:927
本文介绍了弹簧芯.默认的@Bean销毁方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有自己的豆子:

@Bean
public MyBean myBean(){...

在spring文档发布自己的资源之后,我应该指定destroyMethod.如果没有直接指定destroyMethod,我还没有找到spring调用的默认销毁方法.

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.

我用过

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

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

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

spring是否默认尝试使用destroycloserelease之类的东西? 如果spring默认尝试一些方法来释放资源-哪些方法?

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

推荐答案

为方便用户,容器将尝试推断 针对从@Bean方法返回的对象的destroy方法.为了 例如,给定@Bean方法返回一个Apache Commons DBCP BasicDataSource,容器将注意到close()方法 在该对象上可用,并自动将其注册为 destroyMethod.该销毁方法推断"目前受到限制 仅检测名为"close"或"shutdown"的公共无参数方法.

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

换句话说,如果您未指定destroyMethod,但是Bean具有公共的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.

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

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

这篇关于弹簧芯.默认的@Bean销毁方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆