温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring的Controller是怎么保证并发的安全

发布时间:2021-12-02 16:19:45 来源:亿速云 阅读:183 作者:柒染 栏目:云计算

Spring的Controller是怎么保证并发的安全,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

Controller 默认是单例的,不要使用非静态的成员变量,否则会发生数据逻辑混乱。正因为单例所以不是线程安全的。

我们下面来简单的验证下:

package com.xttblog.springbootdemo.controller;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author 业余草,公众号
 */
@Controller
public class ScopeTestController {

    private int num = 0;

    @RequestMapping("/testScope")
    public void testScope() {
        System.out.println(++num);
    }

    @RequestMapping("/testScope2")
    public void testScope2() {
        System.out.println(++num);
    }
}
 

我们首先访问 http://localhost:8080/testScope,得到的答案是1。

然后我们再访问 http://localhost:8080/testScope2,得到的答案是 2。

得到的不同的值,这是线程不安全的。

接下来我们再来给controller增加作用多例 @Scope("prototype")。

package com.xttblog.springbootdemo.controller;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author 业余草,公众号
 */
@Controller
@Scope("prototype")
public class ScopeTestController {
    private int num = 0;
    @RequestMapping("/testScope")
    public void testScope() {
        System.out.println(++num);
    }

    @RequestMapping("/testScope2")
    public void testScope2() {
        System.out.println(++num);
    }
}
 

我们依旧首先访问 http://localhost:8080/testScope,得到的答案是 1。

然后我们再访问 http://localhost:8080/testScope2,得到的答案还是 1。

相信大家不难发现:

❝  

单例是不安全的,会导致属性重复使用。

❞  
 

解决方案

  • 不要在 Controller 中定义成员变量。
  • 万一必须要定义一个非静态成员变量时候,则通过注解 @Scope(“prototype”),将其设置为多例模式。
  • 在 Controller 中使用 ThreadLocal 变量
 

补充说明

spring bean 作用域有以下 5 个:

  • singleton: 单例模式,当 spring 创建 applicationContext 容器的时候,spring 会欲初始化所有的该作用域实例,加上 lazy-init 就可以避免预处理;
  • prototype:原型模式,每次通过getBean获取该bean就会新产生一个实例,创建后spring将不再对其管理;

(下面是在web项目下才用到的)

  • request:搞 web 的大家都应该明白 request 的域了吧,就是每次请求都新产生一个实例,和 prototype 不同就是创建后,接下来的管理,spring 依然在监听;
  • session: 每次会话,同上;
  • global session: 全局的 web 域,类似于 servlet 中的 application。

关于Spring的Controller是怎么保证并发的安全问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI