转:spring mvc 设置@Scope("prototype")
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")的更多相关文章
- spring mvc 设置@Scope("prototype")
spring中bean的scope属性,有如下5种类型: singleton 表示在spring容器中的单例,通过spring容器获得该bean时总是返回唯一的实例 prototype表示每次获得be ...
- spring bean中scope="prototype“的作用
今天写代码时,遇到个问题,问题大概如下:在写一个新增模块,当各文本框等输入值后,提交存入数据库,跳到其它页面,当再次进入该新增页面时,上次输入的数据还存在. 经过检查发现是,spring配置文件中,配 ...
- spring MVC设置不拦截静态资源
问题产生: 因为我们在web.xml中写了 拦截所有请求,当然包括了静态资源,所以页面需要引用css或js的话,该请求也会被拦截,例如: 在style.css中写一个简单样式,加个背景颜色 body ...
- spring mvc 设置设置默认首页的方式
背景: 项目使用springmvc管理请求,有一个小的需求,输入域名的时候自动进入某个页面(或者说自动发起某个请求). 过程: 1,首先想到 在web.xml中配置welcome-file-list的 ...
- spring mvc设置字符集过滤器
<filter> <filter-name>springEncoding</filter-name> <filter-class> org.spring ...
- spring入门(八) spring mvc设置默认首页
1.web.xml配置如下 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3// ...
- Spring MVC 设置UTF-8编码
按照需求选其中之一即可吧. 修改读取参数时候的编码: 在web.xml中: 添加一个过滤器(filter),注册 org.springframework.web.filter.CharacterEnc ...
- SSM(spring mvc+spring+mybatis)学习路径——1-1、spring入门篇
目录 1-1 Spring入门篇 专题一.IOC 接口及面向接口编程 什么是IOC Spring的Bean配置 Bean的初始化 Spring的常用注入方式 专题二.Bean Bean配置项 Bean ...
- Spring MVC 处理静态资源文件
摘要: 三个方案: 1.方案一:激活Tomcat的defaultServlet来处理静态文件 2.方案二: 在spring3.0.4以后版本提供了mvc:resources (需要配置annotati ...
随机推荐
- vue+element 表单验证
效果图 <template> <div class="formValidator"> <div v-for="(item,index) in ...
- 淘宝IP地址库获取到省市IP地址
http://ip.aliyun.com/index.html https://ispip.clang.cn/ https://github.com/Pingze-github/local-ips 1 ...
- Linux安装vim,解决vim: command not found
1,输入rpm -qa|grep vim 命令, 如果 vim 已经正确安裝,会返回下面的三行代码: root@server1 [~]# rpm -qa|grep vim vim-enhanced-7 ...
- matlab 彩色图像转化成灰度图像,灰度图像降低灰度级
灰度级数k,k=2^b,称该图像为b比特图像. 降低灰度级数是靠2的幂次方 网上代码:https://blog.csdn.net/silence2015/article/details/6892736 ...
- 免费https证书
https://certbot.eff.org/lets-encrypt/ubuntuother-nginx https://ruby-china.org/topics/31942 https://l ...
- 【视频开发】伽马校正(gamma correction)学习笔记
我相信几乎所有做图像处理方面的人都听过伽马校正(Gamma Correction)这一个名词,但真正明白它是什么.为什么要有它.以及怎么用它的人其实不多.我也不例外. 最初我查过一些资料,但很多文章 ...
- QuantLib 金融计算——基本组件之 Money 类
目录 QuantLib 金融计算--基本组件之 Money 类 概述 构造函数 成员函数 如果未做特别说明,文中的程序都是 python3 代码. QuantLib 金融计算--基本组件之 Money ...
- CSS3手机端字体不能小于12号的方法
CSS3手机端字体不能小于12号的方法 <pre> .xiaoyu12fontsize{ -webkit-transform-origin: 0% 0%; -webkit-transfor ...
- xunsearch安装
1.下载 sudo wget http://www.xunsearch.com/download/xunsearch-full-latest.tar.bz2 2.解压 sudo tar -xvjf x ...
- [转帖]从 SOA 到微服务,企业分布式应用架构在云原生时代如何重塑?
从 SOA 到微服务,企业分布式应用架构在云原生时代如何重塑? 2019-10-08 10:26:28 阿里云云栖社区 阅读数 54 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权 ...