Vicky
Vicky

Reputation: 5540

JBoss cdi-api uses

I was looking for some help on CDI APIs. I'm sorry if my question looks naive, I tried looking on net for some high level description on CDI APIs, but couldn't get it right.

The javadoc says:

Contexts and Dependency Injection (CDI) defines a set of complementary services that help improve the structure of application code.

My question is in EJB 3.X we already have Annotations for helping with DI and injecting resources like PersistenceContext and other kind of resources. So where exactly the CDI APIs will be helpful? In plain web-app/standalone Java programs using J2SE?

Upvotes: 0

Views: 1283

Answers (2)

Krzysztof Miksa
Krzysztof Miksa

Reputation: 1539

DI in Java EE5 allows you inject only resources like JDBC DataSource, JPA EntityManager, UserTransaction, Web Services, EJBs etc. All this resources was managed by container.

With EE6 and with CDI in particular you aren't restricted to inject only resources - you can inject everything (every bean). Look at annotations which come with CDI specification: @Inject, @Named, @Scope, @Singleton etc. CDI give you features like events, decorators etc.

Look at this tutorial, it should help you to understand CDI: http://java.dzone.com/articles/cdi-di-p2

Upvotes: 1

Jan Groth
Jan Groth

Reputation: 14636

EJB 3.0 comes with dependency injection on resource- and EJB-level - which is pretty cool already :-)

What CDI does (and which is even cooler) - it lowers the barrier to dependency injection to so-called "managed beans" (JSR 316) - which (among others) defines the minimal set of preconditions a class needs to benefit from dependency injection. Just slightly simplifying, one can say that all classes in a CDI project are managed beans and therefore are eligable for DI.

To summarize what CDI brings over EJB 3.0 in terms of DI:

  • you don't need EJBs anymore, CDI basically works with POJOs. That's truly lightweight, because it allows you to use EJBs when you need EJB, not when you need DI.
  • DI turns stateful - different dependencies live in different scopes - something EJB 3.0 completely fails to deliver.
  • you can benefit from a typesafe and loosely coupled interceptor mechanism
  • you can benefit from a typesafe and loosely coupled mechanism

Have a look at the first chapter here, and you'll get the idea :-)

Upvotes: 2

Related Questions