Comparison of the five standards for Dependency Injection in Java EE 7 / Java EE 6 Web Profile

running on WildFly 8.2.0.Final - 1.1.0.Final

Managed Beans 1.0 Dependency Injection for Java 1.0 Contexts and Dependency Injection for the Java EE platform (CDI) 1.1 JavaServer Faces (JSF) 2.2 Enterprise JavaBeans (EJB) 3.2 Lite
JEE7 JSR (JEE6 JSR) JSR-316 / JSR-2501 JSR-330 JSR-346 (JSR-299) JSR-344 (JSR-314) JSR-345 (JSR-318)
Reference Implementation Oracle GlassFish Google Guice JBoss Seam Weld Oracle Mojarra Oracle GlassFish
Annotation Packages javax.annotation.* javax.inject.* javax.inject.*, javax.enterprise.*, javax.decorator.* javax.faces.bean.* javax.ejb.*
Bean Annotation @ManagedBean @Named
@javax.enterprise.inject.Model2
(none)3 @ManagedBean @Stateless, @Stateful, @Singleton, @MessageDriven
Injection Annotation @Resource @Inject @javax.inject.Inject4 @ManagedProperty @EJB
Manual JNDI Lookup Works! Doesn't work! Doesn't work! Doesn't work! Works!
Named BeanManager Lookup Doesn't work! Works! Doesn't work! Doesn't work! Doesn't work!
Typed BeanManager Lookup Doesn't work! Works! Works! Doesn't work! Works!
EL 3.0 Lookup Doesn't work! Works! Doesn't work! Works! Doesn't work!
Can be referenced by JSR-316? Works! Works! Works! Doesn't work! Works!
Can be referenced by JSR-330? Doesn't work! Works! Works! Doesn't work! Works!
Can be referenced by JSR-346? Doesn't work! Works! Works! Doesn't work! Works!
Can be referenced by JSR-344? Works! Works! Works! Works! Works!
Can be referenced by JSR-345? Works! Works! Works! Doesn't work! Works!
Container-managed transactions5 Works! Works! Works! Doesn't work! Works! 6
Scopes provided7 (none)
  • @Singleton
  • @javax.enterprise.context.RequestScoped
  • @javax.enterprise.context.ConversationScoped
  • @javax.enterprise.context.SessionScoped
  • @javax.enterprise.context.ApplicationScoped
  • @javax.enterprise.context.Dependent
  • @RequestScoped
  • @ViewScoped
  • @SessionScoped
  • @ApplicationScoped
  • @NoneScoped
  • @CustomScoped
(none)
Additional features
  • Common resouce injection
  • Bean lifecycle callbacks
  • Security / access control annotations
  • Common utility annotations
  • Scope / Qualifier meta-annotations
  • Qualifier annotations
  • Producer / Destructor annotations
  • Alternative / Specialization annotations
  • Interceptor / Decorator annotations
  • Event control
  • Conversation control
  • Meta-annotations (@Stereotype)
  • JSF components annotations
  • JSF handler classes
  • Local / Remote interface annotations
  • Bean activation / passivation annotations
  • EJB-specific transaction control annotations
  • Asynchronous method call annotations
  • Concurrency control annotations
  • Timer control annotations
  • Exception handling annotations

Notes

  1. The specification is actually part of JSR-316 (the now outdated Java Platform, Enterprise Edition 6 (Java EE 6)), but the annotation itself is defined in JSR-250 (Common Annotations for the Java Platform 1.1).
  2. The built-in stereotype @Model of JSR-346 is an alias to the JSR-330 @Named annotation with an additional @RequestScope.
  3. That's right! Having CDI installed in your container / webapp, any "POJO" class is considered a "CDI managed bean". No annotation is required. You must in fact use the @javax.enterprise.inject.Alternative annotation to prevent a class from being considered a JSR-330 and JSR-299 managed bean. Note that this behavior changed again from Java EE 6 (CDI 1.0) to Java EE 7 (CDI 1.1): Un-annotated classes are only considered a "CDI managed Bean" if there is (a) a beans.xml file and (b) it's configured with bean-discovery-mode="all".
  4. JSR-346 does actually reuse the injection annotation of JSR-330.
  5. Using transaction control annotations specified in Java EE7 JTA 1.2 spec (JSR-907, @javax.transaction.Transactional). They provide declarative container-managed transactions for all Java Bean types except for EJB Beans (see below).
  6. EJB specific transaction control annotations (@javax.ejb.TransactionAttribute) only. JTA transaction control annotations (@javax.transaction.Transactional, mind the package name!) are illegal for EJB Beans.
  7. You can use the scope annotations for their respective bean type only. They don't work for any other bean type. Be careful with the scope annotation name collisions between JSR-346 and JSR-344!

developed by codebulb.ch. (Original blog post.) Uses Silk Icon icon set. Built April 12, 2015.