jakarta-eejava-ee-7ejb-3.2

@DependsOn for subclasses


@DependsOn can be used to ensure that one EJB is initialized before another EJB:

@Startup @Singleton
public class SchemaMigration {
    // ...
}

@DependsOn("SchemaMigration")
public class Crud<E extends BaseEntity> {
    // ...
}

I'm wondering whether subclasses also implicitly inherit this constraint?

@Startup @Singleton
public class Birds extends Crud<Bird> {
    // ...
}

@Startup @Singleton
public class Frogs extends Crud<Frog> {
    // ...
}

I know that plain Java annotations are not inherited, but Java EE changes the rules for some annotations. But I haven't found specific informations about the @DependsOn annotation.


Solution

  • DependsOn does not ensure, that one EJB is initialized before another EJB.

    Please refer to the DependsOn Javadoc:

    The container ensures that all singleton beans with which a singleton has a DependsOn relationship have been initialized before the singleton's PostConstruct method is called.

    This does only work for concrete instances of singleton EJBs using the ejb-name of the referenced singleton.

    Regarding inheritance of session beans, the EJB spec (4.9.2.1) says:

    A session bean class is permitted to have superclasses that are themselves session bean classes. However, there are no special rules that apply to the processing of annotations or the deployment descriptor for this case. For the purposes of processing a particular session bean class, all superclass processing is identical regardless of whether the superclasses are themselves session bean classes. In this regard, the use of session bean classes as superclasses merely represents a convenient use of implementation inheritance, but does not have component inheritance semantics.