Nov 30th, 2011, 03:28 AM
Preload a number of beans with prototype scope
My application is using beans with scope prototype.
When the user is connecting, a main context bean is built and all injections are done.
Depending on the number of objects the main context bean had been injected (always the same injection for an application), i can see in my log files that spring loading time is between 200 ms and 3 seconds (when i have a lot of objects in my application).
In my dream, i would have to add a parameter "numberOfObjectsPreloaded" with an int value and objects would be preloaded (in a low priority thread).
By this way, when my users are logged in, they won't have to wait for 3 seconds, we'll just have to give him one of the preloaded bean.
The question is : is it only in my dream ? Is there a way to do it with spring configuration file ?
Thanks for your help.
Dec 1st, 2011, 10:53 AM
My question is always the same when I see someone create a bean with scope "prototype".
Why do you have beans scoped "prototype"? Are they domain object? Domain objects are not created as <bean>s in Spring because they hold state.
Are they your Services, DAOs? They shouldn't hold any state at all, and therefore having just a Single instance is plenty for your application. Because if they are stateless then there are no threading issues and many threads can call the same method at the exact same time without any issues.
So what type of objects are you defining as "prototype" and why? I have never needed a bean to be "prototype", and while I would never say never, but 99.9999% of your beans would never be "prototype". But I don't know you complete use case for it.
However, you can always just create your own pool class and create a pool. But Spring doesn't have bean pooling, because it isn't necessary.
Dec 1st, 2011, 11:08 AM
I'm using prototype beans in vaadin applications.
I want my pages to be loaded by spring but Vaadin needs a unique instance of those bean for each context.
Keeping it in session is not a good solution because you can have many applications with the same session.
So each time, a user is logging in, i load a user context from Spring (a bean with prototype scope) and i inject it in the vaadin application.
You answered to my question. I'm going to code my own pool.
Thanks for your answer.
Tags for this Thread