Spring bean的DESTROY-METHOD属性和web应用程序“prototype”bean [英] Spring bean's DESTROY-METHOD attribute and web-application "prototype"d bean

查看:227
本文介绍了Spring bean的DESTROY-METHOD属性和web应用程序“prototype”bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使属性destroy-method工作。

Can get work the attribute "destroy-method".

首先,即使我在destroy-method属性中键入不存在的方法名,

First, even if I type non-existing method name into "destroy-method" attribute,

Spring初始化完成正常(已经很奇怪!)。

Spring initialization completes fine (already strange!).

接下来,当bean有一个原型范围时,然后我想它必须在应用程序

Next, when a bean has a "prototype" scope, then I suppose it must be destroyed before the application

关闭之前销毁。这不会发生,在我的情况下根本就没有调用过。

is closed. That not happens, it is simply never called in my case.

虽然在提取这个bean之后我可以明确地调用这个方法并且它可以完成它的工作。

Though, after extracting this bean I can call this method explicitly and it does its job.

你能解释为什么在我的Spring 2.5案例中从未调用过这个方法吗?

Could you explain why this method is never called in my Spring 2.5 case?

ps该方法存在,它是公开的,没有参数。

p.s. The method exists, it is public and has no arguments.

这似乎是一个比我想象的更困难的任务。

It seems to be a more difficult task then I thought.

问题是每当上下文关闭时都会调用此destroy方法,而
这是一种罕见的情况。

The problem is that this destroy method is called whenever the context is closed, and this is a rare case.

我的问题是:

我有一个网络应用程序。我有一个原型-scoped bean。

I have a web app. I have a "prototype"-scoped bean.

我需要的是当前会话关闭时,Spring会自动调用这个destroy方法。

What I need is when the current session is closed, this destroy method was automatically called by Spring.

我可以手工完成,但有没有任何解决方案如何让Spring做这个工作?它会在会话被销毁后销毁bean,Spring可能会在销毁之前调用该bean上的方法吗?

I can do it by hand, but is there any solution how to make Spring do this job? It destroys the bean after the session is destroyed, it might be possible for Spring to call a method on that bean before destroying it?

p.s。 Spring不管理原型bean的生命周期,因此Spring不会销毁它们:)

p.s. Spring does not manage the lifecycle of prototype beans, so Spring does not destroy them :)

推荐答案

Spring容器无法管理原型bean。

The Spring container doesn't manage prototype beans.

来自参考文档


因此,尽管初始化
生命周期回调方法在所有对象上都被称为
而不考虑范围,在
原型的情况下,配置
销毁生命周期回调是
未调用。

Thus, although initialization lifecycle callback methods are called on all objects regardless of scope, in the case of prototypes, configured destruction lifecycle callbacks are not called.

如果可能,请尝试请求或会话范围。

If possible, try the request or session scope.


当HTTP会话最终丢弃
时,作为特定HTTP会话的范围为
的bean也是
被丢弃。

When the HTTP Session is eventually discarded, the bean that is scoped to that particular HTTP Session is also discarded.

Btw:会话和请求范围仅在您使用支持Web的<$ c时有效$ c> ApplicationContext ,例如 XmlWebApplicationContext

Btw: The session and request scope only works if you're using a web-aware ApplicationContext such as XmlWebApplicationContext

这篇关于Spring bean的DESTROY-METHOD属性和web应用程序“prototype”bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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