关于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. hdu 1176免费馅饼(记忆化搜索)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1176 题意不解释了 简单的记忆化搜索可以拿来练练手,注意要从pos = 5 开始搜索 #include ...

  2. Hangman Judge UVA - 489

    In ``Hangman Judge,'' you are to write a program that judges a series of Hangman games. For each gam ...

  3. poj1037 [CEOI 2002]A decorative fence 题解

    ---恢复内容开始--- 题意: t组数据,每组数据给出n个木棒,长度由1到n,除了两端的木棒外,每一根木棒,要么比它左右的两根都长,要么比它左右的两根都短.即要求构成的排列为波浪型.对符合要求的排列 ...

  4. 解决flutter:unable to find valid certification path to requested target 的问题

    1.问题 周末在家想搞搞flutter,家里电脑是windows的,按照官网教程一步步安装好以后,创建flutter工程,点击运行,一片红色弹出来,WTF? PKIX path building fa ...

  5. 用.NET写“算命”程序

    用.NET写"算命"程序 "算命",是一种迷信,我父亲那一辈却执迷不悟,有时深陷其中,有时为求一"上上签",甚至不惜重金,向"天神 ...

  6. spring的嵌套事务

    转自http://www.iteye.com/topic/35907 在所有使用 spring 的应用中, 声明式事务管理可能是使用率最高的功能了, 但是, 从我观察到的情况看, 绝大多数人并不能深刻 ...

  7. 服务器替换san存储

    1.通知DBA停库: 串行登陆服务器 2.备份系统信息 mkdir -p /bakinfo df -h > /bakinfo/df.txt_`date +%Y%m%d%H%M%S` ps -ef ...

  8. Java I/O系统学习四:标准IO

    几乎所有学习Java的同学写的第一个程序都是hello world,使用的也都是System.out.println()这条语句来输出"hello world",我也不例外,当初学 ...

  9. Docker Gitlab CI 部署 Spring Boot 项目

    目前在学习这一块的内容,但是可能每个人环境都不同,导致找不到一篇博客能够完全操作下来没有错误的,所以自己也写一下,记录一下整个搭建的过程. Docker 的安装这里就不赘述了,基本上几行命令都可以了, ...

  10. java+selenium-3.9.1多线程 打开连接截取屏幕截图

    废话不多说上代码:(我是用的chrome举得例子哈) 第一步,需要chromedriver.exe 目的和调起chrome 浏览器打开连接,chromedriver.exe的版本与你的chrome版本 ...