When we use Bean to do autowired, it actually use singleton, so even we create multi instanses, they are the same:

@SpringBootApplication
public class In28minutesApplication { public static void main(String[] args) {
// Application Context
ApplicationContext applicationContext =
SpringApplication.run(In28minutesApplication.class, args);
//BinarySearchImpl binarySearch = new BinarySearchImpl(new QuickSortAlgo());
BinarySearchImpl binarySearch = applicationContext.getBean(BinarySearchImpl.class);
BinarySearchImpl binarySearch1 = applicationContext.getBean(BinarySearchImpl.class);
int result = binarySearch.binarySearch(new int[] {,,,}, );
System.out.println(binarySearch);
System.out.println(binarySearch1); }
}

It print out:

com.example.in28minutes.BinarySearchImpl@704deff2
com.example.in28minutes.BinarySearchImpl@704deff2

We can also tell Spring boot to use Singleton or using proptype:

@Component
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON) // by default
public class BinarySearchImpl { } // the same as @Component
public class BinarySearchImpl { }

But if we switch to Prototype, it will use differnet address in memory:

@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class BinarySearchImpl { }
com.example.in28minutes.BinarySearchImpl@4eaf3684
com.example.in28minutes.BinarySearchImpl@40317ba2

[Spring Boot] Singleton and Prototype的更多相关文章

  1. 通俗易懂spring之singleton和prototype

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

  2. Singleton and Prototype Bean Scope in Spring

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

  3. spring中scope的prototype与singleton区别

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

  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. Spring Boot实战笔记(二)-- Spring常用配置(Scope、Spring EL和资源调用)

    一.Bean的Scope Scope描述的是Spring容器如何新建Bean实例的.Spring的Scope有以下几种,通过@Scope注解来实现. (1)Singleton:一个Spring容器中只 ...

  8. Java Spring Boot VS .NetCore (三)Ioc容器处理

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  9. Spring Boot实战

    Spring在java EE开发中是实际意义上的标准,但我们在开发Spring的时候可能会遇到以下令人头疼的问题: 1.大量配置文件的定义.2.与第三方软件整合的技术问题. Spring每个版本的退出 ...

随机推荐

  1. C#中如何选择使用T[]或List<T>

    当有一组数据需要存放,到底是使用T[]呢,还是选择List<T>呢? 先来看数组. 所有的数组类型都隐式地从System.Array这个抽象类派生,而System.Array又派生自Sys ...

  2. 使用Application.GetResourceStream方法加载资源时得到的总是null

    我们可以预先把程序中用到的资源,如图片,音乐等放入项目中,打包进XAP文档,需要的时候从中调用.下面就说说具体实现方法. 第一步,把数据存进项目. 1.右键点击项目名称-添加-新建文件夹(英文版请自行 ...

  3. Visual Studio 2013 sqlce 配置(转)

    Visual Studio 2013 把內建 SQL CE 的管理工具拿掉了 下载SQL Server Compact Toolbox by ErikEJ并安装 打开VS2013,新建一工程,在“视图 ...

  4. 内存溢出导致jenkins自动部署到tomcat失败

    原文地址:http://openwares.net/java/jenkens_deploy_to_tomcat_error_of_outofmemoryerror.html jenkins自动部署wa ...

  5. C#编程(七十三)----------浅析C#中内存管理

    浅析C#中内存管理 前言:个人觉得C#吸收了各种语言的优点,可谓集大成者,但是不知但,这种集所有语言于一身的情况是好是坏.C#编程的一个优点就是程序员不需要关心具体的内存管理,尤其是垃圾收集器会处理所 ...

  6. springboot redis多数据源设置

    遇到这样一个需求:运营人员在发布内容的时候可以选择性的发布到测试库.开发库和线上库. 项目使用的是spring boot集成redis,实现如下: 1. 引入依赖 <dependency> ...

  7. linux下vi操作Found a swap file by the name

    当我在linux下用vi打开Test.java文件时 [root@localhost tmp]# vi Test.java 会出现如下信息: E325: ATTENTION Found a swap  ...

  8. BZOJ 3172 [Tjoi2013]单词 AC自己主动机(fail树)

    题意:链接 方法:AC自己主动机与fail树性质 解析:复习AC自己主动机的第一道题?(真正的第一题明明是又一次写了遍hdu2222! ) 这题说实话第一眼看上去就是个sb题,仅仅要建出来自己主动机. ...

  9. 《RESTful Web APIs中文版》

    <RESTful Web APIs中文版> 基本信息 原书名:RESTful Web APIs 原出版社: O'Reilly Media 作者: Leonard Richardson    ...

  10. 深入理解多线程(五)—— Java虚拟机的锁优化技术

    本文是<深入理解多线程>的第五篇文章,前面几篇文章中我们从synchronized的实现原理开始,一直介绍到了Monitor的实现原理. 前情提要 通过前面几篇文章,我们已经知道: 1.同 ...