xref: /aosp_15_r20/external/jsr330/javadoc/javax/inject/Scope.html (revision 3ff81872dd771505ae446579ad46cd1fa54f7475)
1*3ff81872SXin Li<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2*3ff81872SXin Li<!--NewPage-->
3*3ff81872SXin Li<HTML>
4*3ff81872SXin Li<HEAD>
5*3ff81872SXin Li<!-- Generated by javadoc (build 1.5.0_16) on Mon Oct 12 16:11:19 PDT 2009 -->
6*3ff81872SXin Li<TITLE>
7*3ff81872SXin LiScope
8*3ff81872SXin Li</TITLE>
9*3ff81872SXin Li
10*3ff81872SXin Li<META NAME="keywords" CONTENT="javax.inject.Scope class">
11*3ff81872SXin Li
12*3ff81872SXin Li<LINK REL ="stylesheet" TYPE="text/css" HREF="../../stylesheet.css" TITLE="Style">
13*3ff81872SXin Li
14*3ff81872SXin Li<SCRIPT type="text/javascript">
15*3ff81872SXin Lifunction windowTitle()
16*3ff81872SXin Li{
17*3ff81872SXin Li    parent.document.title="Scope";
18*3ff81872SXin Li}
19*3ff81872SXin Li</SCRIPT>
20*3ff81872SXin Li<NOSCRIPT>
21*3ff81872SXin Li</NOSCRIPT>
22*3ff81872SXin Li
23*3ff81872SXin Li</HEAD>
24*3ff81872SXin Li
25*3ff81872SXin Li<BODY BGCOLOR="white" onload="windowTitle();">
26*3ff81872SXin Li
27*3ff81872SXin Li
28*3ff81872SXin Li<!-- ========= START OF TOP NAVBAR ======= -->
29*3ff81872SXin Li<A NAME="navbar_top"><!-- --></A>
30*3ff81872SXin Li<A HREF="#skip-navbar_top" title="Skip navigation links"></A>
31*3ff81872SXin Li<TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0" SUMMARY="">
32*3ff81872SXin Li<TR>
33*3ff81872SXin Li<TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1">
34*3ff81872SXin Li<A NAME="navbar_top_firstrow"><!-- --></A>
35*3ff81872SXin Li<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3" SUMMARY="">
36*3ff81872SXin Li  <TR ALIGN="center" VALIGN="top">
37*3ff81872SXin Li  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../javax/inject/package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A>&nbsp;</TD>
38*3ff81872SXin Li  <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> &nbsp;<FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT>&nbsp;</TD>
39*3ff81872SXin Li  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A>&nbsp;</TD>
40*3ff81872SXin Li  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A>&nbsp;</TD>
41*3ff81872SXin Li  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../index-all.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A>&nbsp;</TD>
42*3ff81872SXin Li  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A>&nbsp;</TD>
43*3ff81872SXin Li  </TR>
44*3ff81872SXin Li</TABLE>
45*3ff81872SXin Li</TD>
46*3ff81872SXin Li<TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM>
47*3ff81872SXin Li</EM>
48*3ff81872SXin Li</TD>
49*3ff81872SXin Li</TR>
50*3ff81872SXin Li
51*3ff81872SXin Li<TR>
52*3ff81872SXin Li<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
53*3ff81872SXin Li&nbsp;<A HREF="../../javax/inject/Qualifier.html" title="annotation in javax.inject"><B>PREV CLASS</B></A>&nbsp;
54*3ff81872SXin Li&nbsp;<A HREF="../../javax/inject/Singleton.html" title="annotation in javax.inject"><B>NEXT CLASS</B></A></FONT></TD>
55*3ff81872SXin Li<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
56*3ff81872SXin Li  <A HREF="../../index.html?javax/inject/Scope.html" target="_top"><B>FRAMES</B></A>  &nbsp;
57*3ff81872SXin Li&nbsp;<A HREF="Scope.html" target="_top"><B>NO FRAMES</B></A>  &nbsp;
58*3ff81872SXin Li&nbsp;<SCRIPT type="text/javascript">
59*3ff81872SXin Li  <!--
60*3ff81872SXin Li  if(window==top) {
61*3ff81872SXin Li    document.writeln('<A HREF="../../allclasses-noframe.html"><B>All Classes</B></A>');
62*3ff81872SXin Li  }
63*3ff81872SXin Li  //-->
64*3ff81872SXin Li</SCRIPT>
65*3ff81872SXin Li<NOSCRIPT>
66*3ff81872SXin Li  <A HREF="../../allclasses-noframe.html"><B>All Classes</B></A>
67*3ff81872SXin Li</NOSCRIPT>
68*3ff81872SXin Li
69*3ff81872SXin Li
70*3ff81872SXin Li</FONT></TD>
71*3ff81872SXin Li</TR>
72*3ff81872SXin Li<TR>
73*3ff81872SXin Li<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
74*3ff81872SXin Li  SUMMARY:&nbsp;REQUIRED&nbsp;|&nbsp;OPTIONAL</FONT></TD>
75*3ff81872SXin Li<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
76*3ff81872SXin LiDETAIL:&nbsp;ELEMENT</FONT></TD>
77*3ff81872SXin Li</TR>
78*3ff81872SXin Li</TABLE>
79*3ff81872SXin Li<A NAME="skip-navbar_top"></A>
80*3ff81872SXin Li<!-- ========= END OF TOP NAVBAR ========= -->
81*3ff81872SXin Li
82*3ff81872SXin Li<HR>
83*3ff81872SXin Li<!-- ======== START OF CLASS DATA ======== -->
84*3ff81872SXin Li<H2>
85*3ff81872SXin Li<FONT SIZE="-1">
86*3ff81872SXin Lijavax.inject</FONT>
87*3ff81872SXin Li<BR>
88*3ff81872SXin LiAnnotation Type Scope</H2>
89*3ff81872SXin Li<HR>
90*3ff81872SXin Li<DL>
91*3ff81872SXin Li<DT><PRE><FONT SIZE="-1">@Target(value=ANNOTATION_TYPE)
92*3ff81872SXin Li@Retention(value=RUNTIME)
93*3ff81872SXin Li@Documented
94*3ff81872SXin Li</FONT>public @interface <B>Scope</B></DL>
95*3ff81872SXin Li</PRE>
96*3ff81872SXin Li
97*3ff81872SXin Li<P>
98*3ff81872SXin LiIdentifies scope annotations. A scope annotation applies to a class
99*3ff81872SXin Li containing an injectable constructor and governs how the injector reuses
100*3ff81872SXin Li instances of the type. By default, if no scope annotation is present, the
101*3ff81872SXin Li injector creates an instance (by injecting the type's constructor), uses
102*3ff81872SXin Li the instance for one injection, and then forgets it. If a scope annotation
103*3ff81872SXin Li is present, the injector may retain the instance for possible reuse in a
104*3ff81872SXin Li later injection. If multiple threads can access a scoped instance, its
105*3ff81872SXin Li implementation should be thread safe. The implementation of the scope
106*3ff81872SXin Li itself is left up to the injector.
107*3ff81872SXin Li
108*3ff81872SXin Li <p>In the following example, the scope annotation <code>@Singleton</code> ensures
109*3ff81872SXin Li that we only have one Log instance:
110*3ff81872SXin Li
111*3ff81872SXin Li <pre>
112*3ff81872SXin Li   &#064;Singleton
113*3ff81872SXin Li   class Log {
114*3ff81872SXin Li     void log(String message) { ... }
115*3ff81872SXin Li   }</pre>
116*3ff81872SXin Li
117*3ff81872SXin Li <p>The injector generates an error if it encounters more than one scope
118*3ff81872SXin Li annotation on the same class or a scope annotation it doesn't support.
119*3ff81872SXin Li
120*3ff81872SXin Li <p>A scope annotation:
121*3ff81872SXin Li <ul>
122*3ff81872SXin Li   <li>is annotated with <code>@Scope</code>, <code>@Retention(RUNTIME)</code>,
123*3ff81872SXin Li      and typically <code>@Documented</code>.</li>
124*3ff81872SXin Li   <li>should not have attributes.</li>
125*3ff81872SXin Li   <li>is typically not <code>@Inherited</code>, so scoping is orthogonal to
126*3ff81872SXin Li      implementation inheritance.</li>
127*3ff81872SXin Li   <li>may have restricted usage if annotated with <code>@Target</code>. While
128*3ff81872SXin Li      this specification covers applying scopes to classes only, some
129*3ff81872SXin Li      injector configurations might use scope annotations
130*3ff81872SXin Li      in other places (on factory method results for example).</li>
131*3ff81872SXin Li </ul>
132*3ff81872SXin Li
133*3ff81872SXin Li <p>For example:
134*3ff81872SXin Li
135*3ff81872SXin Li <pre>
136*3ff81872SXin Li   &#064;java.lang.annotation.Documented
137*3ff81872SXin Li   &#064;java.lang.annotation.Retention(RUNTIME)
138*3ff81872SXin Li   &#064;javax.inject.Scope
139*3ff81872SXin Li   public @interface RequestScoped {}</pre>
140*3ff81872SXin Li
141*3ff81872SXin Li <p>Annotating scope annotations with <code>@Scope</code> helps the injector
142*3ff81872SXin Li detect the case where a programmer used the scope annotation on a class but
143*3ff81872SXin Li forgot to configure the scope in the injector. A conservative injector
144*3ff81872SXin Li would generate an error rather than not apply a scope.
145*3ff81872SXin Li<P>
146*3ff81872SXin Li
147*3ff81872SXin Li<P>
148*3ff81872SXin Li<DL>
149*3ff81872SXin Li<DT><B>See Also:</B><DD><A HREF="../../javax/inject/Singleton.html" title="annotation in javax.inject"><CODE>@Singleton</CODE></A></DL>
150*3ff81872SXin Li
151*3ff81872SXin Li<P>
152*3ff81872SXin Li
153*3ff81872SXin Li<P>
154*3ff81872SXin Li<!-- ========= END OF CLASS DATA ========= -->
155*3ff81872SXin Li<HR>
156*3ff81872SXin Li
157*3ff81872SXin Li
158*3ff81872SXin Li<!-- ======= START OF BOTTOM NAVBAR ====== -->
159*3ff81872SXin Li<A NAME="navbar_bottom"><!-- --></A>
160*3ff81872SXin Li<A HREF="#skip-navbar_bottom" title="Skip navigation links"></A>
161*3ff81872SXin Li<TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0" SUMMARY="">
162*3ff81872SXin Li<TR>
163*3ff81872SXin Li<TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1">
164*3ff81872SXin Li<A NAME="navbar_bottom_firstrow"><!-- --></A>
165*3ff81872SXin Li<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3" SUMMARY="">
166*3ff81872SXin Li  <TR ALIGN="center" VALIGN="top">
167*3ff81872SXin Li  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../javax/inject/package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A>&nbsp;</TD>
168*3ff81872SXin Li  <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> &nbsp;<FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT>&nbsp;</TD>
169*3ff81872SXin Li  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A>&nbsp;</TD>
170*3ff81872SXin Li  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A>&nbsp;</TD>
171*3ff81872SXin Li  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../index-all.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A>&nbsp;</TD>
172*3ff81872SXin Li  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A>&nbsp;</TD>
173*3ff81872SXin Li  </TR>
174*3ff81872SXin Li</TABLE>
175*3ff81872SXin Li</TD>
176*3ff81872SXin Li<TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM>
177*3ff81872SXin Li</EM>
178*3ff81872SXin Li</TD>
179*3ff81872SXin Li</TR>
180*3ff81872SXin Li
181*3ff81872SXin Li<TR>
182*3ff81872SXin Li<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
183*3ff81872SXin Li&nbsp;<A HREF="../../javax/inject/Qualifier.html" title="annotation in javax.inject"><B>PREV CLASS</B></A>&nbsp;
184*3ff81872SXin Li&nbsp;<A HREF="../../javax/inject/Singleton.html" title="annotation in javax.inject"><B>NEXT CLASS</B></A></FONT></TD>
185*3ff81872SXin Li<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
186*3ff81872SXin Li  <A HREF="../../index.html?javax/inject/Scope.html" target="_top"><B>FRAMES</B></A>  &nbsp;
187*3ff81872SXin Li&nbsp;<A HREF="Scope.html" target="_top"><B>NO FRAMES</B></A>  &nbsp;
188*3ff81872SXin Li&nbsp;<SCRIPT type="text/javascript">
189*3ff81872SXin Li  <!--
190*3ff81872SXin Li  if(window==top) {
191*3ff81872SXin Li    document.writeln('<A HREF="../../allclasses-noframe.html"><B>All Classes</B></A>');
192*3ff81872SXin Li  }
193*3ff81872SXin Li  //-->
194*3ff81872SXin Li</SCRIPT>
195*3ff81872SXin Li<NOSCRIPT>
196*3ff81872SXin Li  <A HREF="../../allclasses-noframe.html"><B>All Classes</B></A>
197*3ff81872SXin Li</NOSCRIPT>
198*3ff81872SXin Li
199*3ff81872SXin Li
200*3ff81872SXin Li</FONT></TD>
201*3ff81872SXin Li</TR>
202*3ff81872SXin Li<TR>
203*3ff81872SXin Li<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
204*3ff81872SXin Li  SUMMARY:&nbsp;REQUIRED&nbsp;|&nbsp;OPTIONAL</FONT></TD>
205*3ff81872SXin Li<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
206*3ff81872SXin LiDETAIL:&nbsp;ELEMENT</FONT></TD>
207*3ff81872SXin Li</TR>
208*3ff81872SXin Li</TABLE>
209*3ff81872SXin Li<A NAME="skip-navbar_bottom"></A>
210*3ff81872SXin Li<!-- ======== END OF BOTTOM NAVBAR ======= -->
211*3ff81872SXin Li
212*3ff81872SXin Li<HR>
213*3ff81872SXin Li<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>
214*3ff81872SXin Li</BODY>
215*3ff81872SXin Li</HTML>
216