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定义相匹配,则只会 ...
随机推荐
- Web前端性能优化教程05:网站样式和脚本
本文是Web前端性能优化系列文章中的第五篇,主要讲述内容:网站样式和脚本代码的放置位置.使用外部javascript和css.完整教程可查看:Web前端性能优化 一.将样式表放在顶部 可视性回馈的重要 ...
- VI,CI,UI
一.VI VI全称Visual Identity, 即企业VI视觉设计,通译为视觉识别系统.是将CI的非可视内容转化为静态的视觉识别符号.设计到位.实施科学的视觉识别系统,是传播企业经营理念.建立企业 ...
- Leetcode 375. Guess Number Higher or Lower II
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
- Leetcode 95. Unique Binary Search Tree II
由于BST的性质,所以右子树或者左子树中Node的值是连续的: 左子树 = [1, i -1], root = i, 右子树 = [i + 1, n].使用一个递归函数构造这个BST.其中返回值应该是 ...
- POJ3764 The xor-longest Path
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6361 Accepted: 1378 Description In ...
- dedecms /member/myfriend_group.php SQL Injection Vul
catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 Dedecms会员中心注入漏洞 Relevant Link http:/ ...
- Entity Framework使用Sqlite时的一些配置
前段时间试着用Entity Framework for Sqlite环境,发现了一些坑坑洼洼,记录一下. 同时试了一下配置多种数据库,包括Sqlite.Sql Server.Sql Server Lo ...
- HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)
题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS Memory Limit: 32768 K Description The inve ...
- linux 设置mysql 数据库编码utf8
转载地址http://www.linuxidc.com/Linux/2015-08/121676.htm在MySQL数据库中, 当插入数据无法支持中文时, 可将该数据库的编码集设置为utf8, 故在创 ...
- C语言函数指针的用法
函数指针是一种在C.C++.D语言.其他类 C 语言和Fortran 2003中的指针.函数指针可以像一般函数一样,用于调用函数.传递参数.在如 C 这样的语言中,通过提供一个简单的选取.执行函数的方 ...