Is there a way to print all the spring beans that are loaded on startup?I am using Spring 2.0.
The ListableBeanFactory interface provides getBeanDefinitionNames() method which returns the names of all the beans defined in this factory. This interface is implemented by all the bean factories that pre-loads their bean definitions to enumerate all their bean instances.
In Spring Boot, you can use appContext. getBeanDefinitionNames() to get all the beans loaded by the Spring container.
Spring Bean will be defined using stereotype annotations or XML Bean configurations. As soon as bean created and It will be instantiated and loaded into ApplicationContext and JVM memory. Spring container will create a bean id , scope , default values based on the bean definition.
Spring @Bean Annotation is applied on a method to specify that it returns a bean to be managed by Spring context. Spring Bean annotation is usually declared in Configuration classes methods. In this case, bean methods may reference other @Bean methods in the same class by calling them directly.
Yes, get ahold of ApplicationContext
and call .getBeanDefinitionNames()
You can get the context by:
ApplicationContextAware
@Inject
/ @Autowired
(after 2.5)WebApplicationContextUtils.getRequiredWebApplicationContext(..)
Related: You can also detect each bean's registration by registering a BeanPostprocessor
bean. It will be notified for each bean.
public class PrintBeans { @Autowired ApplicationContext applicationContext; public void printBeans() { System.out.println(Arrays.asList(applicationContext.getBeanDefinitionNames())); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With