i am bit confused among the 3 scopes i.e request, prototype and singleton explained at http://static.springsource.org/spring/docs/3.0.0.M3/reference/html/ch04s04.html. My doubts are:-
RequestScope :-say a web request comes from client, In the same request we are creating the bean with statement factory.getBean("MyBean1") will it ret开发者_如何学Gourn two different instances. My understanding is it will return the same instance as it is done under the same http request. Right? While it will return the two different instances in case of prototype request. Correct?
Prototype:- Itis results in the creation of a new bean instance every time a request for that specific bean is made. As a rule of thumb, you should use the prototype scope for all beans that are stateful, while the singleton scope should be used for stateless beans. How come its differnt from request scope and how its support for stateful beans.
Singleton:- its written under section under 4.4.1.
Please be aware that Spring's concept of a singleton bean is quite different from the Singleton pattern as defined in the seminal Gang of Four (GoF) patterns book. The GoF Singleton hardcodes the scope of an object such that one and only one instance of a particular class will ever be created per ClassLoader. The scope of the Spring singleton is best described as per container and per bean.
My question here is on statement "The scope of the Spring singleton is best described as per container and per bean"
per container:-Can we have more than one core conatiner in any application? My understanding is that if we are creating BeanFactory factory = new XmlBeanFactory("MyXml") two times in an applicaton , we can say two container instance exist.Right? in this case we can two instances of bean even if it is declared singleton. Is this correct?
It is stated on some websites that it is stated that Spring core container avoid the need of programming the singleton. Here do they mean we dont have to code for singleton container will provide it for us. Right? Question here is If we declare the public constructor in bean. will container still provide the singleton for that bean(scope is singleton in xml file for this bean)?
RequestScope :-say a web request comes from client, In the same request we are creating the bean with statement factory.getBean("MyBean1") will it return two different instances. My understanding is it will return the same instance as it is done under the same http request. Right? While it will return the two different instances in case of prototype request. Correct?
Correct.
Prototype:- Itis results in the creation of a new bean instance every time a request for that specific bean is made. As a rule of thumb, you should use the prototype scope for all beans that are stateful, while the singleton scope should be used for stateless beans. How come its differnt from request scope and how its support for stateful beans.
All scopes support stateful beans, it's just that you need to be very careful about managing that state. You need to think carefully about deciding the scope of that state. Is the state local to the current request, or is it local to the method that calls getBean
? There is no difference in how the various scopes treat stateful beans.
per container:-Can we have more than one core conatiner in any application? My understanding is that if we are creating BeanFactory factory = new XmlBeanFactory("MyXml") two times in an applicaton , we can say two container instance exist.Right? in this case we can two instances of bean even if it is declared singleton. Is this correct?
Yes, that's correct. The "singleton" is preserved only as long as you ask the container for the bean, and that there is only one container defining that bean.
It is stated on some websites that it is stated that Spring core container avoid the need of programming the singleton. Here do they mean we dont have to code for singleton container will provide it for us. Right? Question here is If we declare the public constructor in bean. will container still provide the singleton for that bean(scope is singleton in xml file for this bean)?
If you simply call new MyBean()
, when MyBean
is defined as a bean in Spring, then the singleton will not be preserved. Spring has no control of things that you do outside of the container.
精彩评论