The scope properties of beans in spring the difference between single and prototype

Source: Internet
Author: User

http://blog.csdn.net/linwei_1029/article/details/18408363

1. When the scope of a bean is set to Singleton, only one shared bean instance exists in the spring IOC container, and all requests to the bean will only return the same instance of the bean as long as the ID matches the bean definition. In other words, when a bean definition is set to singleton scope, the Spring IOC container only creates a unique instance of the bean definition. This single instance is stored in the singleton cache (singleton cache), and all subsequent requests and references to the bean will return the cached object instance, noting that the singleton in the singleton scope and GOF design pattern is completely different, The singleton design pattern indicates that only one class exists in a ClassLoader, and here the singleton represents a container for a bean, which means that when a bean is identified as Singleton, Only one of the beans will exist in spring's IOC container.

2. Prototype-scoped Bean, each request (injecting it into another bean, or invoking the container's Getbean () method programmatically) produces a new bean instance, which is equivalent to the operation of For prototype-scoped beans, it is important to note that spring cannot be responsible for the entire life cycle of a prototype bean, which is given to the client after it is initialized, configured, decorated, or assembled with a prototype instance. The prototype instance is then ignored. Regardless of the scope, the container invokes the initialization lifecycle callback method for all objects, and for prototype, any configured destructor life-cycle callback methods will not be called.   Clearing prototype-scoped objects and releasing expensive resources held by any prototype bean is the responsibility of the client code. (One possible way for the spring container to release the resource that is consumed by the singleton scope Bean is by using the bean's post processor, which holds a reference to the bean to be purged.) )

As you can see, you can use singleton when you need a global unique designation, and singleton only create one object, and the system consumes less resources. But with the singleton may be the thread security problem, this time need to use the prototype. Consider concurrency issues, and the recommendations are all used prototype.

The scope properties of beans in spring the difference between single and prototype

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.