我可以以编程方式确定 spring bean 是否不是单例吗? [英] Can I programmatically determine if a spring bean is not singleton?

查看:45
本文介绍了我可以以编程方式确定 spring bean 是否不是单例吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我得到一个 spring bean(通过 getBean())时,有什么方法可以从 java 代码中验证 bean 是用 scope=prototype 定义的吗?

When I get a spring bean (via getBean()), is there any way to verify from java code that the bean has been defined with scope=prototype ?

弹簧配置:

<bean class="foo.Bar" scope="prototype" />

Java:sc

MyBean bean = springApplicationContext.getBean("MyBean");

我可以将它实例化两次并比较对象,但我想避免创建不必要的对象.与此答案相反的东西可以解决问题:https://stackoverflow.com/a/9125610/156477

I could just instantiate it twice and compare the objects, but I'd like to avoid unnecessary object creation. Something like the opposite of this answer would do the trick: https://stackoverflow.com/a/9125610/156477

推荐答案

您在 ApplicationContext 中有一个 API boolean isPrototype(String name) 来检查它.

You have a API boolean isPrototype(String name) in ApplicationContext to check it.

这篇关于我可以以编程方式确定 spring bean 是否不是单例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆