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定义相匹配,则只会 ...
随机推荐
- Linux下执行.sh文件
Linux下执行.sh文件有两种情况: 一.直接./加上文件名.sh,如运行hello.sh为./hello.sh[hello.sh必须有x权限] 二.直接sh 加上文件名.sh,如运行hello.s ...
- 酶切位点分析(the analysis of enzyme sites)
转自 http://www.yelinsky.com/blog/archives/278.html 稍有修改 默认位点为"CCGG".其他位点分析可修改脚本中的 my $site ...
- js-JavaScript高级程序设计学习笔记18
第21章 AJAX 4.跨域源资源共享 CORS跨域源资源共享,其背后思想,是使用自定义的HTTP头部让浏览器与服务器进行沟通,从而决定请求或响应是否应该成功. 1.IE对CORS的实现 在IE8中引 ...
- js-自制轮播插件!
刚接触轮播的时候,感觉这种东西好高端,后来学习了jquery后,发现原来挺简单的,而且实现轮播也有很多形式,我用jquery自制了一个轮播插件,其实我这个说是插件,好像其实就是一个高度抽象的函数而已. ...
- 高级语言虚拟机的一点理解,对比.NET和Java平台
最近学习了一些高级语言虚拟机的知识,在此对.NET平台和java平台做一个简单的比较.对java平台已经很熟了,所以此处只介绍.NET平台下的一些概念. 一.CLI 通用语言基础架构(Common L ...
- Leetcode Find Minimum in Rotated Sorted Array I and II
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- Git: 生成ssh公钥
生成 SSH 公钥 大多数 Git 服务器都会选择使用 SSH 公钥来进行授权.系统中的每个用户都必须提供一个公钥用于授权,没有的话就要生成一个.生成公钥的过程在所有操作系统上都差不多. 首先先确认一 ...
- EF-CodeFirst-3搞事
本文学习旺杰兄的 CodeFirst 系列教程而写.尽量摆脱之前的影子写出自己的理解 表间关系.级联删除 简单玩法已经走通了,但是我就是想搞点事出来.今天来搞搞表间关系和级联删除 表间关系 毫无疑问在 ...
- MySQL@淘宝 资料分享
MySQL@淘宝 在过去两年, 淘宝数据库团在MySQL.SSD.开源迈出了巨大的步伐,截至11年十月用户数据库库.商品库.交易库都已经稳定的运行在MySQL上,同时也经历的双十一,双十二的考验.这里 ...
- git实习笔记
一.查找文件目录 二.添加上传文件 三.提交文件,描述信息 四.登录