tags:

views:

70

answers:

2

Reading this

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 ids matching that bean definition will result in that one specific bean instance being returned.

Will be managed...

What does that mean?

If there's only one object, than any modification to this object will result in that every another

attempt to get this bean will return a modified instance??

+3  A: 

Not a modified copy but a reference to the only (modified) instance. Singletons are not supposed to be copied.

I don't think "managed" is supposed to refer to any special processing other than keeping "only one shared instance of the bean".

Péter Török
@Péter Török Yes that's what I was talking. So if one web app copy will change this object and another web app instance will try to get a bean, the second web app will see all the modifications???
EugeneP
By "managed" the docs mean "managed by this ApplicationContext/BeanFactory". So you might create many instances of your bean outside of Spring, but Spring will only create one instance and return references to that single instance anytime you call getBean() when it wires it into other beans. @EugeneP what do you mean by "another web app instance"? Each web application is essentially a separate container and unless you have set up some special configuration, you'll have one Spring bean factory in one webapp and a second instance in the second webapp.
matt b
@EugeneP, exactly. Barring visibility issues in multithreaded programs - I don't know how (or whether) Spring handles these, but I suppose you must ensure that your singleton is thread-safe.
Péter Török
@matt b I mean that there may be 100 simultaneous sessions of using this o-n-e w-e-b a-p-p. They are absolutely separated and work with different data. though, it is one deployed war file.
EugeneP
Then you only have one instance of the application.
matt b
A: 

Managed in this case means that Spring will apply a certain lifecycle to the class (read more on that here: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-nature). In general the term was coined to emphasize that you do not have to care about technical issues with the class like instantiating, number of instances, instance lifecycle and so on. All this gets managed for you by Spring.

Oliver Gierke