Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

scope

spring

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

like image 317
Kevin Avatar asked Mar 18 '13 11:03

Kevin


1 Answers

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

like image 79
Sachin Avatar answered Nov 15 '22 17:11

Sachin