Spring scope= "prototype", scope= "session"

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/JemBai/archive/2010/11/10/1873954.html

Struts+spring action should be configured as scope= "prototype"
<bean id=scope="prototype"  class="Quickstart.action.PersonAction" >
<constructor-arg ref="Personservice"/>
</bean>
But for those DAO implementation class referral Scope= "Singleton", because these classes have no state, with Singleton only need to maintain an instance, obviously performance is higher.

In the configuration file, the bean is the singleton mode by default, and the application server starts immediately after the bean is created and can be reused later.
This leads to a problem where the bean's global variable is assigned, and the value is brought past the next use. In other words, the bean is stateful.
In the Web State, requests are multithreaded, and global variables can be modified by different threads, especially when concurrency brings unexpected bugs.
In the development, the traffic is very small, there is no concurrency, multithreading problems, programmers will most likely ignore this problem.
So when configuring the action bean, you should use scope="prototype"to create a new action instance for each request. This
In line with Struts2 's requirements, STRUTS2 creates a new action instance for each request. When the request is finished, the bean is
Destroyed, as garbage recovered.
Of course, you can also set scope= "session", can also avoid the web action concurrency problem, only for the current user to create a bean, until
The session disappears. In this case, the bean is stateful for the current user. The advantage is that fewer instances of the bean are created, and there is a
Point-of-order performance improvements
Application Scenarios:
1. Prototype should be used in most cases
2. If the user is not many, and frequent operation (frequent use of action), hardware generally, you can consider the session, may also be able to improve a little bit of performance.

Spring scope= "prototype", scope= "session"

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.