2

in xml based approach, we configure the bean definition in xml , beans will be created in the order we have defined the beans.

1) <beans>

<bean id="a" class="com.abc.a"/>

`<bean id="b" class="com.abc.b"/>`

</beans> Here , a will be created first before b.

2)<beans> <bean id="a" class="com.abc.a"> <property name="c" ref="c"/> </bean>

<bean id="b" class="com.abc.b/">

<bean id="c" class="com.abc.c/"> here c will be created first, then a then b.

In case of annotation driven approach, how to control the sequence of object creation? using ordered interface ?

ashutosh
  • 23
  • 6

2 Answers2

0

Spring has an Order attribute of java config and an order attribute for xml configuration to control the order in which beans are created. (Lower values means earlyer creation, negative number are allowed too)

An other way is to control the order is DependsOn annotation/attribute.

Ralph
  • 118,862
  • 56
  • 287
  • 383
0

Spring container creates the dependent objects (because they are needed by the main objects as per the object graph) first both in xml & annotation approach.

In case of annotation driven approach, how to control the sequence of object creation? using ordered interface ?

You can't control the order of objects as the dependent objects are always needed to be created first and then followed by the main objects. The order interface is for a different purpose which is to push the objects into a list using autowired.

You can refer the example in the below link for using @Order to set/push an object into a list:

What is the use of @Order annotation in Spring?

Community
  • 1
  • 1
Vasu
  • 21,832
  • 11
  • 51
  • 67