is spring Auto-injection a singleton or a multi-sample? How to inject more than one example?

Source: Internet
Author: User

    Single and multi-case differences:  Single-case multiple examples need to understand these problems:      1. What is a single case of multiple cases;      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 examples;  1. What is a singleton, multiple examples of:      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 a number of cases refers to each request with a new object to handle, For example action;       Singleton mode and multi-sample mode description:      1. Singleton mode and multi-case mode belong to object mode.      2. The Singleton mode object has only one copy in the whole system, and multiple instances can have multiple examples.      3. Neither of them provides a constructor method, that is, the construction method is private.  2. How to produce Singleton, multiple cases:    in common ssh, Singleton is the default in spring, and if you want to produce multiple cases, add scope= "prototype" to the Bean in the configuration file;  3. Why use single-case, multiple-case:    The single case, because there is no need to create a new object for each request, this is a waste of CPU and a waste of memory;    is used in many cases to prevent concurrency problems, that is, a request changes the state of the object, At this point the object processes another request, and the previous request's change to the state of the object causes the object to handle the wrong handling of the other request;    with Singleton and multi-instance criteria 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 case;  4. When to use a single case? When to use multiple cases?     for STRUTS2, action must be in multiple cases, because the action itself contains the value of the request parameter, which can be changed by the State; &nbsP   for STRUTS1, the action can be a singleton, because the value of the request parameter is placed in Actionform instead of the;  in action   In addition to say, the service or DAO must be a singleton, Standard with the 3rd, we have seen some of 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 concurrent bugs are often difficult to reproduce and find;      the Spring Generation object is singleton by default. You can change to multiple cases by using the Scope property<bean id= "user" class= "modle. User "scope=" prototype "> </bean>

is spring Auto-injection a singleton or a multi-sample? How to inject more than one example?

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.