[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每个版本的退出 ...
随机推荐
- 利用dynamic简化数据库的访问
今天写了一个数据库的帮助类,代码如下. public static class DbEx { public static dynamic ReadToObject(this IDataReader r ...
- J1850 Implement
http://avrobdii.googlecode.com/svn/trunk/code/J1850.c /* Copyright (C) Trampas Stern name of author ...
- Open JTAG Project
Open JTAG Project is an open source hardware and software for a high speed USB JTAG tool. In this si ...
- GUN WINDOW 工具
GNU utilities for Win32 CoreUtils for Windows 或者 完整的 package dd for windows Unix ports - WHICH, TEE ...
- Word中如何从某一页重新开始页码
- Revit API得到类别Category设置类别可见性
start [Transaction(TransactionMode.Manual)] [Regeneration(RegenerationOption.Manual)] public class c ...
- WebLogic使用总结(六)——WebLogic创建虚拟主机和修改启动端口号
一.在WebLogic中创建一个虚拟主机 找到虚拟主机面板,如下图所示:
- 查看内核页表kernel_page_tables (aarch32)
作者 彭东林 pengdonglin137@163.com 平台 Linux-4.10.17 Qemu + vexpress-ca9 概述 通过配置内核,会在/sys/kernel/deb ...
- 统计代码执行时间,使用Stopwatch和UserProcessorTime的区别
当我们需要统计一段代码的执行时间,首先想到的可能是Stopwatch类.在这里,先暂不使用Stopwatch,自定义一个统计代码执行时间的类,大致需要考虑到: 1.确保统计的是当前进程.当前线程中代码 ...
- Ant build.xml
Ant的概念可能有些读者并不连接什么是Ant以及入可使用它,但只要使用通过Linux系统得读者,应该知道make这个命令.当编译Linux内核及一些软件的源程序时,经常要用这个命令.Make命令其实就 ...