Michael Wiles
Michael Wiles

Reputation: 21194

How to find out the scope of a spring managed bean

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...

Upvotes: 7

Views: 4250

Answers (1)

Ralph
Ralph

Reputation: 120851

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)

Upvotes: 12

Related Questions