singleton和prototype
public class testScope {
@Test
public void test() {
//默任singleton
ApplicationContext ctx= new ClassPathXmlApplicationContext(
"applicationContext.xml");
//客户端1拿到的东西
User user = ctx.getBean("user",User.class);
System.out.println(user.getUsername());
user.setUsername(user.getUsername()+"1");
//客户端2拿到的东西(对唯一实例的修改)
User user2 = ctx.getBean("user",User.class);
System.out.println(user2.getUsername());
user2.setUsername(user.getUsername()+"2");
//客户端3拿到的东西(对唯一实例的修改)
User user3 = ctx.getBean("user",User.class);
System.out.println(user3.getUsername());
System.out.println(user==user2);
System.out.println(user==user3);
}
}
去掉scope="prototype"就是scope="singleton"
1 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd ">
<bean id="userDao" class="com.bdqn.dao.UserDaoImpl"></bean>
<bean id="userService" class="com.bdqn.service.UserService">
<!-- setter方法 -->
<property name="dao" ref="userDao"></property>
<!-- 构造方法注入 index编号从0开始-->
<constructor-arg index="0" ref="userDao"></constructor-arg>
<!-- ref引用 -->
</bean>
<!-- singleton -->
<bean id="user" class="com.bdqn.entity.User" scope="prototype">
<property name="username" value="admin"></property>
<property name="password" value="123"></property>
</bean>
singleton:所有的客户端拿到的都是同一个实例,所以后面都是加上去,输出的是admin,admin1,admin2 ,true,true
prototype:所有客户端拿到的都是自己的实例,输出的都是admin,admin,admin,false,false
singleton和prototype的更多相关文章
- singleton和prototype的区别
singleton作用域:当把一个Bean定义设置为singleton作用域是,Spring IoC容器中只会存在一个共享的Bean实例,并且所有对Bean的 请求,只要id与该Bean定义相匹配,则 ...
- [Spring] Bean Scope Singleton cs Prototype
We can define a class to be Singleton or Prototype. If the class was defined as Prototype, then ever ...
- 辨析 singleton 和 prototype
<bean id="person1" class="com.bean.life.Person"> <property name="n ...
- Singleton and Prototype Bean Scope in Spring
Scope描述的是Spring容器如何新建Bean的实例的. 1> Singleton: 一个Spring容器只有一个Bean的实例,此为Spring的默认配置,全容器共享一个实例. 2> ...
- 通俗易懂spring之singleton和prototype
关于spring bean作用域,基于不同的容器,会有所不同,如BeanFactory和ApplicationContext容器就有所不同,在本篇文章,主要讲解基于ApplicationContext ...
- 【Spring】bean的作用域(@Scope) - singleton、prototype
已知spring 3+已拥有多种不同的作用域: singleton(默认).prototype.request.session.global session.(参考: spring中scope作用域( ...
- spring创建bean模式singleton与prototype的区别
spring 创建bean有单例模式(singleton)和原始模型模式(prototype)这两种模式. 在默认的情况下,Spring中创建的bean都是单例模式的(注意Spring的单例模式与Go ...
- [Spring Boot] Singleton and Prototype
When we use Bean to do autowired, it actually use singleton, so even we create multi instanses, they ...
- spring中bean的作用域属性singleton与prototype的区别
1.singleton 当一个bean的作用域设置为singleton, 那么Spring IOC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会 ...
随机推荐
- OneThink学习笔记01
OneThink1.0开发手册: http://document.onethink.cn/manual_1_0.html 简介 OneThink是一个开源的内容管理框架,基于最新的ThinkPHP3 ...
- bzoj2821: 作诗(Poetize)
分块 分sqrt(n)块 F[i][j]表示块i到块j的答案 s[i][j]表示数字i在前j块内出现了几次 #include <iostream> #include <cstdio& ...
- asp.net项目发布打包研究
有几种思路: 1.[推荐]直接发布,然后手动打包成压缩包,需要的时候直接上传到服务器,或者在本地解压出来手动上传到虚拟空间(支持绝大多数的虚拟空间,自由度高,DZ也是采用这样的打包,FTP上传操作比较 ...
- poj2284 欧拉公式
题意:给出一图形,求该图形把平面分成了几部分 欧拉公式: http://blog.csdn.net/wangxiaojun911/article/details/4586550 对于二维平面上的情况. ...
- 通过Minimal版的iso安装CentOS7之后升级Desktop
重新安装了CentOS7,但是使用的是Minimal的iso镜像安装的,所以安装之后只有文本界面,这里记录一下重新安装图形界面的过程. 连接网络 通过文本界面登陆后是没有连接网络的,所以需要修改配置连 ...
- no suitable HttpMessageConverter found for request type [java.lang.Integer]
今天在使用Spring Template的时候遇到了这个异常: no suitable HttpMessageConverter found for request type [java.lang.I ...
- AngularJs $interval 和 $timeout
$interval window.setInterval的Angular包装形式.Fn是每次延迟时间后被执行的函数. 间隔函数的返回值是一个承诺.这个承诺将在每个间隔刻度被通知,并且到达规定迭代次数后 ...
- PHP ServerPush (推送) 技术的探讨
2016年11月29日17:51:03 转自:http://www.cnblogs.com/hnrainll/archive/2013/05/07/3064874.html 需求: 我想做个会员站内通 ...
- Extjs GridPanel用法详解
Extjs GridPanel 提供了非常强大数据表格功能,在GridPanel可以展示数据列表,可以对数据列表进行选择.编辑等.在之前的Extjs MVC开发模式详解中,我们已经使用到了GridPa ...
- 新创建一个git远程仓库
1.git 服务器项目初始化已经完毕,请把相关的资料和源码上传到git服务器. 2.第一次需要clone,执行命令:git clone git@192.168.10.184:listenbox_mc_ ...