Can I programmatically determine if a spring bean is not singleton?

谁都会走 提交于 2019-12-10 13:59:55

问题


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 ?

Spring config:

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

Java:sc

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

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


回答1:


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



来源:https://stackoverflow.com/questions/15475852/can-i-programmatically-determine-if-a-spring-bean-is-not-singleton

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!