@Dependent CDI 注解是否被子类继承?

Is the @Dependent CDI annotation inherited by subclasses?

我有这些 类:

@Dependent
public abstract class ClassA{...}

public class ClassB extends ClassA{...}

public class ClassC{
  @Inject
  private ClassB classB;
}

那么,当classC的实例被销毁时,注入到classC的实例中的classB的实例会被销毁吗?换句话说就是 @Dependent CDI 注释被 sub类?

继承

这是 CDI 1.0 spec 中的相关部分。注意第二个要点:

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.

由于 @Dependent 伪作用域确实具有 @Inherited 元注释,如果子 class 或任何中介 class 都没有,则该作用域被继承范围注释(如您的示例所示)。

因为 @Dependent 范围是默认范围,所以我认为这两种方式都没有太大关系。