我有自己的 bean :
@Bean
public MyBean myBean(){...
按照 spring 文档发布自己的资源我应该指定 destroyMethod
。如果没有直接指定 destroyMethod
,我还没有找到 spring 调用的任何默认销毁方法。
我用过
@Bean(destroyMethod = "close")
public MyBean myBean(){...
但是如果默认值有值的话,考虑不直接指定destroy方法的可能性。
spring 会默认尝试 destroy
、close
、release
之类的东西吗?
如果spring默认尝试一些方法来释放资源-哪些?
最佳答案
如 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 DBCPBasicDataSource
, the container will notice theclose()
method available on that object and automatically register it as thedestroyMethod
. This 'destroy method inference' is currently limited to detecting only public, no-arg methods named 'close' or 'shutdown'.
也就是说,如果你没有指定destroyMethod
,但是bean有一个公共(public)的close()
或者shutdown()
方法, 它将自动用作销毁方法。
要禁用此推理,请使用 @Bean(destroyMethod = "")
。
关于java - Spring 芯。默认@Bean 销毁方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44756872/