Single-Profit Mode and non-single-Profit Mode of spring Bean

Source: Internet
Author: User

By default, all the beans in spring bean are single, that is, the single-profit mode. <Bean id = "mybean" class = "... "Scope = "Singleton"/>. Singleton indicates that only one bean object called mybean will be created in the spring container. All requests to this bean are processed by this object. This bean object is shared, that is, this bean is non-thread-safe. In this case, isn't the beans injected by containers in the service and Dao layers of our three-tier framework non-thread-safe? Isn't that a problem? The answer is that the beans do not use global variables, that is, there is no shared data, so we don't have to worry about it. If shared data is available, we can set it for thread security.Singleton = "false"To ensure its security.

When will we often use non-single-profit beans? When the action is handed over to spring for management, the bean of the action generated by the container is not single-profit. Configure the following in struts. XML (struts2): <constant name = "struts. objectfactory" value = "Spring"/> (struts1 does not know how to configure it ). The bean for Action Management by the container also needs a configuration, that isScope = "prototype ".In this way, every time the page requests an action bean, a new object will be generated to process the user's request. This ensures the thread security.

 

For the above questions, you can also refer to the following three explanations:

Http://www.juziku.com/zhouchao/wiki/4036.htm,

Http://blog.csdn.net/mastermind/article/details/1932787 2

3. section 2.5 of chapter 2 of spring in Action (Chinese version 2): Controls bean initialization (2.5.1 bean containerization)

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.