Scope Configuration and @scope annotations in spring

Source: Internet
Author: User

Scope, also known as scope, in the Spring IoC container refers to the requested visible range of the bean object it creates relative to other bean objects. There are several scopes in the Spring IoC container: The basic scope (singleton, prototype), the Web scope (Reqeust, Session, Globalsession), and the custom scope.
1. The scope of Spring must be specified in the configuration file when the Bean is assembled and configured as follows (as an example of an XML configuration file):

<!---<id= "XXX"  class= "com. Xxx.xxxxx "  scope=" XXXX "/>

Singleton: Singleton mode, in the entire spring IOC container, the bean defined with Singleton will have only one instance

Prototype: Prototype mode, which generates a new bean instance each time a prototype defined bean is obtained through the container's Getbean method

Request: For each HTTP request, the bean defined with the request will produce a new instance, that is, each HTTP request will produce a different bean instance. The scope is valid only if you are using spring in a web app

Session: For each HTTP session, a new instance is generated using the bean Soymilk defined by the session. The scope is also valid only if spring is used in the Web App

Globalsession: Each global HTTP session, the bean defined with the session will produce a new instance. Typically, this is only valid when using the Portlet context. The scope is also valid only if spring is used in the Web App

Among the more commonly used are singleton and prototype two kinds of scopes. For singleton-scoped beans, each request for the bean will have the same instance. The container is responsible for keeping track of the state of the bean instance and is responsible for maintaining the life cycle behavior of the bean instance; If a bean is set to prototype scope, the program will create a new bean instance each time the Bean,spring requests that ID, and then return to the program. In this case, the spring container only uses the new keyword to create the bean instance, and once it is successfully created, the container is not tracking the instance and does not maintain the state of the bean instance.

If you do not specify a scope for the bean, Spring uses the singleton scope by default. Java requires a memory request when the Java instance is created, and garbage collection is required to destroy the instance, which can lead to increased system overhead. Therefore, the cost of creating and destroying prototype-scoped beans is relatively high. Once the bean instance of the singleton scope is successfully created, it can be reused. Therefore, try to avoid setting the bean to the prototype scope unless necessary.

2, based on the development of annotations, @scope to complete the scope of the bean configuration by default is a singleton mode (singleton) If you need to set the corresponding value can be modified in accordance with the above mentioned for example: @scope ("prototype")

Reprint Address: 54019223

Scope Configuration and @scope annotations in spring

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.