Difference between singleton and prototype of scope in spring

Source: Internet
Author: User

<Bean id = "meetaction" class = "com. Web. Actions. meetsaction"
Scope = "prototype">
<Property name = "meetsservice" ref = "meetsservice"/>
</Bean>

Scope = "prototype" is not written. to add, delete, and delete a table in the project, an action is used. The actionadd, update, delete, and save methods are used, the ADD and modify operations share a page. When the page gets the ID, the table is modified, and vice versa. Because the bean configured with spring forgets to write scope = "prototype", the last accessed record is displayed each time it is added, scope = "prototype" creates a new action object when this type of object is requested. If scope = prototype is not configured, a new action is not created when added. However, the action of webwork is NOT thread-safe because it retains the recorded information of the previous access, in a multi-threaded environment, a thread must correspond to an independent instance, and Singleton cannot be used. Therefore, When configuring webwork action bean in spring, we need to add the attribute scope = "prototype" or Singleton = "false ".
Singleton mode refers to the full sharing of an object, including the code space and data space. To put it bluntly, if a class is Singleton, if the class has a member variable, the value of this member variable is shared by various threads (a bit similar to static). After thread a assigns a value to the variable, thread B can read the value. Therefore, for foreground actions, the singleton mode cannot be used. A thread request must correspond to an independent instance. Broadly speaking, Singleton cannot be used as long as it is a class with data member variables to prevent multiple threads from mixing data. For the services and Dao we use, Singleton is used because they do not use data member variables. if the service needs data member variables, set Singleton to false. Stateful beans use prototype scopes, while stateless beans use Singleton scopes.

In spring2.0, besides singleton and prototype, three new web scopes are added: request, session, and global session. If you want a bean in the container to have a new Web scope, in addition to configuring the corresponding scope attribute at the bean level, you must also perform an additional Initialization Configuration at the container level. That is to say, add such a contextlistener to the Web. xml file of the Web application: org. springframework. Web. Context. Request. requestcontextlistener. The above is for Versions later than servlet 2.4. For example, request scope.

There are differences between spring 1. * And spring 2.0 configurations.

Spring 1 .*
<Bean id = "hibernatesessionfactory" class = "org. hibernate. admin. component. hibernatesessionfactory" init-method = "init" Destroy-method = "dispose" Singleton = "true">

Spring 2.0
Spring-beans-2.0.dtd/XSD does not support Singleton = "true"/"false" anymore. Use scope = "Singleton/" prototype "instead!
<Bean id = "hibernatesessionfactory" class = "org. hibernate. admin. component. hibernatesessionfactory" init-method = "init" Destroy-method = "dispose" Scope = "Singleton">

 

Source: http://aixiangct.blog.163.com/blog/static/9152246120103110436971/

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.