[Spring Boot] Complex Scope Scenarios of a Spring Bean - Mix Prototype and Singleton, ScopeProxy
We have the following example:
@SpringBootApplication
public class In28minutesScopeApplication { private static Logger LOGGER = LoggerFactory.getLogger(In28minutesScopeApplication.class); public static void main(String[] args) {
// Application Context
ApplicationContext applicationContext =
SpringApplication.run(In28minutesScopeApplication.class, args);
PersonDAO personDAO = applicationContext.getBean(PersonDAO.class);
PersonDAO personDAO1 = applicationContext.getBean(PersonDAO.class); LOGGER.info("{}", personDAO);
LOGGER.info("{}", personDAO.getJdbcConnection()); LOGGER.info("{}", personDAO1);
LOGGER.info("{}", personDAO1.getJdbcConnection()); }
}
@Component
public class PersonDAO { @Autowired
JDBCConnection jdbcConnection; public JDBCConnection getJdbcConnection() {
return jdbcConnection;
} public void setJdbcConnection(JDBCConnection jdbcConnection) {
this.jdbcConnection = jdbcConnection;
}
} @Component
public class JDBCConnection {
public JDBCConnection () {
System.out.println("JDBC Connection");
}
}
The idea is to understand in different cases, how those instanse are created.
Currently when running the application, we got:
com.example.in28minutes.scope.PersonDAO@6c61a903
com.example.in28minutes.scope.JDBCConnection@59d4cd39
com.example.in28minutes.scope.PersonDAO@6c61a903
com.example.in28minutes.scope.JDBCConnection@59d4cd39
Each class share the same instanse.
What if we want that 'PersonDAO' using Singleton but JDBCConnection use 'Prototype'?
@Component
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class PersonDAO { @Autowired
JDBCConnection jdbcConnection; public JDBCConnection getJdbcConnection() {
return jdbcConnection;
} public void setJdbcConnection(JDBCConnection jdbcConnection) {
this.jdbcConnection = jdbcConnection;
}
} @Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class JDBCConnection {
public JDBCConnection () {
System.out.println("JDBC Connection");
}
}
In the end, we got the same result:
com.example.in28minutes.scope.PersonDAO@14008db3
com.example.in28minutes.scope.JDBCConnection@16aa8654
com.example.in28minutes.scope.PersonDAO@14008db3
com.example.in28minutes.scope.JDBCConnection@16aa8654
If we really want JDBCConnection use a different instance, we can add Proxy to it:
@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS)
public class JDBCConnection {
public JDBCConnection () {
System.out.println("JDBC Connection");
}
}
[Spring Boot] Complex Scope Scenarios of a Spring Bean - Mix Prototype and Singleton, ScopeProxy的更多相关文章
- Spring Boot入门教程2-1、使用Spring Boot+MyBatis访问数据库(CURD)注解版
一.前言 什么是MyBatis?MyBatis是目前Java平台最为流行的ORM框架https://baike.baidu.com/item/MyBatis/2824918 本篇开发环境1.操作系统: ...
- Spring Boot 2 (五):Docker Compose + Spring Boot + Nginx + Mysql 实践
Spring Boot 2 (五):Docker Compose + Spring Boot + Nginx + Mysql 实践 Spring Boot + Nginx + Mysql 是实际工作中 ...
- Spring Boot 2.X(三):使用 Spring MVC + MyBatis + Thymeleaf 开发 web 应用
前言 Spring MVC 是构建在 Servlet API 上的原生框架,并从一开始就包含在 Spring 框架中.本文主要通过简述 Spring MVC 的架构及分析,并用 Spring Boot ...
- Spring、Spring Boot和TestNG测试指南 - 使用Spring Boot Testing工具
Github地址 前面一个部分讲解了如何使用Spring Testing工具来测试Spring项目,现在我们讲解如何使用Spring Boot Testing工具来测试Spring Boot项目. 在 ...
- 曹工说Spring Boot源码(7)-- Spring解析xml文件,到底从中得到了什么(上)
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- 曹工说Spring Boot源码(8)-- Spring解析xml文件,到底从中得到了什么(util命名空间)
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- 曹工说Spring Boot源码(17)-- Spring从xml文件里到底得到了什么(aop:config完整解析【中】)
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- 曹工说Spring Boot源码(27)-- Spring的component-scan,光是include-filter属性的各种配置方式,就够玩半天了.md
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- Spring Boot系列(三):Spring Boot整合Mybatis源码解析
一.Mybatis回顾 1.MyBatis介绍 Mybatis是一个半ORM框架,它使用简单的 XML 或注解用于配置和原始映射,将接口和Java的POJOs(普通的Java 对象)映射成数据库中的记 ...
随机推荐
- STM32 CRC-32 Calculator Unit
AN4187 - Using the CRC peripheral in the STM32 family At start up, the algorithm sets CRC to the Ini ...
- mysql exists 如何使用
还没时间看,exists用的少 ==>当你只需要判断后面的查询结果是否存 在时使用exists() http://edu.codepub.com/2011/0208/29218.php 今天正 ...
- .Net Discovery 系列之一--string从入门到精通(上)
string是一种很特殊的数据类型,它既是基元类型又是引用类型,在编译以及运行时,.Net都对它做了一些优化工作,正式这些优化工作有时会迷惑编程人员,使string看起来难以琢磨,这篇文章分上下两章, ...
- 魔兽私服TrinityCore 运行调试流程
配置参见上一篇:TrinityCore 魔兽世界私服11159 完整配置 (1)启动Web服务器 打开TC2_Web_Mysql目录,运行“启动Web服务器.exe” 自动弹出帐号注册界面,并启动Ap ...
- Ubuntu下配置用msmtp发送gmail邮件
从https://gist.github.com/JosefJezek/6194563上找到的一个配置脚本,下载后添加可执行权限,然后运行即可. 下面是脚本setup-msmtp-for-gmail. ...
- 添加类似navigationController自带的返回按钮
添加类似navigationController自带的返回按钮,效果如下: 一.UINavigationcontroller自带的navigationBar 是无法添加左箭头的返回按钮的 在网上搜索了 ...
- 快速将wax配置到项目中进行lua开发
通过Finder浏览到你保存该项目的文件夹.创建三个新的文件夹:wax.scripts和Classes. 第一:首先,下载源代码的压缩包.Wax放在GitHub上(https://github.com ...
- 用 setMessage 方法灵活复用 UIAlertView
- 【Devops】【docker】【CI/CD】jenkins源码管理,添加SSH地址后报错+Jenkins构建报错:Please make sure you have the correct access rights and the repository exists.
jenkins源码管理,添加SSH地址后报错: Could not read from remote repository. Please make sure you have the correct ...
- 【ELK】【docker】【elasticsearch】2.使用elasticSearch+kibana+logstash+ik分词器+pinyin分词器+繁简体转化分词器 6.5.4 启动 ELK+logstash概念描述
官网地址:https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html#docker-cli-run-prod ...