关于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. 【百度之星】【java大数+C++做法】hdu 6719 Strassen

    代码:递归搜索一下.java大数做法 import java.util.*; import java.math.*; import java.security.MessageDigest; publi ...

  2. Treasure Hunt CodeForces - 979B

    After the big birthday party, Katie still wanted Shiro to have some more fun. Later, she came up wit ...

  3. poj 3159 Candies(dijstra优化非vector写法)

    题目链接:http://poj.org/problem?id=3159 题意:给n个人派糖果,给出m组数据,每组数据包含A,B,c 三个数,意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的 ...

  4. mybatis转义

    SELECT * FROM test WHERE 1 = 1 AND start_date <= CURRENT_DATE AND end_date >= CURRENT_DATE 在执行 ...

  5. .net core 部署到windows上的方法与 系统中相关问题的解决

    前言 Net core 项目部门在Windows有很多种方式,大致有以下几种, dotnet 命令, iis(windowshosts), 一些开源的应用容器(docker ) 基于一些exe 程序, ...

  6. 通过Service访问应用 (1)

    目录 通过Service访问应用  通过Pod IP访问应用  通过ClusterIP Service在集群内部访问  通过Service访问应用 通过之前的操作,应用部署完成了,我们的Demo网站已 ...

  7. Jenkins流水线(pipeline)实战之:从部署到体验

    关于Jenkins流水线(pipeline) Jenkins 流水线 (pipeline) 是一套插件,让Jenkins可以实现持续交付管道的落地和实施. 关于blueocean Blue Ocean ...

  8. jstat虚拟机统计信息监视工具

    jstsat(JVM Statistics Monitoring Tool) jstat用于监视虚拟机各种运行状态信息的命令工具.可以显示本地或者远程虚拟机进程中的类装载.内存.垃圾收集.JIT编译等 ...

  9. Mybatis值ResultMap的使用详解

    Mybatis的定义 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis ...

  10. .NET分布式大规模计算利器-Orleans(一)

      写在前面 Orleans是基于Actor模型思想的.NET领域的框架,它提供了一种直接而简单的方法来构建分布式大规模计算应用程序,而无需学习和应用复杂的并发或其他扩展模式.我在2015年下半年开始 ...