Spring 3 bean instantiation sequence

If bean A depends on bean B by defining <property/>, @Autowired or <constructor-arg/> then the order is forced and fixed by the Spring container. No problem here.

But if you want to enforce specific order of bean creation which is not expressed via explicit dependencies feel free to use:

<bean id="A" depends-on="B"/>
<bean id="B"/>

or better (with annotations, works also with @Bean Java configuration):

@Service
@DependsOn("B")
public class A {}

or even better… don’t use it. These constructs are a code smell and often suggest you have some nasty invisible dependency between your components.

Leave a Comment