Singleton multiple examples need to understand two questions:
1. What is a single case, and
2. How to produce a single case of multiple cases;
3. Why use a single case of multiple cases
4. When to use a single case, when to use multiple cases;
1. What is a singleton:
The so-called Singleton is that all requests are handled with an object, such as our common service and DAO layer objects are usually single-case, In other cases, each request is treated with a new object, such as action;
2. How to produce singleton multiple cases:
in common ssh, the singleton is the default in spring, If you want to produce multiple cases, add scope= "prototype" to the Bean in the configuration file,
3. Why use singleton multiple cases:
the reason for using Singleton, Because there's no need to create a new object for each request, which wastes both CPU and memory;
uses multiple cases to prevent concurrency problems, where one request alters the state of an object, and the object processes another request. The previous change in the state of the object caused the object to handle the error of the other request;
with Singleton and multi-instance criteria has only one:
When the object contains a state that can be changed (more precisely in the actual application, the state will change), then multiple cases, or a single example;
4. when to use a singleton? When to use multiple cases?
for struts2, the action must be in multiple cases, because the action itself contains the value of the requested parameter and can be changed;
for STRUTS1, The action is available as a singleton, because the value of the request parameter is placed in the Actionform, not the action;
Another thing to say is that the service or DAO must be a singleton, the standard with the 3rd, It has been seen that the service also contains a change in the state, while the execution method also relies on the state, but the same as the singleton, so that there will be hidden bugs, and the concurrency of the bug is often difficult to reproduce and find;
Singleton multiple cases