Given two beans annotated with @Startup:
@Singleton
@Startup
@DependsOn("B")
public A {
@Inject
private B b;
}
@Singleton
@Startup
public B {}
Is @DependsOn neccessary in this situation to ensure that B is initialized before A? Or is there some convention over configuration that in such a situation the order of injections determines the order of initialization?
The official tutorial does not cover this case but only beans that are merely semantically covered without any syntactic/wiring link via @Inject.
If bean A actually depends on bean B being initialized then you need this.
With @Startup you are doing eager Instantiation - the singleton is instantiated at startup time whether or not it gets used.
In lazy instantiation, the singleton isn't instantiated until it's method's are first needed.
In both cases, container can initialize beans at whichever order it wants:
Sometimes multiple singleton session beans are used to initialize data for an application and therefore must be initialized in a specific order. In these cases, use the javax.ejb.DependsOn annotation to declare the startup dependencies of the singleton session bean.