Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring calling 'destroy' method on session/request scoped beans

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?

like image 598
shrini1000 Avatar asked Feb 06 '12 09:02

shrini1000


People also ask

When destroy-method is called in Spring?

The init-method is called after the properties are set by spring container and the destroy-method is called before the JVM is closed.

What will happen if we inject bean of session scope into another bean of request scope?

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.

What is destroy-method for @bean?

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.

When a bean has scope limited to only HTTP request that is called?

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.


1 Answers

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.

like image 136
kan Avatar answered Sep 27 '22 15:09

kan