Spring integrates spring MVC Controller and Struts2 Action control layer Bean's default Scope
Spring's Controller scope when integrating Spring MVC is a singleton by default.
When Spring integrates Strut2, Action is a prototype of scope.
We know that Spring is a singleton by default when bean is configured. Then, the service and dao layer beans are also the singleton model.
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;/*** Creation Time: 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:
It can be seen that the variable num_n uses the same member variable. Therefore, the default value is Singleton.
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + tbHO0sPHzO2808jnz8K0 + expires + c?vcd4kphbyzsbjbgfzcz0 = "brush: java;"> @ Controller @ Scope ("prototype") @ RequestMapping ("/my ")
We can see that non-static member variables are always new.
The Controller data of SpringMVC is transmitted from the method parameters at the method level. The Singleton mode is used. Therefore, it is thread-safe without sharing member variables, avoid shared member variables to prevent thread security issues,
The Struts2 Action managed by Spring is the prototype mode, and Action is similar. The parameter can be a member variable and there is no thread security issue.