Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding out order of bean creation in the spring IOC

How can i find out in what order my beans were created?

+1 for anyone who can recommend a tool to display it visually.

like image 210
JavaRocky Avatar asked Jul 12 '10 04:07

JavaRocky


People also ask

What is the order of bean creation in Spring?

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.

How do I get beans from IoC container?

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.

Does Bean order matter Spring?

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.

What is @order annotation in Spring?

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.


1 Answers

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.

like image 68
Paul Grime Avatar answered Sep 30 '22 04:09

Paul Grime