user5182503
user5182503

Reputation:

Is the @Dependent CDI annotation inherited by subclasses?

I have these classes:

@Dependent
public abstract class ClassA{...}

public class ClassB extends ClassA{...}

public class ClassC{
  @Inject
  private ClassB classB;
}

So, will the instance of classB injected in instance of classC be destroyed when instance of classC will be destroyed? In other words is the @Dependent CDI annotation inherited by subclasses?

Upvotes: 4

Views: 4837

Answers (1)

Robby Cornelissen
Robby Cornelissen

Reputation: 97247

This is the relevant section in the CDI 1.0 spec. Note the second bullet point:

Suppose a class X is extended directly or indirectly by the bean class of a managed bean or session bean Y.

  • If X is annotated with a qualifier type, stereotype or interceptor binding type Z then Y inherits the annotation if and only if Z declares the @Inherited meta-annotation and neither Y nor any intermediate class that is a subclass of X and a superclass of Y declares an annotation of type Z.
    (This behavior is defined by the Java Language Specification.)

  • If X is annotated with a scope type Z then Y inherits the annotation if and only if Z declares the @Inherited meta-annotation and neither Y nor any intermediate class that is a subclass of X and a superclass of Y declares a scope type.
    (This behavior is different to what is defined in the Java Language Specification.)

A scope type explicitly declared by X and inherited by Y from X takes precedence over default scopes of stereotypes declared or inherited by Y.

As the @Dependent pseudo-scope does indeed have the @Inherited meta-annotation, the scope is inherited if nor the subclass nor any intermediary class have a scope annotation (as in your example).

Since the @Dependent scope is the default scope, it doesn't matter too much either way, I think.

Upvotes: 1

Related Questions