Spring MVC不要在@Service bean中保存状态
先看这么一段代码:
@Service
public class AccountService {
private String message; public void foo1() {
if (true) {
this.message = "a";
} else {
this.message = "b";
}
} public void foo2() {
// 改动this.message的代码...
// ... ...
}
}
假设你打算在@Controller里这么调用AccountService :
accountService.foo1();
model.addAttribute(accountService.getMessage());
那么就有线程安全的危急了。
问题原因
那么问题来了,多个线程同一时候读写message成员变量。就可能让getMessage()方法返回错误的值
解决方法
@Service
@Scope("request")
public class AccountService {
private String message;
但坏处是创建@Service bean的开销往往比較大,会导致程序性能下降。
class MessageWrapper {
private String message;
public MessageWrapper(String msg) {
this.message = msg;
}
// 仅仅提供get方法
public String getMessage() {
return this.message;
}
}
AccountService的foo1()方法改动例如以下:
@Service
public class AccountService {
public MessageWrapper foo1() {
if (true) {
return new MessageWrapper("a");
} else {
return new MessageWrapper("b");
} // ... ...
}
这样便能够完美避免线程安全问题,又不会带来过多的额外开销。
Spring MVC不要在@Service bean中保存状态的更多相关文章
- [翻译]Spring MVC RESTFul Web Service CRUD 例子
Spring MVC RESTFul Web Service CRUD 例子 本文主要翻译自:http://memorynotfound.com/spring-mvc-restful-web-serv ...
- 程序中保存状态的方式之Cookies
程序中保存状态的方式之 Cookies,之前写过一篇关于ViewState的.现在继续总结Cookies方式的 新建的测试页面login <%@ Page Language="C#&q ...
- 程序中保存状态的方式之ViewState
程序中保存状态的方式有以下几种: 1.Application 2.Cookie 3.Session 4.ViewState:ViewState是保存状态的方式之一,ViewState实际就是一个Hid ...
- 在C 函数中保存状态:registry、reference和upvalues
在C函数中保存状态:registry.reference和upvalues C函数能够通过堆栈来和Lua交换数据,但有时候C函数须要在函数体的作用域之外保存某些Lua数据.那么我们想到全局变 ...
- Spring MVC page render时jsp中元素相对路径的解决办法
前段时间做了用Spring Security实现的登录和访问权限控制的功能,但是page render使用的是InternalResourceResolver,即在spring的servlet配置文件 ...
- spring MVC 管理HttpClient---实现在java中直接向Controller发送请求
在spring MVC中,大多数时候是由客户端的页面通过ajax等方式向controller发送请求,但有时候需要在java代码中直接向controller发送请求,这时可以使用HttpCilent实 ...
- spring mvc 接收表单 bean
spring MVC如何接收表单bean 呢? 之前项目中MVC框架一直用struts2,所以我也就按照struts2 的思维来思考 页面loginInput.jsp: <?xml versio ...
- spring mvc 数据校验(bean实体注解实现)
spring mvc 数据校验 1.添加个jar (jar与一版本会冲突) <dependency> <groupId>com.fasterxml</groupId> ...
- 利用Spring MVC搭建REST Service
之前写过一篇 利用JAX-RS快速开发RESTful 服务 今天来看下spring-mvc框架如何实现类似的功能: 一.pom.xml <?xml version="1.0" ...
随机推荐
- es6 基础语法
var c= 1 <!--都不能预解析-->let a = 1//const不能修改变量const b = 1 箭头函数 =>var c = function fun(a, b) { ...
- 【git】搭建git服务器
在 Linux 下搭建 Git 服务器 目录 ① 安装 Git ② 服务器端创建 git 用户,用来管理 Git 服务,并为 git 用户设置密码 ③ 服务器端创建 Git 仓库 ④ 客户端 clon ...
- SVG 浏览器支持
可以参考以下链接: https://caniuse.com/#search=svg https://en.wikipedia.org/wiki/Comparison_of_layout_engines ...
- Laravel Homestead的安装和使用(照搬)
原文:https://blog.csdn.net/woqianduo/article/details/81091154/ 1.简介 1.1.Homestead是什么 Laravel Homestead ...
- vue工程化引入组件模板
vue脚手架搭建好项目后,组件间的引用通过components import bannerComponent from './banner' export default { data(){ retu ...
- vue之$mount
数据挂载 在实例化Vue的时候,两种方式挂载数据 方法一:最常用的方法 var app=new vue({ el:"#app", data(){} ````` }) 注:文档中最常 ...
- Learning Discriminative and Transformation Covariant Local Feature Detectors实验环境搭建详细过程
依赖项: Python 3.4.3 tensorflow>1.0.0, tqdm, cv2, exifread, skimage, glob 1.安装tensorflow:https://www ...
- Server.MapPath() 用法
Server.MapPath() ./当前目录/网站主目录../上层目录~/网站虚拟目录 如果当前的网站目录为E:\wwwroot 应用程序虚拟目录为E:\wwwroot\company 浏览的页 ...
- 树莓派 - platform总线,设备和驱动
以树莓派为例子,分析一下其中LED的 platform device 和 platform driver. 查看LED设备,被挂载在/sys/devices/platform下. 注意其中的drive ...
- ubuntu14.04 mysql-workbench Connecting to MySQL server ... Native table 'performance_schema'.'session_variables' has the wrong structure错误解决
使用的mysql版本: mysql Ver 14.14 Distrib 5.7.9, for Linux (x86_64) using EditLine wrapper 打开shell命令 1.输 ...