I sometimes joke that CDI is basically EJB 4.0. While that's obviously not true there is a considerable amount of similarity and that does create a bit of confusion for people. Here is some general information on EJB and CDI as they relate to each together.
Note that EJBs are CDI beans and therefore have all the benefits of CDI. The reverse is not true (yet). So definitely don't get into the habit of thinking "EJB vs CDI" as that logic really translates to "EJB+CDI vs CDI", which is an odd equation.
In future versions of Java EE we'll be continuing to align them. What aligning means is allowing people to do what they already can do, just without the
@Singletonannotation at the top.
Ultimately, EJB and CDI share the same fundamental design of being proxied components. When you get a reference to an EJB or CDI bean, it isn't the real bean. Rather the object you are given is a fake (a proxy). When you invoke a method on this fake object, the call goes to the container who will send the call through interceptors, decorators, etc. as well as take care of any transaction or security checks. Once all that is done, the call finally goes to the real object and the result is passed back through the proxy to the caller.
The difference only comes in how the object to be invoked is resolved. By "resolved" we simply mean, where and how the container looks for the real instance to invoke.
In CDI the container looks in a "scope", which will basically be a hashmap that lives for a specific period of time (per request
@RequestScoped, per HTTP Session
@SessionScoped, per application
@ApplicationScoped, JSF Conversation
@ConversationScoped, or per your custom scope implementation).
In EJB the container looks also into a hashmap if the bean is of type
@Statefulbean can also use any of the above scope annotations causing it to live and die with all the other beans in the scope. In EJB
@Statefulis essentially the "any scoped" bean. The
@Statelessis basically an instance pool -- you get an instance from the pool for the duration of one invocation. The
So in a fundamental level, anything you can do with an "EJB" bean you should be able to do with a "CDI" bean. Under the covers it's awfully hard to tell them apart. All the plumbing is the same with the exception of how instances are resolved.
They aren't currently the same in terms of the services the container will offer when doing this proxying, but as I say we're working on it at the Java EE spec level.
Disregard any "light" or "heavy" mental images you may have. That's all marketing. They have the same internal design for the most part. CDI instance resolution is perhaps a bit more complex because it is slightly more dynamic and contextual. EJB instance resolution is fairly static, dumb and simple by comparison.
I can tell you from an implementation perspective in TomEE, there's about zero performance difference between invoking an EJB vs invoking a CDI bean.
Of course don't use CDI or EJB when there is no benefit. Throw in CDI when you start to want injection, events, interceptors, decorators, lifecycle tracking and things like that. That's most the time.
Beyond those basics, there are a number of useful container services you only have the option to use if you make your CDI bean also an EJB by adding
Here's a short list of when I break out the EJBs.
Exposing a JAX-WS
@WebService. I'm lazy. When the
@WebServiceis also an EJB, you don't have to list it and map it as a servlet in the
web.xmlfile. That's work to me. Plus I get the option to use any of the other functionality mentioned below. So it's a no-brainer for me.
Exposing a JAX-RS resource via
@Path. I'm still lazy. When the RESTful service is also an EJB, again you get automatic discovery and don't have to add it to a JAX-RS
Applicationsubclass or anything like that. Plus I can expose the exact same bean as an
@WebServiceif I want to or use any of the great functionality mentioned below.
Load on startup via
@Startup. There is currently no equivalent to this in CDI. Somehow we missed adding something like an
AfterStartupevent in the container lifecycle. Had we done this, you simply could have had an
@ApplicationScopedbean that listened for it and that would be effectively the same as an
@Startup. It's on the list for CDI 1.1.
@Asynchronousmethod invocation. Starting threads is a no-no in any server-side environment. Having too many threads is a serious performance killer. This annotation allows you to parallelize things you do using the container's thread pool. This is awesome.
ScheduleExpressionis basically a cron or
Quartzfunctionality. Also very awesome. Most containers just use Quartz under the covers for this. Most people don't know, however, that scheduling work in Java EE is transactional! If you update a database then schedule some work and one of them fails, both will automatically cleaned up. If the
EntityManagerpersist call fails or there is a problem flushing, there is no need to un-schedule the work. Yay, transactions.
The above note on transactions of course requires you to use a
EntityManager. You can use them with plain "CDI", but without the container-managed transactions it can get really monotonous duplicating the
Available to all Java EE components including CDI, JSF
@WebFilter, etc. The
@TransactionAttributeannotation, however, is available to
EntityManagerallows you to keep an
JTAtransactions and not lose the cached data. Good feature for the right time and place. Use responsibly :)
When you need synchronization, the
@Lock(WRITE)annotations are pretty excellent. It allows you to get concurrent access management for free. Skip all the ReentrantReadWriteLock plumbing. In the same bucket is
@AccessTimeout, which allows you to say how long a thread should wait to get access to the bean instance before giving up.