[Spring Boot] Singleton and Prototype
2023-09-14 08:59:14 时间
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[] {1,2,3,4}, 3); 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 之 spring security 配置
- Spring Boot启动时数据库初始化spring.datasource(转)
- [Spring Boot] Complex Scope Scenarios of a Spring Bean - Mix Prototype and Singleton, ScopeProxy
- [Spring Boot] Adding JPA and Spring Data JPA
- [Spring boot] Configuring and Accessing a Data Source
- [Spring boot] A quick REST API Guide
- spring boot 集成 kaptcha
- [Spring boot] CommandLineRunner and Autowired
- [Spring Boot] @Component, @AutoWired and @Primary
- Spring Boot——2分钟构建spring web mvc REST风格HelloWorld
- maven 创建spring boot 需要的配置[一]
- spring boot单元测试之五:用mybatis+H2数据库测试sql(spring boot 2.4.3)
- Spring Data Redis 让 NoSQL 快如闪电 (1)
- spring boot xml与dao 映射关系
- 【springboot异步处理】Spring Boot + @Async
- Spring Boot使用外部配置文件
- 学习Spring Boot:(十四)spring-shiro的密码加密
- 自我分析-Spring IOC
- spring boot 使用 mybatis 开启事务回滚 的总结
- 【Spring Boot 开发实战】第1讲 Kotlin 的极简特性之:隐式类型与函数式编程
- A Comparison Between Spring and Spring Boot
- Spring Boot 项目打包问题集锦: jar依赖多出boot-inf 文件夹问题/多环境动态打包/缺少BOOT-INF目录问题等...
- 毕业设计 Spring Boot的留学生交流互动管理系统(含源码+论文)
- Spring Boot整合Elasticsearch
- 001-Spring Cloud Edgware.SR3 升级最新 Finchley.SR1,spring boot 1.5.9.RELEASE 升级2.0.4.RELEASE注意问题点
- java-mybaits-009-mybatis-spring-使用,SqlSessionFactoryBean、事务
- MyBatis系列教程(六)-- 与Spring综合(Integrate with Spring)
- Spring Boot构建系统
- Spring Cloud是一系列框架的有序集合
- Spring MVC内部资源视图解析器
- 启动Spring boot项目报错:java.lang.IllegalArgumentException: LoggerFactory is not a Logback