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的更多相关文章

  1. Spring Boot入门教程2-1、使用Spring Boot+MyBatis访问数据库(CURD)注解版

    一.前言 什么是MyBatis?MyBatis是目前Java平台最为流行的ORM框架https://baike.baidu.com/item/MyBatis/2824918 本篇开发环境1.操作系统: ...

  2. Spring Boot 2 (五):Docker Compose + Spring Boot + Nginx + Mysql 实践

    Spring Boot 2 (五):Docker Compose + Spring Boot + Nginx + Mysql 实践 Spring Boot + Nginx + Mysql 是实际工作中 ...

  3. Spring Boot 2.X(三):使用 Spring MVC + MyBatis + Thymeleaf 开发 web 应用

    前言 Spring MVC 是构建在 Servlet API 上的原生框架,并从一开始就包含在 Spring 框架中.本文主要通过简述 Spring MVC 的架构及分析,并用 Spring Boot ...

  4. Spring、Spring Boot和TestNG测试指南 - 使用Spring Boot Testing工具

    Github地址 前面一个部分讲解了如何使用Spring Testing工具来测试Spring项目,现在我们讲解如何使用Spring Boot Testing工具来测试Spring Boot项目. 在 ...

  5. 曹工说Spring Boot源码(7)-- Spring解析xml文件,到底从中得到了什么(上)

    写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...

  6. 曹工说Spring Boot源码(8)-- Spring解析xml文件,到底从中得到了什么(util命名空间)

    写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...

  7. 曹工说Spring Boot源码(17)-- Spring从xml文件里到底得到了什么(aop:config完整解析【中】)

    写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...

  8. 曹工说Spring Boot源码(27)-- Spring的component-scan,光是include-filter属性的各种配置方式,就够玩半天了.md

    写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...

  9. Spring Boot系列(三):Spring Boot整合Mybatis源码解析

    一.Mybatis回顾 1.MyBatis介绍 Mybatis是一个半ORM框架,它使用简单的 XML 或注解用于配置和原始映射,将接口和Java的POJOs(普通的Java 对象)映射成数据库中的记 ...

随机推荐

  1. linux 内核升级2 转

    linux内核升级 一.Linux内核概览 Linux是一个一体化内核(monolithic kernel)系统. 设备驱动程序可以完全访问硬件. Linux内的设备驱动程序可以方便地以模块化(mod ...

  2. Windows Application Data拒绝访问打开方法?

    在Windows7操作系统,打开 Application Data等文件夹时,弹出位置不可用的警告窗口,提示拒绝访问.下面提供简单的解决方法,希望有用. 工具/原料 计算机. Windows7操作系统 ...

  3. 给第三方dll强签名

    假若我们要对第三方控件或者是其他的没有源代码的DLL文件想做类似的处理,增加强名称签名,怎么处理,是很多人都会面对的问题.     步骤: 1.首先采用反汇编工具ildasm生成中间语言. ildas ...

  4. 对一个前端使用AngularJS后端使用ASP.NET Web API项目的理解(1)

    chsakell分享了一个前端使用AngularJS,后端使用ASP.NET Web API的项目. 源码: https://github.com/chsakell/spa-webapi-angula ...

  5. The Win32 Rundll and Rundll32 Interface Related Topics

    The Win32 Rundll and Rundll32 Interface Related Topics Microsoft Knowledge Base Article Q164787 Appl ...

  6. Android SDK Download List

    from://http://sofire.iteye.com/blog/1961552 Android SDK Download List! 通过分析SDK Manager里要用到的repositor ...

  7. Swift - RotateView

    Swift - RotateView 效果 源码 https://github.com/YouXianMing/Swift-Animations // // RotateView.swift // S ...

  8. 绝命毒师第一季/全集Breaking Bad迅雷下载

    本季Breaking Bad Season 1(2008)看点:新墨西哥州的高中化学老师沃尔特·H·怀特(布莱恩·科兰斯顿 Bryan Cranston 饰)是拮据家庭的唯一经济来源.他大半生安分守己 ...

  9. Java_并发线程_Semaphore、CountDownLatch、CyclicBarrier、Exchanger

    1.Semaphore 信号量(Semaphore),有时被称为信号灯,是在多线程环境下使用的一种设施, 它负责协调各个线程, 以保证它们可以正确.合理的使用公共资源. Semaphore当前在多线程 ...

  10. js如何判断用户是在pc端和还是移动端访问

    js如何判断用户是在pc端和还是移动端访问 来源:A5技术交流 作者:wofa 时间:2014-04-25收藏本页 最近一直在忙我们团队的项目“咖啡之翼”,在这个项目中,我们为移动平台提供了一个优秀的 ...