Java single and multi-instance explanations

Source: Internet
Author: User

1. What is a single case of multiple cases:
The so-called Singleton is that all requests are handled with an object, such as our common service and DAO layer objects are usually singleton, and many cases refer to each request with a new object to handle, such as action;
2. How to produce a single case of multiple cases:
In common SSH, Singleton is the default in spring, and if multiple cases are to be generated, add scope= "prototype" to the bean in the config file;
3. Why use single-case multiple examples:
The reason for using a singleton is that there is no need to create a new object for each request, which wastes both CPU and memory;
Multiple cases are used to prevent concurrency problems, that is, one request changes the state of an object, and the object processes another request, while the previous request changes the state of the object causing the object to handle the other request incorrectly;
There is only one criterion for single and multiple cases:
When the object contains a state that can be changed (more precisely in the actual application, the state will change), then more than one case;
4. When to use a single case? When to use multiple cases?
For STRUTS2, the action must be in multiple cases, because the action itself contains the value of the request parameter, which can be changed in the state;
For STRUTS1, the action can be a singleton, because the value of the request parameter is placed in the actionform, not in the action;
In addition, it is not said that the service or DAO must be a singleton, the standard with the 3rd, we have seen some service also contains a change of state, while the execution method also relies on the state, but the same use of the Singleton, so there will be hidden bugs, Concurrency bugs are often difficult to reproduce and find;

Java single and multi-instance explanations

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.