summaryrefslogtreecommitdiffstats
path: root/javadoc/javax/inject/Inject.html
blob: 061999e63f9763301e9deb6447c26b990395caa4 (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--NewPage-->
<HTML>
<HEAD>
<!-- Generated by javadoc (build 1.5.0_16) on Mon Oct 12 16:11:19 PDT 2009 -->
<TITLE>
Inject
</TITLE>

<META NAME="keywords" CONTENT="javax.inject.Inject class">

<LINK REL ="stylesheet" TYPE="text/css" HREF="../../stylesheet.css" TITLE="Style">

<SCRIPT type="text/javascript">
function windowTitle()
{
    parent.document.title="Inject";
}
</SCRIPT>
<NOSCRIPT>
</NOSCRIPT>

</HEAD>

<BODY BGCOLOR="white" onload="windowTitle();">


<!-- ========= START OF TOP NAVBAR ======= -->
<A NAME="navbar_top"><!-- --></A>
<A HREF="#skip-navbar_top" title="Skip navigation links"></A>
<TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0" SUMMARY="">
<TR>
<TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1">
<A NAME="navbar_top_firstrow"><!-- --></A>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3" SUMMARY="">
  <TR ALIGN="center" VALIGN="top">
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../javax/inject/package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A>&nbsp;</TD>
  <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> &nbsp;<FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT>&nbsp;</TD>
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A>&nbsp;</TD>
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A>&nbsp;</TD>
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../index-all.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A>&nbsp;</TD>
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A>&nbsp;</TD>
  </TR>
</TABLE>
</TD>
<TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM>
</EM>
</TD>
</TR>

<TR>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
&nbsp;PREV CLASS&nbsp;
&nbsp;<A HREF="../../javax/inject/Named.html" title="annotation in javax.inject"><B>NEXT CLASS</B></A></FONT></TD>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
  <A HREF="../../index.html?javax/inject/Inject.html" target="_top"><B>FRAMES</B></A>  &nbsp;
&nbsp;<A HREF="Inject.html" target="_top"><B>NO FRAMES</B></A>  &nbsp;
&nbsp;<SCRIPT type="text/javascript">
  <!--
  if(window==top) {
    document.writeln('<A HREF="../../allclasses-noframe.html"><B>All Classes</B></A>');
  }
  //-->
</SCRIPT>
<NOSCRIPT>
  <A HREF="../../allclasses-noframe.html"><B>All Classes</B></A>
</NOSCRIPT>


</FONT></TD>
</TR>
<TR>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
  SUMMARY:&nbsp;REQUIRED&nbsp;|&nbsp;OPTIONAL</FONT></TD>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
DETAIL:&nbsp;ELEMENT</FONT></TD>
</TR>
</TABLE>
<A NAME="skip-navbar_top"></A>
<!-- ========= END OF TOP NAVBAR ========= -->

<HR>
<!-- ======== START OF CLASS DATA ======== -->
<H2>
<FONT SIZE="-1">
javax.inject</FONT>
<BR>
Annotation Type Inject</H2>
<HR>
<DL>
<DT><PRE><FONT SIZE="-1">@Target(value={METHOD,CONSTRUCTOR,FIELD})
@Retention(value=RUNTIME)
@Documented
</FONT>public @interface <B>Inject</B></DL>
</PRE>

<P>
Identifies injectable constructors, methods, and fields. May apply to static
 as well as instance members. An injectable member may have any access
 modifier (private, package-private, protected, public). Constructors are
 injected first, followed by fields, and then methods. Fields and methods
 in superclasses are injected before those in subclasses. Ordering of
 injection among fields and among methods in the same class is not specified.

 <p>Injectable constructors are annotated with <code>@Inject</code> and accept
 zero or more dependencies as arguments. <code>@Inject</code> can apply to at most
 one constructor per class.

 <p><tt><blockquote style="padding-left: 2em; text-indent: -2em;">@Inject
       <i>ConstructorModifiers<sub>opt</sub></i>
       <i>SimpleTypeName</i>(<i>FormalParameterList<sub>opt</sub></i>)
       <i>Throws<sub>opt</sub></i>
       <i>ConstructorBody</i></blockquote></tt>

 <p><code>@Inject</code> is optional for public, no-argument constructors when no
 other constructors are present. This enables injectors to invoke default
 constructors.

 <p><tt><blockquote style="padding-left: 2em; text-indent: -2em;">
       @Inject<sub><i>opt</i></sub>
       <i>Annotations<sub>opt</sub></i>
       public
       <i>SimpleTypeName</i>()
       <i>Throws<sub>opt</sub></i>
       <i>ConstructorBody</i></blockquote></tt>

 <p>Injectable fields:
 <ul>
   <li>are annotated with <code>@Inject</code>.
   <li>are not final.
   <li>may have any otherwise valid name.</li></ul>

 <p><tt><blockquote style="padding-left: 2em; text-indent: -2em;">@Inject
       <i>FieldModifiers<sub>opt</sub></i>
       <i>Type</i>
       <i>VariableDeclarators</i>;</blockquote></tt>

 <p>Injectable methods:
 <ul>
   <li>are annotated with <code>@Inject</code>.</li>
   <li>are not abstract.</li>
   <li>do not declare type parameters of their own.</li>
   <li>may return a result</li>
   <li>may have any otherwise valid name.</li>
   <li>accept zero or more dependencies as arguments.</li></ul>

 <p><tt><blockquote style="padding-left: 2em; text-indent: -2em;">@Inject
       <i>MethodModifiers<sub>opt</sub></i>
       <i>ResultType</i>
       <i>Identifier</i>(<i>FormalParameterList<sub>opt</sub></i>)
       <i>Throws<sub>opt</sub></i>
       <i>MethodBody</i></blockquote></tt>

 <p>The injector ignores the result of an injected method, but
 non-<code>void</code> return types are allowed to support use of the method in
 other contexts (builder-style method chaining, for example).

 <p>Examples:

 <pre>
   public class Car {
     // Injectable constructor
     &#064;Inject public Car(Engine engine) { ... }

     // Injectable field
     &#064;Inject private Provider&lt;Seat> seatProvider;

     // Injectable package-private method
     &#064;Inject void install(Windshield windshield, Trunk trunk) { ... }
   }</pre>

 <p>A method annotated with <code>@Inject</code> that overrides another method
 annotated with <code>@Inject</code> will only be injected once per injection
 request per instance. A method with <i>no</i> <code>@Inject</code> annotation
 that overrides a method annotated with <code>@Inject</code> will not be
 injected.

 <p>Injection of members annotated with <code>@Inject</code> is required. While an
 injectable member may use any accessibility modifier (including
 <tt>private</tt>), platform or injector limitations (like security
 restrictions or lack of reflection support) might preclude injection
 of non-public members.

 <h3>Qualifiers</h3>

 <p>A <A HREF="../../javax/inject/Qualifier.html" title="annotation in javax.inject">qualifier</A> may annotate an injectable field
 or parameter and, combined with the type, identify the implementation to
 inject. Qualifiers are optional, and when used with <code>@Inject</code> in
 injector-independent classes, no more than one qualifier should annotate a
 single field or parameter. The qualifiers are bold in the following example:

 <pre>
   public class Car {
     &#064;Inject private <b>@Leather</b> Provider&lt;Seat> seatProvider;

     &#064;Inject void install(<b>@Tinted</b> Windshield windshield,
         <b>@Big</b> Trunk trunk) { ... }
   }</pre>

 <p>If one injectable method overrides another, the overriding method's
 parameters do not automatically inherit qualifiers from the overridden
 method's parameters.

 <h3>Injectable Values</h3>

 <p>For a given type T and optional qualifier, an injector must be able to
 inject a user-specified class that:

 <ol type="a">
   <li>is assignment compatible with T and</li>
   <li>has an injectable constructor.</li>
 </ol>

 <p>For example, the user might use external configuration to pick an
 implementation of T. Beyond that, which values are injected depend upon the
 injector implementation and its configuration.

 <h3>Circular Dependencies</h3>

 <p>Detecting and resolving circular dependencies is left as an exercise for
 the injector implementation. Circular dependencies between two constructors
 is an obvious problem, but you can also have a circular dependency between
 injectable fields or methods:

 <pre>
   class A {
     &#064;Inject B b;
   }
   class B {
     &#064;Inject A a;
   }</pre>

 <p>When constructing an instance of <code>A</code>, a naive injector
 implementation might go into an infinite loop constructing an instance of
 <code>B</code> to set on <code>A</code>, a second instance of <code>A</code> to set on
 <code>B</code>, a second instance of <code>B</code> to set on the second instance of
 <code>A</code>, and so on.

 <p>A conservative injector might detect the circular dependency at build
 time and generate an error, at which point the programmer could break the
 circular dependency by injecting <A HREF="../../javax/inject/Provider.html" title="interface in javax.inject"><CODE>Provider&lt;A></CODE></A> or <code>Provider&lt;B&gt;</code> instead of <code>A</code> or <code>B</code> respectively. Calling <A HREF="../../javax/inject/Provider.html#get()"><CODE>get()</CODE></A> on the provider directly from the constructor or
 method it was injected into defeats the provider's ability to break up
 circular dependencies. In the case of method or field injection, scoping
 one of the dependencies (using <A HREF="../../javax/inject/Singleton.html" title="annotation in javax.inject">singleton scope</A>, for
 example) may also enable a valid circular relationship.
<P>

<P>
<DL>
<DT><B>See Also:</B><DD><A HREF="../../javax/inject/Qualifier.html" title="annotation in javax.inject"><CODE>@Qualifier</CODE></A>, 
<A HREF="../../javax/inject/Provider.html" title="interface in javax.inject"><CODE>Provider</CODE></A></DL>

<P>

<P>
<!-- ========= END OF CLASS DATA ========= -->
<HR>


<!-- ======= START OF BOTTOM NAVBAR ====== -->
<A NAME="navbar_bottom"><!-- --></A>
<A HREF="#skip-navbar_bottom" title="Skip navigation links"></A>
<TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0" SUMMARY="">
<TR>
<TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1">
<A NAME="navbar_bottom_firstrow"><!-- --></A>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3" SUMMARY="">
  <TR ALIGN="center" VALIGN="top">
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../javax/inject/package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A>&nbsp;</TD>
  <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> &nbsp;<FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT>&nbsp;</TD>
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A>&nbsp;</TD>
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A>&nbsp;</TD>
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../index-all.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A>&nbsp;</TD>
  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A>&nbsp;</TD>
  </TR>
</TABLE>
</TD>
<TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM>
</EM>
</TD>
</TR>

<TR>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
&nbsp;PREV CLASS&nbsp;
&nbsp;<A HREF="../../javax/inject/Named.html" title="annotation in javax.inject"><B>NEXT CLASS</B></A></FONT></TD>
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
  <A HREF="../../index.html?javax/inject/Inject.html" target="_top"><B>FRAMES</B></A>  &nbsp;
&nbsp;<A HREF="Inject.html" target="_top"><B>NO FRAMES</B></A>  &nbsp;
&nbsp;<SCRIPT type="text/javascript">
  <!--
  if(window==top) {
    document.writeln('<A HREF="../../allclasses-noframe.html"><B>All Classes</B></A>');
  }
  //-->
</SCRIPT>
<NOSCRIPT>
  <A HREF="../../allclasses-noframe.html"><B>All Classes</B></A>
</NOSCRIPT>


</FONT></TD>
</TR>
<TR>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
  SUMMARY:&nbsp;REQUIRED&nbsp;|&nbsp;OPTIONAL</FONT></TD>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
DETAIL:&nbsp;ELEMENT</FONT></TD>
</TR>
</TABLE>
<A NAME="skip-navbar_bottom"></A>
<!-- ======== END OF BOTTOM NAVBAR ======= -->

<HR>
<font size='-1'>Copyright (C) 2009 <a href='http://code.google.com/p/atinject/'>The JSR-330 Expert Group</a>. Licensed under the <a href='http://www.apache.org/licenses/LICENSE-2.0'>Apache License</a>, Version 2.0.</font>
</BODY>
</HTML>