Is spring automatic injection singleton or multiple? How does a singleton inject multiple instances?

   The difference between singleton and multiple instances:
 
Singleton and multiple instances need to understand these issues:
     1. What is a single instance and multiple instances;
     2. How to generate a single instance with multiple instances;
     3. Why use a single instance with multiple instances
     4. When to use a single instance and when to use multiple instances;
 
1. What is singleton and multiple instances:
     The so-called singleton means that all requests are handled by one object. For example, the objects of our commonly used service and dao layers are usually singletons, while multiple instances mean that each request is handled by a new object, such as action;
 
     Description of singleton mode and multi-instance mode:
     1. The singleton pattern and the multi-instance pattern belong to the object pattern.
     2. There is only one copy of the object in the singleton mode in the entire system, and there can be multiple instances in the multi-case mode.
     3. None of them provide external constructors, that is, the constructors are all private.
 
2. How to generate a single instance and multiple instances:
    In general SSH, singleton is the default in spring. If you want to generate multiple instances, add scope="prototype" to the bean of the configuration file;
 
3. Why use singleton and multiple instances:
    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;
   The reason for using multiple instances is to prevent concurrency problems; that is, a request changes the state of the object, and the object processes another request at this time, and the change of the state of the object by the previous request causes the object to process another request incorrectly. ;
    There is only one criterion for using singleton and multiple instances:
    When the object contains a state that can be changed (more precisely, the state will change in practical applications), there are multiple instances, otherwise a single instance;
 
4. When to use a singleton? When to use multiple instances?
    For struts2, action must be used in multiple cases, because the action itself contains the value of the request parameter, which can be changed;
    For STRUTS1, action can be used as a singleton, because the value of the request parameter is placed in the actionForm, not in the action;
    In addition, it does not mean that service or dao must be a singleton. The standard is the same as that mentioned in point 3. I have seen that some services also contain a changeable state, and the execution method also depends on this state, but Use the same singleton, so there will be hidden bugs, and concurrent bugs are usually difficult to reproduce and find;
 
       Spring generated objects are singletons by default. Can be changed to multiple instances through the scope attribute
 
<bean id="user" class="modle.User" scope="prototype">  </bean>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325256918&siteId=291194637