<BeanID= "Person1"class= "Com.bean.life.Person"> < Propertyname= "Name"> <value>Xiao ming</value> </ Property></Bean><BeanID= "Person2"class= "Com.bean.life.Person"> < Propertyname= "Name"> <value>Little Red</value> </ Property></Bean>
Just two days of contact with spring, has been a misunderstanding of these two concepts, always thought that Singleton said that all implementations of "class with the same name" Bean point to the same Java instance, the above bean as an example, previously thought Person1, Person2 is the same Com.bean.life.Person object reference, so foolish, whether the scope of the bean is singleton or prototype, Person1, Person2 are different instances!
The positive solution is that each bean configured in the XML is a different instance!
The singleton we're talking about is when we get a bean with getbean () or ref, whether we get the same bean or create a new bean instance every time. In spring, the Singleton and prototype are all aimed at a "one" bean. So:SingletonRefers to a single bean, each bean has only one instance object, no matter how many times the Getbean () method is called, no matter how many ref dependencies, there is only one instance of the bean in the spring container.prototypeRefers to the bean, each time the Getbean () method is called, or if there is a ref dependency, a completely new instance of the bean is created, with multiple bean instances in the container. Note: The default scope in Spring is: Singleton.
Discrimination Singleton and prototype