resources.xml
6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [ ]>
<chapter id="resources">
<title>Java EE component environment resources</title>
<para>
Java EE 5 already introduced some limited support for dependency injection, in the form of component
environment injection. A component environment resource is a Java EE component, for example a JDBC
datasource, JMS queue or topic, JPA persistence context, remote EJB or web service.
</para>
<para>
Naturally, there is now a slight mismatch with the new style of dependency injection in CDI. Most notably,
component environment injection relies on string-based names to qualify ambiguous types, and there is no
real consistency as to the nature of the names (sometimes a JNDI name, sometimes a persistence unit name,
sometimes an EJB link, sometimes a nonportable "mapped name"). Producer fields turned out to be an elegant
adaptor to reduce all this complexity to a common model and get component environment resources to
participate in the CDI system just like any other kind of bean.
</para>
<para>
Fields have a duality in that they can both be the target of Java EE component environment injection and be
declared as a CDI producer field. Therefore, they can define a mapping from a string-based name in the
component environment, to a combination of type and qualifiers used in the world of typesafe injection. We
call a producer field that represents a reference to an object in the Java EE component environment a
<emphasis>resource</emphasis>.
</para>
<section>
<title>Defining a resource</title>
<para>The CDI specification uses the term <emphasis>resource</emphasis> to refer, generically, to any of
the following kinds of object which might be available in the Java EE component environment:</para>
<itemizedlist>
<listitem>
<para>JDBC <literal>Datasource</literal>s, JMS <literal>Queue</literal>s, <literal>Topic</literal>s
and <literal>ConnectionFactory</literal>s, JavaMail <literal>Session</literal>s and other transactional
resources including JCA connectors,</para>
</listitem>
<listitem>
<para>JPA <literal>EntityManager</literal>s and <literal>EntityManagerFactory</literal>s,</para>
</listitem>
<listitem>
<para>remote EJBs, and</para>
</listitem>
<listitem>
<para>web services</para>
</listitem>
</itemizedlist>
<para>We declare a resource by annotating a producer field with a component environment injection
annotation: <literal>@Resource</literal>, <literal>@EJB</literal>, <literal>@PersistenceContext</literal>,
<literal>@PersistenceUnit</literal> or <literal>@WebServiceRef</literal>.</para>
<programlisting role="JAVA"><![CDATA[@Produces @WebServiceRef(lookup="java:app/service/Catalog")
Catalog catalog;]]></programlisting>
<programlisting role="JAVA"><![CDATA[@Produces @Resource(lookup="java:global/env/jdbc/CustomerDatasource")
@CustomerDatabase Datasource customerDatabase;]]></programlisting>
<programlisting role="JAVA"><![CDATA[@Produces @PersistenceContext(unitName="CustomerDatabase")
@CustomerDatabase EntityManager customerDatabasePersistenceContext;]]></programlisting>
<programlisting role="JAVA"><![CDATA[@Produces @PersistenceUnit(unitName="CustomerDatabase")
@CustomerDatabase EntityManagerFactory customerDatabasePersistenceUnit;]]></programlisting>
<programlisting role="JAVA"><![CDATA[@Produces @EJB(ejbLink="../their.jar#PaymentService")
PaymentService paymentService;]]></programlisting>
<para>The field may be static (but not final).</para>
<para>A resource declaration really contains two pieces of information:</para>
<itemizedlist>
<listitem>
<para>the JNDI name, EJB link, persistence unit name, or other metadata needed to obtain a
reference to the resource from the component environment, and</para>
</listitem>
<listitem>
<para>the type and qualifiers that we will use to inject the reference into our beans.</para>
</listitem>
</itemizedlist>
<tip>
<para>It might feel strange to be declaring resources in Java code. Isn't this stuff that might be
deployment-specific? Certainly, and that's why it makes sense to declare your resources in a class
annotated <literal>@Alternative</literal>.</para>
</tip>
</section>
<section>
<title>Typesafe resource injection</title>
<para>
These resources can now be injected in the usual way.
</para>
<programlisting role="JAVA"><![CDATA[@Inject Catalog catalog;]]></programlisting>
<programlisting role="JAVA"><![CDATA[@Inject @CustomerDatabase Datasource customerDatabase;]]></programlisting>
<programlisting role="JAVA"><![CDATA[@Inject @CustomerDatabase EntityManager customerDatabaseEntityManager;]]></programlisting>
<programlisting role="JAVA"><![CDATA[@Inject @CustomerDatabase EntityManagerFactory customerDatabaseEntityManagerFactory;]]></programlisting>
<programlisting role="JAVA"><![CDATA[@Inject PaymentService paymentService;]]></programlisting>
<para>
The bean type and qualifiers of the resource are determined by the producer field declaration.
</para>
<para>
It might seem like a pain to have to write these extra producer field declarations, just to gain an additional
level of indirection. You could just as well use component environment injection directly, right? But remember
that you're going to be using resources like the <literal>EntityManager</literal> in several different beans.
Isn't it nicer and more typesafe to write
</para>
<programlisting>@Inject @CustomerDatabase EntityManager</programlisting>
<para>instead of</para>
<programlisting>@PersistenceContext(unitName="CustomerDatabase") EntityManager</programlisting>
<para>
all over the place?
</para>
</section>
</chapter>