gpt4 book ai didi

java - Spring 芯。默认@Bean 销毁方法

转载 作者:IT老高 更新时间:2023-10-28 13:55:50 27 4
gpt4 key购买 nike

我有自己的 bean :

@Bean
public MyBean myBean(){...

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

我用过

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

但是如果默认值有值的话,考虑不直接指定destroy方法的可能性。


spring 会默认尝试 destroycloserelease 之类的东西吗?如果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 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有一个公共(public)的close()或者shutdown()方法, 它将自动用作销毁方法。

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

关于java - Spring 芯。默认@Bean 销毁方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44756872/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com