9

Suppose I have bean, which init-method or constructor should be called after init-method of another bean. Is it possible?

Shikarn-O
  • 3,337
  • 7
  • 26
  • 27

3 Answers3

14

Use depends-on attribute in spring context XML file:

<bean id="beanOne" class="ExampleBean" depends-on="manager">
  <property name="manager"><ref local="manager"/></property>
</bean>

or @DependsOn annotation on bean if you are using annotations.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • 1
    @fasttooth, good question. I do not know exact answer. It *probably* works because as far as I know spring invokes all post construct methods after wiring, so probably it uses the same order as used for wiring. But I am not sure. – AlexR Nov 05 '15 at 14:58
5

Use @DependsOn annotation or depends-on attribute if you're using xml configuration.

soulcheck
  • 36,297
  • 6
  • 91
  • 90
  • 1
    Does spring also honor the `javax.ejb.DependsOn` annotation? The documentation for the corresponding spring annotation can be found at http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/DependsOn.html – Jörn Horstmann Jan 29 '14 at 12:27
4

You can use de depends-on attribute on your second bean.

Reference: http://static.springsource.org/spring/docs/1.2.x/reference/beans.html#beans-factory-dependson

Tomas Narros
  • 13,390
  • 2
  • 40
  • 56