java Spring bean销毁方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36805422/
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-11-03 01:53:03  来源:igfitidea点击:

Spring bean destroy method

javaspringservlets

提问by user1539343

What could be a possible example use of destroy method in a real world application? Why would a running application want to destroy its beans? If the beans are created by the spring container for the web application by, say, ContextLoaderListener, then how can these beans be recreated, because the container already started. Is there a way to restart the spring IoC container without restarting the Application server?

在现实世界的应用程序中使用 destroy 方法的可能示例是什么?为什么正在运行的应用程序想要销毁它的 bean?如果 bean 是由 web 应用程序的 spring 容器创建的,例如,ContextLoaderListener,那么如何重新创建这些 bean,因为容器已经启动。有没有办法在不重启应用服务器的情况下重启spring IoC容器?

回答by leeor

One example would be a DataSourceor any resource that needs clean up. you might have something like this:

一个例子是DataSource需要清理的一个或任何资源。你可能有这样的事情:

@Bean(destroyMethod = "close")
DataSource dataSource() {
 ....
}

This can be especially important in environments with multiple classloaders like an application server to prevent memory leaks.

这在具有多个类加载器(如应用程序服务器)的环境中尤其重要,以防止内存泄漏。

It is sometimes the case that doing this is redundant since the underlying resource may do its own cleanup (say as part of a contextDestroyedevent in the servlet lifecycle), but you should always verify this.

有时这样做是多余的,因为底层资源可能会自行清理(比如作为contextDestroyedservlet 生命周期中事件的一部分),但您应该始终验证这一点。

These spring docsare a useful reference as well. The example cited there is similar:

这些 spring文档也是一个有用的参考。那里引用的例子是类似的:

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

Then the bean class:

然后是bean类:

public class ExampleBean {

    public void cleanup() {
        // do some destruction work (like releasing pooled connections)
    }
}

It is possible to refresh a context. This answerprovides a good explanation of that and when you might want to do it.

可以刷新上下文。这个答案很好地解释了这一点以及您何时可能想要这样做。

回答by haolin

bellow is my views:

以下是我的观点:

What could be a possible example use of destroy method in a real world application?

在现实世界的应用程序中使用 destroy 方法的可能示例是什么?

For most applications, maybe we focus on real businesses mostly, so we won't often meet the scenes where need to define destroy method. But when you meet some basic components or middlewares, you need to pay attention to Resource Management, such as Database Connection, Memory Usage, Disk Usage, etc. You must know how to release unnecessary resource holdings clearly, or this will cause serious problems.

对于大多数应用来说,可能我们主要关注实际业务,所以不会经常遇到需要定义destroy 方法的场景。但是遇到一些基础的组件或者中间件,就需要注意资源管理,比如数据库连接内存使用磁盘使用等,必须清楚地知道如何释放不必要的资源持有,否则会导致严重的问题。

If the beans are created by the spring container for the web application by, say, ContextLoaderListener, then how can these beans be recreated, because the container already started?

如果 bean 是由 web 应用程序的 spring 容器创建的,例如,ContextLoaderListener,那么如何重新创建这些 bean,因为容器已经启动?

Spring Containerisn't only for web application, it can serve for common java application(Main Application). Spring Containerhas two different Bean Type(Singletonand Prototype), Singleton Beanonly be pre-instantiated when Spring Containerfinish to start up, while Prototype Beanwill be instantiated by invoking getBeanevery time.

Spring Container不仅适用于Web 应用程序,它还可以为常见的 Java 应用程序(主应用程序)提供服务。Spring Container有两种不同的Bean TypeSingletonPrototype),Singleton Bean只有在Spring Container完成启动时才会被预实例化,而Prototype Bean会通过每次调用getBean来实例化。

Is there a way to restart the spring IoC container without restarting the Application server?

有没有办法在不重启应用服务器的情况下重启spring IoC容器?

Spring IoC Containerstarts to be instantiated by invoking AbstractRefreshableApplicationContext.refresh()method. This method will destroy entire Spring IoC Containerif you have instantiated the Containerbefore. So you can invoke this method to re-instantiate the Container. If you want to understand Spring IoC Mechanism, I suggest you read the Spring's source code: spring-core, spring-beans, spring-context.

Spring IoC Container通过调用AbstractRefreshableApplicationContext.refresh()方法开始实例化。如果您之前已经实例化了Container,则此方法将销毁整个Spring IoC Container。所以你可以调用这个方法来重新实例化Container。如果你想了解Spring IoC 机制,建议你阅读 Spring 的源代码:spring-corespring-beansspring-context

Hope to help you.

希望能帮到你。

回答by EdH

I have seen beans in a Spring application that either directly or indirectly started non daemon threads. Then it became impossible to stop the process without killing the process. This affected some Jenkins jobs that handled automated testing. So there are plenty of real world examples especially in a DevOps world

我曾在 Spring 应用程序中看到过直接或间接启动非守护线程的 bean。然后就不可能在不杀死进程的情况下停止进程。这影响了一些处理自动化测试的 Jenkins 工作。所以有很多真实世界的例子,尤其是在 DevOps 世界中