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

关于bean的作用域,在spring中,主要包括singleton,prototype,session,request,global,本篇文章主要讲解常用的两种,即:singleton和prototype.

一  singleton

singleton为单例模式,即scope="singleton"的bean,在容器中,只实例化一次。

dao示例代码:

package com.demo.dao;

public class UserDao {

    public UserDao(){
System.out.println("UserDao 无参构造函数被调用");
}
//获取用户名
public String getUserName(){
//模拟dao层
return "Alan_beijing";
}
}

applicationContext.xml

<?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.xsd"> <bean class="com.demo.dao.UserDao" id="userDao" scope="singleton"/>
</beans>

test:

public class MyTest {

    @Test
public void test(){
//定义容器并初始化
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); //定义第一个对象
UserDao userDao = applicationContext.getBean(UserDao.class);
System.out.println(userDao.getUserName()); //定义第二个对象
UserDao userDao2 = (UserDao) applicationContext.getBean("userDao");
System.out.println(userDao2.getUserName());
//比较两个对象实例是否是同一个对象实例
System.out.println("第一个实例:"+userDao+"\n"+"第二个实例:"+userDao2);
}
}

测试结果:

分析:在测试代码中,将bean定义为singleton,并先后2次通过ApplicationContext的getBean()方法获取bean(userDao),却返回相同的实例对象:com.demo.dao.UserDao@27a5f880,仔细观察,虽然获取bean两次,但是UserDao的无参构造函数却只被调用一次,这也证明了在容器中,singleton实际只被实例化一次,需要注意的是,Singleton模式的bean,ApplicationContext加载bean时,就实例化了bean。

定义bean:

测试结果:

如下代码只是加载bean,却没调用getBean方法获取bean,但UserDao却被调用了一次,即实例化。

二 prototype

prototype即原型模式,调用多少次bean,就实例化多少次。

将singleton代码改为原型

<?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.xsd"> <bean class="com.demo.dao.UserDao" id="userDao" scope="prototype"/>
</beans>

测试代码与singleton一样,但结果却不一样:

分析:通过测试结果,不难发现,调用两次bean,就实例化两次UserDao对象,且对象不一样,需要注意的是,prototype类型的bean,只有在获取bean时,才会实例化对象。

三 singleton和prototype区别

(1)singleton在容器中,只被实例化一次,而prototype在容器中,调用几次,就被实例化几次;

(2)在AppplicationContext容器中,singleton在applicaitonContext.xml加载时就被预先实例化,而prototype必须在调用时才实例化

singleton:

定义bean:

测试:

prototype:

定义bean:

测试:不调用

测试:调用

4.singleton比prototype消耗性能,在web开发中,推荐使用singleton模式,在app开发中,推荐使用prototype模式。

四 版权区

转载博客,必须注明博客出处
 博客园:http://www.cnblogs.com/wangjiming/ (侧重.NET)
 CSDN:https://blog.csdn.net/u010228798  (侧重JAVA)
 如您有新想法,欢迎提出,邮箱:2098469527@qq.com
 专业.NET之家技术QQ群:490539956
 专业化Java之家QQ群:924412846
  有问必答QQ群:2098469527
   一对一技术辅导QQ:2098469527

通俗易懂spring之singleton和prototype的更多相关文章

  1. [Spring Boot] Singleton and Prototype

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

  2. spring中scope的prototype与singleton区别

    最近在研究单例模式,突然想起项目中以下配置,scope="singleton" 和 scope="prototype"到底有何区别呢?以下做下简要分析. < ...

  3. Singleton and Prototype Bean Scope in Spring

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

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

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

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

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

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

  7. singleton和prototype的区别

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

  8. 辨析 singleton 和 prototype

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

  9. 转:spring mvc 设置@Scope("prototype")

    spring中bean的scope属性,有如下5种类型: singleton 表示在spring容器中的单例,通过spring容器获得该bean时总是返回唯一的实例prototype表示每次获得bea ...

随机推荐

  1. Two Graphs 牛客网暑期ACM多校训练营(第一场)D 图论基础知识 全排列

    链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 Two undirected simple graphs and where are isomo ...

  2. 对git使用的初步总结

    使用git也才一周多,就已经深深爱上这款软件了. 之前公司一直用的是clearcase,一款老到除了公司内部的人和曾经开发这款软件的人,估计再也不会有人知道了吧! (当然也许还会有其他公司也会使用,因 ...

  3. R语言基础入门

    请先安装好R和RStudio 如果不干别的,控制台就是一个内置计算器 2 * 3 #=> 6 sqrt(36) #=> 6, square root log10(100) #=> 2 ...

  4. Marrkdown基础用法

    目录 前言 markdown简介 用法列表 标题 字符效果和横线 引用 锚点与链接 代码高亮 图片 有序列表&无序列表 表格 特殊符号与颜色处理 markdown进阶技巧 参考文章 前言 因为 ...

  5. Appium+python自动化(三十六)- 士兵突击许三多 - 多个appium服务启动,多个设备启动,多进程并发启动设备-并发测试 - 上(超详解)

    简介 前面课程只是启动了单个appium服务,只能控制单台设备.如果需要针对多台设备测试那么该如何处理?而且发现群里的小伙伴们也在时不时地在讨论这个问题,想知道怎么实现的,于是宏哥就决定写一片这样的文 ...

  6. SSM相关面试题(简答)

    1.springmvc的执行    流程: 2.mybstis的执行流程: 3.ioc和DI的理解: 4.对aop的理解: 5.spring中常见的设计模式: 6.spring中声明式事务处理的配置: ...

  7. out.print()与response.sendRedirect()

    这样的情况下 out.print("<script>alert('非法操作,请重新登录!');</script>"); response.sendRedir ...

  8. jsp学习:jsp学习阶段性总结2019.9.21

    Jsp学习 jsp语法格式: 脚本程序:<% 代码片段 %> jsp声明:<%! declaration; [ declaration; ]+ ... %> 表达式:<% ...

  9. 深入理解JVM内存分配策略

    理解JVM内存分配策略 三大原则+担保机制 JVM分配内存机制有三大原则和担保机制 具体如下所示: 优先分配到eden区 大对象,直接进入到老年代 长期存活的对象分配到老年代 空间分配担保 对象优先在 ...

  10. 字符串之————三向字符串快速排序(Quick3string)

    上一篇介绍了字符串的两种经典排序方法(LSD MSD): https://www.cnblogs.com/Unicron/p/11531111.html 下面我们来介绍一种通用的字符串排序方法——三向 ...