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定义相匹配,则只会 ...
随机推荐
- TreeMap按照value进行排序
TreeMap底层是根据红黑树的数据结构构建的,默认是根据key的自然排序来组织(比如integer的大小,String的字典排序).所以,TreeMap只能根据key来排序,是不能根据value来排 ...
- C# 通过后台获取浏览器域名
通过httpContext.获取当前地址当前主机域名 string url = HttpContext.Current.Request.Url.Host.ToString();
- perl reverse 函数
参考 http://www.perlcn.com/perlbc/perljc/315.html 使用reverse操作符时,perl会先计算变量的值,也就是=右边的值,然后再进行复制,如果revers ...
- Mysql将近两个月的记录合并为一行显示
最近做报表统计,用到要求把近两个月的绩效作比较,并作出一些环比数据等. 场景:将1班同学的两个月的语文的平均成绩合并到一行比较. CREATE TABLE `Chinese_score` ( `id` ...
- [日常训练]FJ省夏令营day1
T1 Description 给出n个矩形的顶点坐标(每个矩形的底边都在x轴上),求这n个矩形所组成图形的轮廓线的顶点. Input 第一行一个整数n,表示矩形个数. 以下n行,每行3个整数,分别表示 ...
- Windows批处理以服务的方式启动解决思路(ShadowsockR注册成Windows Service)
我以ShadowsockR的server启动来解释: 由于这东西是python,如果要启动,可以写一个批处理(python.exe server.py)来启动,但是我观察发现启动的时候是附带pytho ...
- struts2 CVE-2012-0838 S2-007 Remote Code Execution && Hotfix
catalog . Description . Effected Scope . Exploit Analysis . Principle Of Vulnerability . Patch Fix 1 ...
- 分析ffmpeg解析ts流信息的源码
花费一些时间,然后全部扔了.为了不忘记和抛砖引玉,特发此贴. ffmpeg解析ts流 1.目的 打算软件方式解析出pat,pmt等码流信息 2.源代码所在位置 下载ffmpeg ...
- 数据结构作业——N!的位数(斯特灵公式)
Description 求N!的位数 Input 输入第一行为一个正整数 n(1<=n<=25000). Output 输出 n!的位数. Sample Input 1020 Sample ...
- Python KeyError
Google一下轻松找到了答案,大家可以看一下Python Wiki,很简单,翻译如下. 在Python中,当你使用a[key]这种方式从字典中获取一个值时,若字典中不存在这个此key时就会产生一个K ...