spring中bean的scope属性,有如下5种类型:

singleton 表示在spring容器中的单例,通过spring容器获得该bean时总是返回唯一的实例
prototype表示每次获得bean都会生成一个新的对象
request表示在一次http请求内有效(只适用于web应用)
session表示在一个用户会话内有效(只适用于web应用)
globalSession表示在全局会话内有效(只适用于web应用)
在多数情况,我们只会使用singleton和prototype两种scope,如果在spring配置文件内未指定scope属性,默认为singleton。

单例的原因有二:
1、为了性能。

2、不需要多例。

1、单例不用每次都new,当然快了。

2、不需要实例会让很多人迷惑,因为spring mvc官方也没明确说不可以多例。

我这里说不需要的原因是看开发者怎么用了,如果你给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("/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

最佳实践:定义一个非静态成员变量时候,则通过注解@Scope("prototype"),将其设置为多例模式。

---------------------
作者:zhang_dianliang
来源:CSDN
原文:https://blog.csdn.net/zhang_dianliang/article/details/76850906
版权声明:本文为博主原创文章,转载请附上博文链接!

转:spring mvc 设置@Scope("prototype")的更多相关文章

  1. spring mvc 设置@Scope("prototype")

    spring中bean的scope属性,有如下5种类型: singleton 表示在spring容器中的单例,通过spring容器获得该bean时总是返回唯一的实例 prototype表示每次获得be ...

  2. spring bean中scope="prototype“的作用

    今天写代码时,遇到个问题,问题大概如下:在写一个新增模块,当各文本框等输入值后,提交存入数据库,跳到其它页面,当再次进入该新增页面时,上次输入的数据还存在. 经过检查发现是,spring配置文件中,配 ...

  3. spring MVC设置不拦截静态资源

    问题产生: 因为我们在web.xml中写了 拦截所有请求,当然包括了静态资源,所以页面需要引用css或js的话,该请求也会被拦截,例如: 在style.css中写一个简单样式,加个背景颜色  body ...

  4. spring mvc 设置设置默认首页的方式

    背景: 项目使用springmvc管理请求,有一个小的需求,输入域名的时候自动进入某个页面(或者说自动发起某个请求). 过程: 1,首先想到 在web.xml中配置welcome-file-list的 ...

  5. spring mvc设置字符集过滤器

    <filter> <filter-name>springEncoding</filter-name> <filter-class> org.spring ...

  6. spring入门(八) spring mvc设置默认首页

    1.web.xml配置如下 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3// ...

  7. Spring MVC 设置UTF-8编码

    按照需求选其中之一即可吧. 修改读取参数时候的编码: 在web.xml中: 添加一个过滤器(filter),注册 org.springframework.web.filter.CharacterEnc ...

  8. SSM(spring mvc+spring+mybatis)学习路径——1-1、spring入门篇

    目录 1-1 Spring入门篇 专题一.IOC 接口及面向接口编程 什么是IOC Spring的Bean配置 Bean的初始化 Spring的常用注入方式 专题二.Bean Bean配置项 Bean ...

  9. Spring MVC 处理静态资源文件

    摘要: 三个方案: 1.方案一:激活Tomcat的defaultServlet来处理静态文件 2.方案二: 在spring3.0.4以后版本提供了mvc:resources (需要配置annotati ...

随机推荐

  1. C语言实现Linux之ls

    ls命令用来显示目标列表. 常用参数: -l    :以长格式显示目录下的内容列表.输出信息从左向右依次包括文件名,文件类型,权限模式,硬连接数,所有者.组.文件大小和文件的最后修改时间等: -a   ...

  2. centos7中将tomcat注册为系统服务

    一.准备环境 操作系统:CentOS Linux release 7.6.1810 (Core) tomcat:apache-tomcat-8.5.41.tar.gz 二.编辑catalina.sh ...

  3. 【cf补题记录】Codeforces Round #607 (Div. 2)

    比赛传送门 这里推荐一位dalao的博客-- https://www.cnblogs.com/KisekiPurin2019/ A:字符串 B:贪心 A // https://codeforces.c ...

  4. 搭建git服务器配置gitolite[迁移原来的gitolite工程]

    参考 https://www.liaoxuefeng.com/wiki/896043488029600/899998870925664 http://www.worldhello.net/gotgit ...

  5. 加入mapstruct后出现 找不到符号 符号: 方法 setXX 的解决方法

    加入lombok解决 <build> <plugins> <plugin> <groupId>org.springframework.boot</ ...

  6. cad.net 图元延迟显示,动画效果,编辑器延迟发送提示.

    public class Command_test { [CommandMethod("tt", CommandFlags.Modal | CommandFlags.UsePick ...

  7. asbDio (asbDio.dll)

    using System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices ...

  8. samba服务器安装测试

    samba服务器安装 1.简介 作用:samba文件服务器允许通过网络跨步同的操作系统进行文件共享. 2.安装samba服务器 $ sudo apt-get update $ sudo apt-get ...

  9. Kafka部署篇

    目录 安装 下载与安装 配置 启停操作 验证 基本操作 创建topic 列出现有的topic 查看topic的详细信息 增加topic的partition数量 修改一个topic的副本数 删除一个to ...

  10. python模块、异常

    1. python 模块 模块是一个包含所有你定义的函数和变量的文件,其后缀名是.py.模块可以被别的程序引入,以使用该模块中的函数等功能.这也是使用 python 标准库的方法.(有点像java的c ...