简体   繁体   中英

Spring Custom Scoped Bean - Destruction Callback

I have created a custom scope in my application that implements 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.

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.

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.

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).

For example, for session-scoped beans, this callback is hooked up to HttpSessionListener.sessionDestroyed(..) event. See sources for org.springframework.web.context.request.SessionScope class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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