Spring consolidates default scopes for SPRINGMVC controller and STRUTS2 action control layer Beans

Source: Internet
Author: User

The controller scope for spring when consolidating SPRINGMVC is a singleton by default.

The action is a scope prototype when spring consolidates Strut2.


We know that spring defaults to singleton when configuring beans. then for the service and DAO layer beans is also the singleton model used.

Let's do a test.

Package Com.pyc.test.controller;import Org.springframework.context.annotation.scope;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.bind.annotation.responsebody;/** * Created: 2015-2-1 pm 9:40:03 *  * @author Andy * @version 2.2 *  */@Controller @requestmapping ("/my") public class TestController {private static integer num_s = 0;private integer num_n = 0; @RequestMapping ("/count") public @ResponseBodyInteger Show () {System.out.println ("num_s:" + (num_s++) + "   Num_n: "+ (num_n++)); return 1;}}

After multiple requests:


You can see that the variable num_n uses the same member variable, so the default is a singleton.


When we add the following code, add @scope changes to prototype mode:


@Controller @scope ("prototype") @RequestMapping ("/my")

You can see that non-static member variables are always new.


SPRINGMVC controller data is passed from the method parameters, is method-level, using singleton mode, so in the absence of shared member variables are thread-safe, should avoid shared member variables to prevent thread security problems,


The action of spring-managed Struts2 is a prototype pattern, the action is class-level, the argument can be a member variable, and there is no thread safety issue.


Spring consolidates default scopes for SPRINGMVC controller and STRUTS2 action control layer Beans

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.