Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java EE CDI Class-based injection

Tags:

jakarta-ee

cdi

I am reading up on Java EE CDI and I am a confused about how class-based Resource injection works

The Java EE 6 tutorial gives the following example usage:

@Resource(name="myMessageQueue",
                type="javax.jms.ConnectionFactory")
public class SomeMessageBean {
...
}

I get how it is declared but how is the declared resource supposed to be used in the SomeMessageBeanClass? What is the myMessageQueue resource injected into?

like image 398
s.tikoo Avatar asked Nov 29 '12 19:11

s.tikoo


1 Answers

@Resource in an annotation that originates from EJB.

Its lineage goes back to the days before injection into fields was possible. In those days, "injection" meant putting something into a kind of map that is associated with each EJB bean; the so-called ENC (Enterprise Naming Context).

You can access this "map" by requesting the "java:/comp" namespace from the InitialContext when inside an EJB, or directly from the EJB context (which you have to inject as well).

These days using the ENC seems rare. Field injection is much more convenient.

like image 112
Arjan Tijms Avatar answered Sep 28 '22 07:09

Arjan Tijms