简体   繁体   中英

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.

CDI gives you more opportunities. And provide it in some different way that Spring - is trying to rest more on the possibility of verification at compile time. But sometimes it is look like overhead. In a very small project EJB plus DI from JSF for example will be quite enough. But you can use CDI, only first it is worth reading more carefully about the main advantages in comparison with EJB's DI. There are many little details and you must think about where to use it.

I take it from " https://javatalks.ru/topics/40299 " and translate the closest answer for you. And I can advise to read this: " https://blogs.oracle.com/enterprisetechtips/using-cdi-and-dependency-injection-for-java-in-a-jsf-20-application " this is good article from Oracle engineer about using CDI and DI

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM