博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Spring Boot] Complex Scope Scenarios of a Spring Bean - Mix Prototype and Singleton, ScopeProxy
阅读量:5040 次
发布时间:2019-06-12

本文共 2681 字,大约阅读时间需要 8 分钟。

We have the following example:

@SpringBootApplicationpublic 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());    }}
@Componentpublic class PersonDAO {    @Autowired    JDBCConnection jdbcConnection;    public JDBCConnection getJdbcConnection() {        return jdbcConnection;    }    public void setJdbcConnection(JDBCConnection jdbcConnection) {        this.jdbcConnection = jdbcConnection;    }}@Componentpublic 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@6c61a903com.example.in28minutes.scope.JDBCConnection@59d4cd39 com.example.in28minutes.scope.PersonDAO@6c61a903com.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@14008db3com.example.in28minutes.scope.JDBCConnection@16aa8654com.example.in28minutes.scope.PersonDAO@14008db3com.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");    }}

 

转载于:https://www.cnblogs.com/Answer1215/p/10658915.html

你可能感兴趣的文章
IIS初始化(预加载),解决第一次访问慢,程序池被回收问题(转载)
查看>>
Bean的Scope
查看>>
【BZOJ】3142: [Hnoi2013]数列
查看>>
http初探
查看>>
elasticsearch的安装
查看>>
__next__()
查看>>
爬取:中国大学排名
查看>>
聊天室(C++客户端+Pyhton服务器)_1.框架搭设
查看>>
UpdatePanel 内控件 更新“外的”控件【转】
查看>>
mybatis中>=和<=的实现方式
查看>>
Python面向对象03/继承
查看>>
java序列化和反序列化
查看>>
绝对定位
查看>>
flink源码编译(windows环境)
查看>>
dpkg 删除 百度网盘 程序
查看>>
服务器nginx安装
查看>>
std::nothrow
查看>>
rest-framework 分页器
查看>>
JQuery(一)安装&选择器 样式篇
查看>>
浏览器的DNS缓存查看和清除
查看>>