File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes Doubt in bean creation - sesion scoped bean is referenced by singleton scoped bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Doubt in bean creation - sesion scoped bean is referenced by singleton scoped bean" Watch "Doubt in bean creation - sesion scoped bean is referenced by singleton scoped bean" New topic
Author

Doubt in bean creation - sesion scoped bean is referenced by singleton scoped bean

Tapan Maru
Ranch Hand

Joined: May 08, 2006
Posts: 65
Hi,

The default behavior for ApplicationContext implementations is to eagerly pre-instantiate all singleton beans at startup. Pre-instantiation means that an ApplicationContext will eagerly create and configure all of its singleton beans as part of its initialization process.

Now my xml file has following kind of bean declaration.

----------------- xml declaraion ----------------------------------------------------

<bean name="singletonBean" class="somepkg.SingletonBean">
<property name="someProperty" ref="sessionBean"/>
</bean>

<bean name="sessionBean" class="somepkg.SessionBean"/>

------------------------------------------------------------------------------------------

My doubts are:

1. Will singletonBean be able to initialize? or will it give any exception?
2. if singletonBean will be able to initialize, will spring instantiate sessionBean or not? because by default behavior, it is to be instantiate for each new session.
3. if sessionBean will be instantiated by spring, how it will again instantiate a new instance of that bean and give reference to singletonBean
4. if sessionBean will not be instantiated, will singletonBean contains a null reference of it after it is initialized or will it throw any exception and will not initialize at all.


Tapan Maru
tapanmaru@gmail.com
Tapan Maru
Ranch Hand

Joined: May 08, 2006
Posts: 65
sorry but change in xml:


----------------- xml declaraion ----------------------------------------------------

<bean name="singletonBean" class="somepkg.SingletonBean">
<property name="someProperty" ref="sessionBean"/>
</bean>

<bean name="sessionBean" class="somepkg.SessionBean" scope="session"/>

------------------------------------------------------------------------------------------

Thanks,
Sumeet H Singh
Greenhorn

Joined: Jan 03, 2009
Posts: 29
According to Spring framework reference, a different approach needs to be followed in cases where a class which "lives longer"(singleton bean in this case) needs to be injected with another class having a comparatively shorter life-span(session-scoped bean). The approach is different for prototype & singleton scope though.

In your XML, what we want is that the singletonBean instance should be instantiated only once, and it should be injected with sessionBean. But since sessionBean is session-scoped(which means it should be re-instantiated for every session), the configuration is ambiguous(as the dependencies are set at instantiation time and the session scoped value can change later also).

So instead of injecting with that class, its injected with a proxy that exposes the exact same public interface as sessionBean. The container injects this proxy object into the singletonBean bean, which is unaware that this sessionBean reference is a proxy. Its specified by writing this tag in the sessionBean:
<aop:scoped-proxy/>

XML Configuration:


<bean name="singletonBean" class="somepkg.SingletonBean">
<property name="someProperty" ref="sessionBean"/>
</bean>

<bean name="sessionBean" class="somepkg.SessionBean" scope="session">
<aop:scoped-proxy/>
</bean>


When a singletonBean instance invokes a method on the dependency-injected sessionBean object, it actually is invoking a method on the proxy. The proxy then fetches the real sessionBean object from (in this case) the HTTP Session, and delegates the method invocation onto the retrieved real sessionBean object.


Refer spring-framework-reference 3.0. Its mentioned in detail there. Section 3.5 - Bean scopes, Sub topic - Scoped beans as dependencies

Sumeet
SCJP, SCWCD
Tapan Maru
Ranch Hand

Joined: May 08, 2006
Posts: 65
Thanks Sumeet.

Just one question from your answer.

<bean name="sessionBean" class="somepkg.SessionBean" scope="session">
<aop:scoped-proxy/>
</bean>

developer needs to take care of this or spring will take care of this?

my guess is developer needs to take care for this.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in bean creation - sesion scoped bean is referenced by singleton scoped bean