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的更多相关文章

  1. singleton和prototype的区别

    singleton作用域:当把一个Bean定义设置为singleton作用域是,Spring IoC容器中只会存在一个共享的Bean实例,并且所有对Bean的 请求,只要id与该Bean定义相匹配,则 ...

  2. [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 ...

  3. 辨析 singleton 和 prototype

    <bean id="person1" class="com.bean.life.Person"> <property name="n ...

  4. Singleton and Prototype Bean Scope in Spring

    Scope描述的是Spring容器如何新建Bean的实例的. 1> Singleton: 一个Spring容器只有一个Bean的实例,此为Spring的默认配置,全容器共享一个实例. 2> ...

  5. 通俗易懂spring之singleton和prototype

    关于spring bean作用域,基于不同的容器,会有所不同,如BeanFactory和ApplicationContext容器就有所不同,在本篇文章,主要讲解基于ApplicationContext ...

  6. 【Spring】bean的作用域(@Scope) - singleton、prototype

    已知spring 3+已拥有多种不同的作用域: singleton(默认).prototype.request.session.global session.(参考: spring中scope作用域( ...

  7. spring创建bean模式singleton与prototype的区别

    spring 创建bean有单例模式(singleton)和原始模型模式(prototype)这两种模式. 在默认的情况下,Spring中创建的bean都是单例模式的(注意Spring的单例模式与Go ...

  8. [Spring Boot] Singleton and Prototype

    When we use Bean to do autowired, it actually use singleton, so even we create multi instanses, they ...

  9. spring中bean的作用域属性singleton与prototype的区别

    1.singleton 当一个bean的作用域设置为singleton, 那么Spring IOC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会 ...

随机推荐

  1. Mysql 5.7.12解压版的安装及配置系统编码

    这篇博文是由于上篇EF+MySql博文引发的,上篇博文中在Seed方法中插入中文数据到Mysql数据库中乱码,后来网上找了N种方法也没解决.重装了MySql并在安装过程中配置了系统编码,此篇记录一下. ...

  2. json2form实例

    <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  3. 运行python代码报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 91: ordinal not in range(128)的解决办法

    1.通过搜集网上的资料,自己多次尝试,问题算是解决了,在代码中加上如下几句即可: import sys reload(sys) sys.setdefaultencoding('utf-8') 2.原因 ...

  4. BZOJ 4144: [AMPPZ2014]Petrol

    4144: [AMPPZ2014]Petrol Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 457  Solved: 170[Submit][Sta ...

  5. SSH无密码登陆Agent admitted failure to sign using the key

    A :CentOS_Master B:Slave_1 C:Slave_2 普通用户hxsyl 1.现在A 上 ssh-keygen -t rsa 一路回车,不需要输入密码 执行该操作将在/home/h ...

  6. ubuntu中maven建的web项目不能将project facet设置为 dynamic web module 3.0

    核心参考:maven 不能设置为web3.0人解决方法 error:Description    Resource    Path    Location    Type Cannot change ...

  7. Deformity JSP Webshell、Webshell Hidden Learning

    catalogue . JSP基础语法 . JSP Lexer By Lua . Open Source Code Analyzers in Java . WEBSHELL Samples . she ...

  8. PHP框架中的日志系统

    现在在一家公司做PHP后台开发程序猿(我们组没有前端,做活动时会做前端的东西),刚开始到公司的时候花2个周赶出了一个前端加后台的活动(记得当时做不出来周末加了两天班...),到现在过去4个多月了,可以 ...

  9. C# 生成xml文件

    本篇文章旨在.net环境下生成xml文件,以控制台应用程序为例进行说明. 1.在vs中新建控制台应用程序CreateXml 2.CreateXmlFile:主要生成xml的函数 public void ...

  10. PHP之:析构函数

    如何正确理解PHP析构函数 参考文献:http://developer.51cto.com/art/200912/167023.htm 初次学习PHP语言的朋友们可能对PHP析构函数有些不太了解.我们 ...