Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out the scope of a spring managed bean

Tags:

java

spring

Is it possible to ascertain whether a bean is a prototype bean or not?

I'm hoping for a method on one of the variants of application Context like getScope or getBeanMetaData...

like image 487
Michael Wiles Avatar asked Jan 10 '23 05:01

Michael Wiles


1 Answers

You can "ask" the BeanFactory for the BeanDefintion, it contains the scope

 @Autowired
 ConfigurableApplicationContext applicationContext;
 ...

 applicationContext.getBeanFactory().getBeanDefinition("beanName").getScope()

(getBeanFactory() is defined at ConfigurableApplicationContext which is an interface that is implemented by every concrete ApplicationContext except StubWebApplicationContext)

like image 79
Ralph Avatar answered Jan 17 '23 17:01

Ralph