简单了解spring bean作用域属性singleton和prototype的区别

 更新时间:2019年12月24日 11:30:58   作者:何其有静  
这篇文章主要介绍了简单了解spring bean作用域属性singleton和prototype的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

这篇文章主要介绍了简单了解spring bean作用域属性singleton和prototype的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1.singleton

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

换言之,当把一个bean定义设置为singleton作用域时,Spring IOC容器只会创建该bean定义的唯一实例。这个单一实例会被存储到单例缓存(singleton cache)中,并且所有针对该bean的后续请求和引用都将返回被缓存的对象实例,这里要注意的是singleton作用域和GOF设计模式中的单例是完全不同的,单例设计模式表示一个ClassLoader中只有一个class存在,而这里的singleton则表示一个容器对应一个bean,也就是说当一个bean被标识为singleton时候,spring的IOC容器中只会存在一个该bean。

applicationContextER.xml:

  <!--Spring bean作用域-->
  <bean id="get_date" class="java.util.Date" scope="singleton"/>

测试代码:

public class GetDate {
  public static void main(String[] args){
    //获取应用程序上下文接口
    ApplicationContext apl = new ClassPathXmlApplicationContext("applicationContextER.xml");
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
      //反复调用getBean来查看时间
      Date date = (Date) apl.getBean("get_date");
      //休息3秒
      Thread.sleep(1000);
      System.out.println("--------------:" + simpleDateFormat.format(date));

      Date date1 = (Date) apl.getBean("get_date");
      Thread.sleep(1000);
      System.out.println("--------------:" + simpleDateFormat.format(date1));

      Date date2 = (Date) apl.getBean("get_date");
      Thread.sleep(1000);
      System.out.println("--------------:" + simpleDateFormat.format(date2));

      System.out.println("date is date1 : " + (date == date1));
      System.out.println("date1 is date2 : " + (date1 == date2));
    } catch (Exception e) {

    }

  }
}

测试结果:

23:05:04.298 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'get_date'
23:05:04.298 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'get_date'
23:05:04.308 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'get_date' to allow for resolving potential circular references
23:05:04.309 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'get_date'
23:05:04.310 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@3108bc]
23:05:04.310 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
23:05:04.311 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source
23:05:04.316 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'get_date'
--------------:2019-12-21 23:05:04
23:05:05.320 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'get_date'
--------------:2019-12-21 23:05:04
23:05:06.324 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'get_date'
--------------:2019-12-21 23:05:04
date is date1 : true
date1 is date2 : true

从上面的结果可以看出,创建好对象之后,存入了缓存中。后面每次都是获取的对象都是从缓存中获取的,而不是新创建的。所以每次获取的对象都是一样的。

2.prototype

prototype作用域部署的bean,每一次请求(将其注入到另一个bean中,或者以程序的方式调用容器的getBean()方法)都会产生一个新的bean实例,相当与一个new的操作,对于prototype作用域的bean,有一点非常重要,那就是Spring不能对一个prototype bean的整个生命周期负责,容器在初始化、配置、装饰或者是装配完一个prototype实例后,将它交给客户端,随后就对该prototype实例不闻不问了。

不管何种作用域,容器都会调用所有对象的初始化生命周期回调方法,而对prototype而言,任何配置好的析构生命周期回调方法都将不会被调用。清除prototype作用域的对象并释放任何prototype bean所持有的昂贵资源,都是客户端代码的职责。(让Spring容器释放被singleton作用域bean占用资源的一种可行方式是,通过使用bean的后置处理器,该处理器持有要被清除的bean的引用。

applicationContextER.xml:

  <!--Spring bean作用域-->
  <bean id="get_date" class="java.util.Date" scope="prototype"/>

测试结果:

23:01:51.314 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'get_date'
23:01:51.324 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'get_date'
--------------:2019-12-21 23:01:51
23:01:52.329 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'get_date'
23:01:52.329 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'get_date'
--------------:2019-12-21 23:01:52
23:01:53.330 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'get_date'
23:01:53.331 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'get_date'
--------------:2019-12-21 23:01:53
date is date1 : false
date1 is date2 : false

从上面的结果可以看出,每次都是创建一个新对象,所以每次的对象都不一样。

总结:从1和2可以看出,当你需要全局的唯一标示的时候可以用singleton,而且singleton只创建一个对象,系统消耗资源小.但是用singleton可能会有线程安全化的问题,这个时候就需要用到prototype 。考虑并发的问题,建议都用prototype。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java基于Tcp/ip连接的多人交互聊天室

    Java基于Tcp/ip连接的多人交互聊天室

    这篇文章主要为大家详细介绍了Java基于Tcp/ip连接的多人交互聊天室,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • Java动态代理(设计模式)代码详解

    Java动态代理(设计模式)代码详解

    这篇文章主要介绍了Java动态代理(设计模式)代码详解,具有一定借鉴价值,需要的朋友可以参考下
    2017-12-12
  • SpringBoot自动装配原理详细解析

    SpringBoot自动装配原理详细解析

    这篇文章主要介绍了SpringBoot自动装配原理详细解析,一个对象交给Spring来管理的三种方式 @Bean @Compoment @Import,
    @Bean主要在@Configuration中,通过方法进行注入相关的Bean,@Compoent与@Service归为一类,在类上加注入对应的类,需要的朋友可以参考下
    2024-01-01
  • Maven下 mybatis-generator使用

    Maven下 mybatis-generator使用

    这篇文章主要介绍了Maven下 mybatis-generator使用 ,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-09-09
  • 分享几个Java工作中实用的代码优化技巧

    分享几个Java工作中实用的代码优化技巧

    这篇文章主要给大家分享几个Java工作中实用代码优化技巧,文章基于Java的相关资料展开对其优化技巧的分享,需要的小伙伴可以参考一下
    2022-04-04
  • 基于Session的国际化实现方法

    基于Session的国际化实现方法

    下面小编就为大家带来一篇基于Session的国际化实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-08-08
  • SpringBoot Jackson日期格式化统一配置的实现

    SpringBoot Jackson日期格式化统一配置的实现

    Spring项目中经常需要配置日期时间格式格式,本文主要介绍了SpringBoot Jackson日期格式化统一配置的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-08-08
  • Spring之异步任务@Async解读

    Spring之异步任务@Async解读

    这篇文章主要介绍了Spring之异步任务@Async,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • Spring Boot 优雅停机原理详解

    Spring Boot 优雅停机原理详解

    这篇文章主要为大家介绍了Spring Boot 优雅停机原理详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • Java中new Date().getTime()指定时区的时间戳问题小结

    Java中new Date().getTime()指定时区的时间戳问题小结

    本文主要介绍了Java中new Date().getTime()时间戳问题小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07

最新评论