[Spring Boot] Singleton and Prototype
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的更多相关文章
- 通俗易懂spring之singleton和prototype
关于spring bean作用域,基于不同的容器,会有所不同,如BeanFactory和ApplicationContext容器就有所不同,在本篇文章,主要讲解基于ApplicationContext ...
- Singleton and Prototype Bean Scope in Spring
Scope描述的是Spring容器如何新建Bean的实例的. 1> Singleton: 一个Spring容器只有一个Bean的实例,此为Spring的默认配置,全容器共享一个实例. 2> ...
- spring中scope的prototype与singleton区别
最近在研究单例模式,突然想起项目中以下配置,scope="singleton" 和 scope="prototype"到底有何区别呢?以下做下简要分析. < ...
- spring中bean的作用域属性singleton与prototype的区别
1.singleton 当一个bean的作用域设置为singleton, 那么Spring IOC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会 ...
- 【Spring】bean的作用域(@Scope) - singleton、prototype
已知spring 3+已拥有多种不同的作用域: singleton(默认).prototype.request.session.global session.(参考: spring中scope作用域( ...
- [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 ...
- Spring Boot实战笔记(二)-- Spring常用配置(Scope、Spring EL和资源调用)
一.Bean的Scope Scope描述的是Spring容器如何新建Bean实例的.Spring的Scope有以下几种,通过@Scope注解来实现. (1)Singleton:一个Spring容器中只 ...
- Java Spring Boot VS .NetCore (三)Ioc容器处理
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Spring Boot实战
Spring在java EE开发中是实际意义上的标准,但我们在开发Spring的时候可能会遇到以下令人头疼的问题: 1.大量配置文件的定义.2.与第三方软件整合的技术问题. Spring每个版本的退出 ...
随机推荐
- sqlserver 2012 IDE中 Windows身份验证连接服务器报错 ,Login failed for user 'xxx\Administrator'. 原因: 找不到与提供的名称匹配的登录名。
问题描述: 本地装了两个实例,一个是SQLEXPRESS,可以正常操作.但是另一个开发常用的实例MSSQLSERVER却连Windows身份验证都报错,报的错误也是很奇葩,怎么会找不到Administ ...
- AngularJS路由系列(2)--刷新、查看路由,路由事件和URL格式,获取路由参数,路由的Resolve
本系列探寻AngularJS的路由机制,在WebStorm下开发.主要包括: ● 刷新路由● 查看当前路由以及所有路由● 路由触发事件● 获取路由参数 ● 路由的resolve属性● 路由URL格式 ...
- WCF中修改接口或方法名称而不影响客户端程序
本篇接着"从Web Service和Remoting Service引出WCF服务"中有关WCF的部分. 运行宿主应用程序. 运行Web客户端中的网页. 输入内容,点击按钮,能获取 ...
- 初识安卓小程序(Android电话拨号器)
首先,先创建一个安卓项目(我的版本号是4.4.2的),名字为"电话拨号器",创建的时候点击"clipart",如图: 然后在res目录下找到layout目录,找 ...
- SharePoint Online 创建和使用视图
前言 本文介绍如何在Office 365中创建和使用视图. 正文 首先,解释一下什么是SharePoint站点视图,所谓视图,就是列表的一个呈现形式,包含特定的栏.排序.筛选.分组等特性,我们通常创建 ...
- Mac 安装zsh
1.安装zsh mac下自带zsh,但不是最新.查看zsh版本:zsh --version如果没有安装, 可以通过brew安装最新版,brew install zsh 2.安装oh-my-zsh cd ...
- ios之归档demo
ios对自定义对象的归档.首先需要实现NSCoding与NSCopying接口 #import <Foundation/Foundation.h> @interface Person : ...
- Android scrollbar的设置
insideOverlay:默认值,表示在padding区域内并且覆盖在view上 insideInset:表示在padding区域内并且插入在view后面 outsideOverlay:表示在pad ...
- [Android P] Android P版本 新功能介绍和兼容性处理(一)
cp from :https://blog.csdn.net/yi_master/article/details/80046696 Android P版本已经到来,首篇我们当然要先看下Android ...
- Mysql 编译安装并使用自定义用户启动
本文基于 Redhat Linux 6.7 的环境,Mysql 版本为 5.5.37 安装前的检查 必备的组件,如果没有使用 yum 进行安装,可以使用网上的源,也可以使用本地光盘作为 Yum 源. ...