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