blob: 17b52f8e6bbf387879bb7ea670fb8d691ea7ba15 [file] [log] [blame]
<!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_13) on Tue Apr 28 17:34:50 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-files/index-1.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>
<font color='red'><b>This is a DRAFT specification.</b></font></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;<A HREF="#annotation_type_optional_element_summary">OPTIONAL</A></FONT></TD>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
DETAIL:&nbsp;<A HREF="#annotation_type_element_detail">ELEMENT</A></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. Applies 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.
Which values are injected depends upon the injector implementation and its
configuration.
<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>return <code>void</code>.</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>
void
<i>Identifier</i>(<i>FormalParameterList<sub>opt</sub></i>)
<i>Throws<sub>opt</sub></i>
<i>MethodBody</i></blockquote></tt>
<p>For example:
<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 by
default. This behavior can be overridden by setting <A HREF="../../javax/inject/Inject.html#optional()"><CODE>optional</CODE></A> equal to <code>true</code>.
<p>Detecting and resolving circular dependencies is left as an exercize 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>
<HR>
<P>
<!-- =========== ANNOTATION TYPE OPTIONAL MEMBER SUMMARY =========== -->
<A NAME="annotation_type_optional_element_summary"><!-- --></A>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TH ALIGN="left" COLSPAN="2"><FONT SIZE="+2">
<B>Optional Element Summary</B></FONT></TH>
</TR>
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;boolean</CODE></FONT></TD>
<TD><CODE><B><A HREF="../../javax/inject/Inject.html#optional()">optional</A></B></CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Whether or not injection is optional.</TD>
</TR>
</TABLE>
&nbsp;
<P>
<A NAME="optional()"><!-- --></A><H3>
optional</H3>
<PRE>
public abstract boolean <B>optional</B></PRE>
<DL>
<DD>Whether or not injection is optional. If <code>true</code>, the injector's
behavior varies depending on the type of injection point:
<p><ul>
<li><b>Constructors:</b> <i>Not allowed</i></li>
<li><b>Fields:</b> If a dependency matching the field can't
be found, the injector will not set the field.</li>
<li><b>Methods:</b> If a dependency matching a method parameter can't
be found, the injector will skip invoking the method entirely, even
if other arguments could be provided.</li>
</ul>
<p>If an applicable dependency has been configured but the injector
encounters an error while resolving the dependency (a transitive
dependency could be missing, for example), the injector should generate
an error, not skip the injection. For example:
<pre>
&#064;Inject(optional=true) Gps gps;</pre>
<p>If a Gps isn't available at all, the injector will simply not set the
<code>gps</code> field. If a Gps is available but an algorithm it depends
upon can't be found, the injector will generate an error.
<P>
<DD><DL>
</DL>
</DD>
<DD><DL>
</DL>
<DL>
<DT><B>Default:</B><DD>false</DD>
</DL>
</DL>
<!-- ========= 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-files/index-1.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>
<font color='red'><b>This is a DRAFT specification.</b></font></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;<A HREF="#annotation_type_optional_element_summary">OPTIONAL</A></FONT></TD>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
DETAIL:&nbsp;<A HREF="#annotation_type_element_detail">ELEMENT</A></FONT></TD>
</TR>
</TABLE>
<A NAME="skip-navbar_bottom"></A>
<!-- ======== END OF BOTTOM NAVBAR ======= -->
<HR>
</BODY>
</HTML>