浅谈Spring 的Controller 是单例or多例

所属分类: 软件编程 / java 阅读数: 114
收藏 0 赞 0 分享

背景:今天写代码遇到一个Controller 中的线程安全问题,那么Spring 的Controller 是单例还是多例的呢?若为单例又如何保证并发安全呢?

一、面试回答

Spring管理的Controller,即加入@Controller 注入的类,默认是单例的,因此建议:

1、不要在Controller 中定义成员变量;(单例非线程安全,会导致属性重复使用)

2、若必须要在Controller 中定义一个非静态成员变量,则通过注解@Scope("prototype"),将其设置为多例模式。

二、验证Controller 单例

验证代码:

package com.ausclouds.bdbsec.tjt;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author tjt
 * @time 2020-08-25
 * @desc 验证Controller 单例
 */
@Controller
@ResponseBody
@RequestMapping("/tjt")
public class TestSingleController {

  private long money = 10;

  @GetMapping("/test1")
  public long testSingleOne(){
    money = ++money;
    System.out.println("/tjt/test1: the money I have: " + money);
    return money;
  }

  @GetMapping("test2")
  public long testSingleTwo(){
    money = ++money;
    System.out.println("/tjt/test2: the money I have: " + money);
    return money;
  }

}

首先,访问http://localhost:8088/test1,得到的答案是11

接着,再访问http://localhost:8088/test2,得到的答案是 12

不难看出:同一个变量,两次访问得到不同的结果,很明显是线程不安全的。

验证截图:

三、Controller 如何实现多例?

尽量不要在Controller 中定义成员变量,若必须要在Controller 中定义一个非静态成员变量,则通过注解@Scope("prototype"),将其设置为多例模式;或者是在Controller 中使用ThreadLocal 变量。

验证代码:

package com.ausclouds.bdbsec.tjt;

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

/**
 * @author tjt
 * @time 2020-08-25
 * @desc 验证Controller 单例
 */
@Controller
@ResponseBody
@Scope("prototype") // 将Controller 设置为多例模式
@RequestMapping("/tjt")
public class TestSingleController {

  private long money = 10;

  @GetMapping("/test1")
  public long testSingleOne(){
    money = ++money;
    System.out.println("/tjt/test1: after use @Scope the money I have: " + money);
    return money;
  }

  @GetMapping("test2")
  public long testSingleTwo(){
    money = ++money;
    System.out.println("/tjt/test2: after use @Scope the money I have: " + money);
    return money;
  }

}

在加上@Scope("prototype")后首先,访问http://localhost:8088/test1,得到的答案是11

接着,再访问http://localhost:8088/test2,得到的答案也是 11

不难看出:同一个变量,两次访问得到相同的结果。

验证截图:

四、作用域

其实,spring bean 的作用域除了上面使用的prototype 外,还有singleton、request、session 和global session 四种;其中request、session 和global session 主要运用在Web 项目中。

  • singleton:单例模式,当spring 创建applicationContext 容器的时候,spring会预初始化所有的该作用域实例,加上lazy-init 就可以避免预处理;
  • prototype:原型模式,每次通过getBean 获取该bean 就会新产生一个实例,创建后spring 将不再对其管理;
  • request:每次请求都新产生一个实例,和prototype 不同就是创建后,接下来的管理,spring依然在监听;
  • session:每次会话,同上;
  • global session:全局的web 域,类似于servlet 中的application。
更多精彩内容其他人还在看

使用maven整合Spring+SpringMVC+Mybatis框架详细步骤(图文)

这篇文章主要介绍了使用maven整合Spring+SpringMVC+Mybatis框架详细步骤(图文),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

SpringMVC+Spring+Mybatis实现支付宝支付功能的示例代码

这篇文章主要介绍了SpringMVC+Spring+Mybatis实现支付宝支付功能的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

spring+springmvc+mybatis+maven入门实战(超详细教程)

这篇文章主要介绍了spring+springmvc+mybatis+maven入门实战(超详细教程),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

mybatis输入映射和输出映射实例详解

这篇文章主要介绍了mybatis输入映射和输出映射,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

JAVA发送http get/post请求,调用http接口、方法详解

这篇文章主要介绍了Java发送http get/post请求调用接口/方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring里的Async注解实现异步操作的方法步骤

这篇文章主要介绍了Spring里的Async注解实现异步操作的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

java实现简单的学生信息管理系统代码实例

这篇文章主要介绍了java实现简单的学生信息管理系统,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

SpringBoot2.X Kotlin系列之数据校验和异常处理详解

这篇文章主要介绍了SpringBoot 2.X Kotlin系列之数据校验和异常处理详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Spring Boot 数据校验@Valid+统一异常处理的实现

这篇文章主要介绍了Spring Boot 数据校验@Valid+统一异常处理的实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

浅谈SpringBoot集成Quartz动态定时任务

这篇文章主要介绍了SpringBoot集成Quartz动态定时任务,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享
查看更多