Spring bean DESTROY-METHOD attribute and "prototype" web application d bean

The "destroy-method" attribute can get work.

First, even if I entered a nonexistent method name in the "destroy-method" attribute,

Spring initialization completes fine (already weird!).

Then when the bean has a "prototype" scope, I assume it should be destroyed before the application

closed. It doesn't, it just never gets called in my case.

Though after retrieving this bean, I can explicitly call this method and make it work.

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

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

This seems to be a more difficult task than I thought.

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

My question is:

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

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

I can do it manually, but is there some solution how to make Spring make this work? It destroys the bean after destroying the session, maybe it is possible for Spring to call a method on that bean before destroying it?

ps Spring does not manage the beans prototype lifecycle, so Spring does not destroy them :)

+2


a source to share


1 answer


Spring container does not manage beans prototype.

An excerpt from the reference documentation:

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



Try the query or session scope if possible.

When the HTTP session is eventually dropped, the bean that this particular HTTP session is also dropped.

Btw: session and request scope only works if you are using web oriented ApplicationContext

likeXmlWebApplicationContext

+7


a source