How can i find out in what order my beans were created?
+1 for anyone who can recommend a tool to display it visually.
The order in which Spring container loads beans cannot be predicted. There's no specific ordering logic specification given by Spring framework. But Spring guarantees if a bean A has dependency of B (e.g. bean A has an instance variable @Autowired B b; ) then B will be initialized first.
The IoC Container A bean is the foundation of a Spring-managed application; all beans reside withing the IOC container, which is responsible for managing their life cycle. We can get a list of all beans within this container in two ways: Using a ListableBeanFactory interface. Using a Spring Boot Actuator.
Order of bean initialization should not matter since the fields are injected after creating the bean. The only problem where the order will matter is when the bean is needed at constructor argument of other class, but Spring notices this and will solve it.
The @Order annotation defines the sorting order of an annotated component or bean. It has an optional value argument which determines the order of the component; the default value is Ordered. LOWEST_PRECEDENCE. This marks that the component has the lowest priority among all other ordered components.
If you want to find out the order of bean creation, you can register a custom BeanPostProcessor that prints out the info you are interested in. Follow the link for a Spring 2.5.x example.
If you want to view the bean configuration before any actual bean creation, then register a custom BeanFactoryPostProcessor that prints out the info you are interested in. Follow the link for a Spring 2.5.x example.
In either case, if you want to display the info visually then your PostProcessors could easily create a JFrame with a JList and add the info to that.
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