侧边栏壁纸
  • 累计撰写 251 篇文章
  • 累计创建 138 个标签
  • 累计收到 16 条评论

目 录CONTENT

文章目录

Spring MVC Controller单例陷阱

Sherlock
2016-03-25 / 0 评论 / 0 点赞 / 801 阅读 / 2167 字 / 编辑
温馨提示:
本文最后更新于 2023-10-09,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

Spring MVC Controller默认是单例的:

单例的原因有二:
1、为了性能。
2、不需要多例。

1、这个不用废话了,单例不用每次都new,当然快了。
2、不需要实例会让很多人迷惑,因为spring mvc官方也没明确说不可以多例。
我这里说不需要的原因是看开发者怎么用了,如果你给controller中定义很多的属性,那么单例肯定会出现竞争访问了。 因此,只要controller中不定义属性,那么单例完全是安全的。下面给个例子说明下:

package com.lavasoft.demo.web.controller.lsh.ch5;  
import org.springframework.context.annotation.Scope;  
import org.springframework.stereotype.Controller;  
import org.springframework.ui.ModelMap;  
import org.springframework.web.bind.annotation.RequestMapping;  
/**
 * Created by Administrator on 14-4-9.
 *
 * @author leizhimin 14-4-9 上午10:55
 */
@Controller
@RequestMapping("/demo/lsh/ch5")
@Scope("prototype")
public class MultViewController {  
    private static int st = 0;      //静态的
    private int index = 0;          //非静态
    @RequestMapping("/show")
    public String toShow(ModelMap model) {
        User user = new User();
        user.setUserName("testuname");
        user.setAge("23");
        model.put("user", user);
        return "/lsh/ch5/show";
    }
    @RequestMapping("/test")
    public String test() {
        System.out.println(st++ + " | " + index++);
        return "/lsh/ch5/test";
    }
}

0 | 0
1 | 1
2 | 2
3 | 3
4 | 4

改为单例的:
0 | 0
1 | 0
2 | 0
3 | 0
4 | 0

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

最佳实践: 1、不要在controller中定义成员变量。
2、万一必须要定义一个非静态成员变量时候,则通过注解@Scope("prototype"),将其设置为多例模式。

0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区