Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between CDI (Contexts and Dependency Injection) and DI (Dependency Injection)

The official documentation says

The most fundamental services provided by CDI are as follows:
  • Contexts: The ability to bind the lifecycle and interactions of stateful components to well-defined but extensible lifecycle contexts
  • Dependency injection: The ability to inject components into an application in a typesafe way, including the ability to choose at deployment time which implementation of a particular interface to inject

but i still do not get the difference. Spring for example is DI and we still can access the context in it.

like image 268
Toumi Avatar asked Oct 29 '25 01:10

Toumi


1 Answers

Reading How To Use DI in Java EE 6 by Antonio Goncalves, give us excellent understanding. I will quote one important part of it:

These two specifications are complementary and can’t be used one without the other.

Dependency Injection for Java (aka @Inject) defines a set of annotations (@Inject, @Named, @Provider, @Qualifier, @Scope and @Singleton) mainly used for injection. If you download this JSR (which is actually just Javadoc) and read through it, you will be surprised to see that no semantic is defined (i.e. the injection behaviour is not portable across implementation). This specification is implemented in Spring 3, Guice, and also in Java EE 6 with CDI. You will find the DI annotations in the javax.inject package.

And finally

Contexts and Dependency Injection gives semantic to JSR 330 and adds more features such as context management, events, decorators and enhanced interceptors (AOP).

like image 195
Gilberto Avatar answered Oct 31 '25 16:10

Gilberto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!