Spring integrates spring MVC Controller and Struts2 Action control layer Bean's default Scope

Source: Internet
Author: User

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.


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.