How does Spring know when to call 'destory' method on a session/request scoped bean (in other words, how does it detect that the concerned bean is going out of scope)?
I read somewhere that it uses request/session listeners to be notified of these events. But these listners need to be defined in web.xml, and there's no mention of defining such listeners in Spring literature. So how does this work?
The init-method is called after the properties are set by spring container and the destroy-method is called before the JVM is closed.
If you are using it correctly, there is proxy instance injected into new bean. And when you perform the call, proxy will resolve it to current session/scope instance of the bean. So it should work fine, you just need to know at what stage the instance behind proxy is replaced.
The destroy() method will be called before the bean is removed from the container. destroy-method is bean attribute using which we can assign a custom bean method that will be called just before the bean is destroyed.
1 The singleton scope. When a bean is a singleton, only one shared instance of the bean will be managed, and all requests for beans with an id or id s matching that bean definition will result in that one specific bean instance being returned by the Spring container.
The org.springframework.web.servlet.DispatcherServlet
does it. It uses own code, e.g. the org.springframework.web.context.request.RequestAttributes#registerDestructionCallback
callback list functionality to register all these scoped beans.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With