[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 对象)映射成数据库中的记 ...
随机推荐
- LPC43xx Asymmetric Dual Core : Cortex-M0 and Cortex-M4
- Timer triggered DMA transfer - Delay between requesting the DMA transfer
Hello, I'm working with a STM32F407 controller board. Right now, I want to trigger a DMA transfer ...
- go中的接口
对于golang的接口,纠结两天了,今天有种茅塞顿开的感觉,有必要写点东西了. 纠结接口,说白了就是搞不透接口,方法,结构体几者之间的关系以及具体的用途.可以简单的从三者的定义说起,接口说白了就是一个 ...
- ASP.NET Web API中实现版本的几种方式
在ASP.NET Web API中,当我们的API发生改变,就涉及到版本问题了.如何实现API的版本呢? 1.通过路由设置版本 最简单的一种方式是通过路由设置,不同的路由,不同的版本,不同的contr ...
- Windows Phone本地数据库(SQLCE):2、LINQ to SQL(翻译)(转)
首先.要说到的是,windows phone 7.1上基本的数据库功能是SQL Compact关于Mango的一个实现,使用linq to sql访问存储在数据库上的数据. 1.LINQ to S ...
- Spring Boot开发之流水无情(二)
http://my.oschina.net/u/1027043/blog/406558 上篇散仙写了一个很简单的入门级的Spring Boot的例子,没啥技术含量,不过,其实学任何东西只要找到第一个突 ...
- C++ 继承体系中的名称覆盖
首先一个简单的样例: int x; int f() { double x; cin >> x; return x; } 在上述代码中.函数f的局部变量x掩盖了全局变量x.这得从 " ...
- 算法java实现--动态规划--电路布线问题
/* * dianlubuxian.java * Version 1.0.0 * Created on 2017年11月30日 * Copyright ReYo.Cn */ package reyo. ...
- mysql error You must reset your password using ALTER USER statement before executing this statement.
mysql修改密码Your password does not satisfy the current policy requirements 出现这个问题的原因是:密码过于简单.刚安装的mysql的 ...
- ExtJS 4.2 教程-07:Ext.Direct
转载自起飞网,原文地址:http://www.qeefee.com/extjs-course-7-Ext-Direct ExtJS 4.2 教程-01:Hello ExtJS ExtJS 4.2 教程 ...