Aug 16th, 2012, 03:32 AM
Is it possible for beans to have their data changed inbetween requests?
I am very new to spring so still learning daily but whilst writing a small test application I have noticed some behaviour that made me wonder whether or not it would be possible for a beans data to be changed by another request whilst in use, on a very busy system of course.
What I noticed is that I wrote a small class has an array list as a private parameter and this array list is returned by a get method. Multiple requests increase the array list, ie it holds its value as I guess only one instance exists.
So what would happen if request A sets a value in a bean and before it reads it request B changes those values?
Aug 16th, 2012, 08:05 AM
I strongly suggest a read of the spring reference guide which explains bean scopes.
First question why are you holding state in singletons? State is something that should belong to the current request/session/user and not in the application beans. Also not everything needs to be a spring bean .
However to solve your problem you probably want to use a request scoped bean (instead of a singleton). Again as suggested read the reference guide about scopes.
Aug 17th, 2012, 02:05 AM
I will read through the reference material .re scopes again today This is just a very simple application with 1 jsp and 1 controller. The controller has a service injected into it and the service has a bean which needs to be returned to the jsp, that service also has a DAO injected. The DAO returns some data to the service which returns an object (the bean in question) to the controller. The controller then returns this object to the jsp to render the screen. Now on a subsequent page refresh the ArrayList in that bean has the first requests data, plus the next requests data and so on.
Originally Posted by Marten Deinum
I didnt choose or want to hold the state in a singleton I just wanted a bean to return some data to the jsp and I noticed that the ArrayList was still full of the previous request\return cycles content, I only used an annotation based example to build this application but figured that in production this could cause some issues. I am just using the defaults that come with the annotations tutorial I was following.
You said not everything needs to be a spring bean, so is it then ok to just create objects in the normal way saying something like Bean myBean = new DataContainerBean(); etc? Someone at work (who already uses spring on their team) told me that we shouldnt be creating beans to return to a jsp in this way with spring which is the only reason I had used autowire to create the bean via the container.
Last edited by paul_; Aug 17th, 2012 at 02:11 AM.
Aug 17th, 2012, 02:23 AM
You should use spring to bootstrap and wire your application beans (services, dao's etc.) however for other things that hold state you should create new beans yourself...
As stated you should never hold state in a singleton (at least if that state can be changed and isn't the same for each and every user). So simply return the values you want and don't store them in instance variables they should be contained to the method.