The mystery of Java EE 6 annotations inheritance

前端 未结 1 619
无人及你
无人及你 2020-12-16 02:39

I\'m using inheritance with EJB in a few scenarios, sometimes with annotations in the super class like this generic entityDAO:

public class JpaDAO{
         


        
相关标签:
1条回答
  • 2020-12-16 03:21

    Java annotations are not inherited, but the JavaEE specs change the rules to allow these attributes to work as expected. See the common annotations 1.1 spec. Section 2.1 even uses @TransactionAttribute as an example. EJB 3.1 section 13.3.7.1 also explicitly states the rules for @TransactionAttribute:

    If the bean class has superclasses, the following additional rules apply.

    • transaction attribute specified on a superclass S applies to the business methods defined by S. If a class-level transaction attribute is not specified on S, it is equivalent to specification of TransactionAttribute(REQUIRED) on S.
    • A transaction attribute may be specified on a business method M defined by class S to override for method M the transaction attribute value explicitly or implicitly specified on the class S.
    • If a method M of class S overrides a business method defined by a superclass of S, the transaction attribute of M is determined by the above rules as applied to class S.

    In short, for most JavaEE annotations, method-level annotations apply to that method unless a subclass overrides the method, and class-level annotations apply to all methods defined in that class only. The rule does not apply to "component-defining" class-level annotations, such as @Stateless (see the EJB 3.1 specification section 4.9.2.1)

    0 讨论(0)
提交回复
热议问题