简体   繁体   English

Spring Custom Scoped Bean - 销毁回调

[英]Spring Custom Scoped Bean - Destruction Callback

I have created a custom scope in my application that implements org.springframework.beans.factory.config.Scope . 我在我的应用程序中创建了一个自定义作用域,它实现了org.springframework.beans.factory.config.Scope

As part of this, i have to implement the method below so that the PreDestroy method gets correctly invoked on my custom scoped beans. 作为其中的一部分,我必须实现下面的方法,以便在我的自定义范围bean上正确调用PreDestroy方法。

public void registerDestructionCallback(String name, Runnable callback) {
}

The javadocs on the method are not that clear and I seem to be lost about what code i should write in that method. 方法上的javadocs不是那么清楚,我似乎对于我应该在该方法中编写的代码感到迷茫。

Can someone please help? 有人可以帮忙吗?

It depends on what your custom scope actually does and if you are using this scope for DisposableBean, beans with destroy-method, and DestructionAwareBeanPostProcessor. 这取决于您的自定义作用域实际执行的操作以及您是否将此作用域用于DisposableBean,使用destroy-method和DestructionAwareBeanPostProcessor。

In simplest case you don't need to do anything or just log a warning that callback is not supported. 在最简单的情况下,您不需要执行任何操作或仅记录不支持回调的警告。

In more complex case, you need to handle object destruction that is outside of regular custom scope life cycle. 在更复杂的情况下,您需要处理超出常规自定义范围生命周期的对象销毁。 Eg. 例如。 when object is destroyed, based on its expiration or something similar (unlike explicit call to Scope.remove(String name) method). 当对象被销毁时,基于其到期或类似的东西(与对Scope.remove(String name)方法的显式调用不同)。

For example, for session-scoped beans, this callback is hooked up to HttpSessionListener.sessionDestroyed(..) event. 例如,对于会话范围的bean,此回调连接到HttpSessionListener.sessionDestroyed(..)事件。 See sources for org.springframework.web.context.request.SessionScope class. 请参阅org.springframework.web.context.request.SessionScope类的源代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM