Information about @Inject in CDI / Weld and DI / Guice

I am trying to distinguish (some) the difference between CDI and DI.

What does CDI (JSR-299) do for DI (JSR-330) only with respect to @Inject?

Is it true that CDI @Injects (just like DI), however, the life cycle of what he introduced is tied to some context / scope. If the object exists in the scope, when you use @Inject, you get an instance in which there is a context / scope in which it was declared. If it does not exist, a new one will be created.

So, when you want to be sure that you get the correct instance of the object that you are entering. those. @Inject MyObject myObj; will get an instance of myObject that is in the session area (assuming I annotated it as such)

Is it correct?

+3
source share
1 answer

Disclaimer: I work for Red Hat and I am a leading CDI consortium. Therefore, my knowledge of CDI is probably better than in another DI solution. That said, I will try to give you an objective answer

Yes, it's right.

View the corresponding API:

You can see that CDI is much richer than JSR 330. The InInject specification defines only 1 interface and 5 annotations to provide a common way to declare and resolve an injection. It gives no rules regarding how components are managed, or nothing about their life cycle. CDI (which implements JSR 330) is the complete Injection Dependency specification, the implementation of which can be compared with Guice or Spring Core.

(, , , ) CDI . , (beans) . bean ( @RequestScoped bean @ApplicationScoped bean). bean , bean .

CDI JSR 346 (CDI 1.1), 9 , Java EE 7. , bean. JBoss Wildlfy 8 Oracle Glassfish 4 ( - Java EE 7) grab Weld 2.x bootstrap CDI Java SE .

1.2 JSR CDI 2.0.

CDI, (Weld - CDI), .

+7

All Articles