Java – information about @ inject in CDI / weld and di / Guice

I tried to distinguish between (some) CDI and di

About @ inject, CDI (jsr-299) only brings di (jsr-330). What is it?

CDI @ injects (like DI) is true, but the life cycle it injects is related to some background / scope If the object exists in the object when @ inject is used, an instance of the context / scope declared in it will be obtained If it does not exist, a new object is created

So when you want to make sure you're getting the right instance of the object you injected I.e. @ inject myobj myobj; You will get the MyObject instance within the session scope (assuming I have annotated it)

Is it correct?

Solution

Disclaimer: I work for red hat. I am the head of CDI cooperation specification So my understanding of CDI may be better than other Di solutions That said, I will try my best to give you an objective answer

Yes, that's right

Look at their APIs:

>Atinject > CDI 1.0 and CDI 1.1

You can see that CDI is richer than JSR 330 The atinject specification only defines one interface and five annotations to ensure that common methods of injection are declared and solved It does not provide any rules on how to manage components or on their lifecycle CDI (implementing JSR 330) is a complete dependency injection specification, which can be compared with Guice or spring core

In addition to other things (events, portable extensions, decorators, interceptors), CDI adds the concept of context As you said, it allows you to automatically manage components (beans) in your own lifecycle You can inject long-lived beans in a shorter life cycle and vice versa (that is, inject @ requestscoped beans into @ applicationsscoped beans) The bean manager will do its job to always provide you with the correct bean about the activity context

CDI also has an updated version with Java EE 7.0 released 9 months ago by JSR 346 (CDI 1.1) It adds some interesting content about bean lifecycle control You can use it in JBoss wild 8 or Oracle GlassFish 4 application servers (other servers are still using their Java EE 7 versions), or if you prefer to integrate yourself, you can get weld 2 from the servlet listener or Java se X and boot CDI

Now we are developing version 1.2 and preparing JSR proposal for CDI 2.0

To learn more about CDI, I suggest you read the weld documentation (weld is the CDI reference implementation), which is a very good introduction to the specification from the user's point of view

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>