You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by bu...@apache.org on 2015/07/27 00:19:48 UTC

svn commit: r959689 [4/4] - in /websites/production/tapestry/content: ./ cache/

Modified: websites/production/tapestry/content/using-jsr-330-standard-annotations.html
==============================================================================
--- websites/production/tapestry/content/using-jsr-330-standard-annotations.html (original)
+++ websites/production/tapestry/content/using-jsr-330-standard-annotations.html Sun Jul 26 22:19:48 2015
@@ -62,37 +62,56 @@
 <div class="clearer"></div>
 
   <div id="breadcrumbs">
-        <a href="index.html">Apache Tapestry</a>&nbsp;&gt;&nbsp;<a href="documentation.html">Documentation</a>&nbsp;&gt;&nbsp;<a href="user-guide.html">User Guide</a>&nbsp;&gt;&nbsp;<a href="ioc.html">IoC</a>&nbsp;&gt;&nbsp;<a href="using-jsr-330-standard-annotations.html">Using JSR 330 standard annotations</a>
+        <a href="index.html">Apache Tapestry</a>&nbsp;&gt;&nbsp;<a href="documentation.html">Documentation</a>&nbsp;&gt;&nbsp;<a href="user-guide.html">User Guide</a>&nbsp;&gt;&nbsp;<a href="ioc.html">IOC</a>&nbsp;&gt;&nbsp;<a href="using-jsr-330-standard-annotations.html">Using JSR 330 standard annotations</a>
     <a class="edit" title="Edit this page (requires approval -- just ask on the mailing list)" href="https://cwiki.apache.org/confluence/pages/editpage.action?pageId=26120928">edit</a>
   </div>
 
 <div id="content">
-<div id="ConfluenceContent">
+<div id="ConfluenceContent"><p>
 
-
-
-<div class="confluence-information-macro confluence-information-macro-information"><p class="title">Added in 5.3</p><span class="aui-icon aui-icon-small aui-iconfont-info confluence-information-macro-icon"></span><div class="confluence-information-macro-body">
+</p><div class="confluence-information-macro confluence-information-macro-information"><p class="title">Added in 5.3</p><span class="aui-icon aui-icon-small aui-iconfont-info confluence-information-macro-icon"></span><div class="confluence-information-macro-body">
 </div></div>
 <div style="border-right: 20px solid #D8E4F1;border-left: 20px solid #D8E4F1;">
-</div>
-<div class="navmenu" style="float:right; background:#eee; margin:3px; padding:3px">
-<div class="error"><span class="error">Error formatting macro: contentbylabel: com.atlassian.confluence.api.service.exceptions.BadRequestException: Could not parse cql : null</span> </div></div>
+<p>&#160;</p></div><strong>JSR-330 annotations</strong> can be used for injection in Tapestry 5.3 and later.<div class="aui-label" style="float:right" title="Related Articles">
+
+
 
-<p>Starting from Tapestry 5.3, it's possible to use JSR-330 annotations for injection.&#160;</p>
 
-<p>The following table demonstrates that most of annotations in Tapestry IoC and JSR-330 are interchangeable. However, there are few differences in semantics.</p>
 
 
-<div class="table-wrap"><table class="confluenceTable"><tbody><tr><th colspan="1" rowspan="1" class="confluenceTh"><p> JSR-330 <br clear="none" class="atl-forced-newline">
-javax.inject </p></th><th colspan="1" rowspan="1" class="confluenceTh"><p> Tapestry <br clear="none" class="atl-forced-newline">
-org.apache.tapestry5.ioc.annotations </p></th><th colspan="1" rowspan="1" class="confluenceTh"><p> Comment </p></th></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p> @Inject </p></td><td colspan="1" rowspan="1" class="confluenceTd"><p> @Inject </p></td><td colspan="1" rowspan="1" class="confluenceTd"><p> - </p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p> @Inject <br clear="none" class="atl-forced-newline">
-@Named </p></td><td colspan="1" rowspan="1" class="confluenceTd"><p> @InjectService </p></td><td colspan="1" rowspan="1" class="confluenceTd"><p> - </p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p> @Scope </p></td><td colspan="1" rowspan="1" class="confluenceTd"><p> @Scope </p></td><td colspan="1" rowspan="1" class="confluenceTd"><p> - </p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p> @Qualifier </p></td><td colspan="1" rowspan="1" class="confluenceTd"><p> - </p></td><td colspan="1" rowspan="1" class="confluenceTd"><p> Tapestry marker annotations don&#8217;t need any qualifier annotations </p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p> @Singleton </p></td><td colspan="1" rowspan="1" class="confluenceTd"><p> - </p></td><td colspan="1" rowspan="1" class="confluenceTd"><p> By default all Tapestry services are singletons </p></td></tr></tbody></table></div>
 
 
-<h1 id="UsingJSR330standardannotations-FieldInjection">Field Injection</h1>
+<h3>Related Articles</h3>
 
-<p>Let&#8217;s start with field injection. In Tapestry the injection into fields is triggered by&#160;<a shape="rect" class="external-link" href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/annotations/Inject.html">@Inject</a>&#160;or&#160;<a shape="rect" class="external-link" href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/annotations/InjectService.html">@InjectService</a>&#160;annotations. When @Inject annotation is present on a field, Tapestry tries to resolve the object to inject by the type of the field. If several implementations of the same service interface are available in the registry, you have to disambiguate which implementation you want to be injected. This can be done by placing the&#160;<a shape="rect" class="external-link" href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/annotations/InjectService.html">@InjectService</a>&#160;annotation on the injection point.</p>
-<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+<ul class="content-by-label"><li>
+        <div>
+                <span class="icon aui-icon aui-icon-small aui-iconfont-page-default" title="Page">Page:</span>        </div>
+
+        <div class="details">
+                        <a shape="rect" href="injection-in-detail.html">Injection in Detail</a>
+                
+                        
+                    </div>
+    </li><li>
+        <div>
+                <span class="icon aui-icon aui-icon-small aui-iconfont-page-default" title="Page">Page:</span>        </div>
+
+        <div class="details">
+                        <a shape="rect" href="injection-faq.html">Injection FAQ</a>
+                
+                        
+                    </div>
+    </li><li>
+        <div>
+                <span class="icon aui-icon aui-icon-small aui-iconfont-page-default" title="Page">Page:</span>        </div>
+
+        <div class="details">
+                        <a shape="rect" href="injection.html">Injection</a>
+                
+                        
+                    </div>
+    </li></ul>
+</div><p>The following table demonstrates that most of annotations in Tapestry IoC and JSR-330 are interchangeable. However, there are few differences in semantics.</p><div class="table-wrap"><table class="confluenceTable"><tbody><tr><th colspan="1" rowspan="1" class="confluenceTh"><p>JSR-330 <br clear="none" class="atl-forced-newline"> javax.inject</p></th><th colspan="1" rowspan="1" class="confluenceTh"><p>Tapestry <br clear="none" class="atl-forced-newline"> org.apache.tapestry5.ioc.annotations</p></th><th colspan="1" rowspan="1" class="confluenceTh"><p>Comment</p></th></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p>@Inject</p></td><td colspan="1" rowspan="1" class="confluenceTd"><p>@Inject</p></td><td colspan="1" rowspan="1" class="confluenceTd"><p>-</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p>@Inject <br clear="none" class="atl-forced-newline"> @Named</p></td><td colspan="1" rowspan="1" class="confluenceTd"><p>@InjectService</p></td><td col
 span="1" rowspan="1" class="confluenceTd"><p>-</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p>@Scope</p></td><td colspan="1" rowspan="1" class="confluenceTd"><p>@Scope</p></td><td colspan="1" rowspan="1" class="confluenceTd"><p>-</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p>@Qualifier</p></td><td colspan="1" rowspan="1" class="confluenceTd"><p>-</p></td><td colspan="1" rowspan="1" class="confluenceTd"><p>Tapestry marker annotations don&#8217;t need any qualifier annotations</p></td></tr><tr><td colspan="1" rowspan="1" class="confluenceTd"><p>@Singleton</p></td><td colspan="1" rowspan="1" class="confluenceTd"><p>-</p></td><td colspan="1" rowspan="1" class="confluenceTd"><p>By default all Tapestry services are singletons</p></td></tr></tbody></table></div><h1 id="UsingJSR330standardannotations-FieldInjection">Field Injection</h1><p>Let&#8217;s start with field injection. In Tapestry the injection into fields is triggered by&#160;<a shape="
 rect" class="external-link" href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/annotations/Inject.html">@Inject</a>&#160;or&#160;<a shape="rect" class="external-link" href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/annotations/InjectService.html">@InjectService</a>&#160;annotations. When @Inject annotation is present on a field, Tapestry tries to resolve the object to inject by the type of the field. If several implementations of the same service interface are available in the registry, you have to disambiguate which implementation you want to be injected. This can be done by placing the&#160;<a shape="rect" class="external-link" href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/annotations/InjectService.html">@InjectService</a>&#160;annotation on the injection point.</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
 <pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">import org.apache.tapestry5.ioc.annotations.Inject;
 import org.apache.tapestry5.ioc.annotations.InjectService;
 
@@ -112,9 +131,7 @@ public class AuthenticationFilter implem
    ...
 
 }</pre>
-</div></div>
-<p>Now let&#8217;s see the JSR-330 equivalent of the same service. As you can see the @Inject annotations are interchangeable. The difference is how to get a service by its unique id. For this purpose JSR-330 provides the&#160;<a shape="rect" class="external-link" href="http://atinject.googlecode.com/svn/trunk/javadoc/javax/inject/Named.html" >@Named</a>&#160;annotation which accompanies the @Inject annotation.</p>
-<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+</div></div><p>Now let&#8217;s see the JSR-330 equivalent of the same service. As you can see the @Inject annotations are interchangeable. The difference is how to get a service by its unique id. For this purpose JSR-330 provides the&#160;<a shape="rect" class="external-link" href="http://atinject.googlecode.com/svn/trunk/javadoc/javax/inject/Named.html" >@Named</a>&#160;annotation which accompanies the @Inject annotation.</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
 <pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">import javax.inject.Inject;
 import javax.inject.Named;
 
@@ -134,14 +151,7 @@ public class AuthenticationFilter implem
    ...
 
 }</pre>
-</div></div>
-
-<h1 id="UsingJSR330standardannotations-ConstructorInjection">Constructor Injection</h1>
-
-<p>For constructor injection the @Inject annotations are interchangeable. You can use either JSR-330 or Tapestry annotation to mark a constructor for injection. Note that at most one constructor per class may be marked as injection point.</p>
-
-<p>However, the semantics of constructor injection are different in JSR-330 and Tapestry IoC. In JSR-330 a constructor is injectable only if the @Inject annotation is present.</p>
-<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+</div></div><h1 id="UsingJSR330standardannotations-ConstructorInjection">Constructor Injection</h1><p>For constructor injection the @Inject annotations are interchangeable. You can use either JSR-330 or Tapestry annotation to mark a constructor for injection. Note that at most one constructor per class may be marked as injection point.</p><p>However, the semantics of constructor injection are different in JSR-330 and Tapestry IoC. In JSR-330 a constructor is injectable only if the @Inject annotation is present.</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
 <pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">public class Car {
 
    public Car() { ... }
@@ -149,9 +159,7 @@ public class AuthenticationFilter implem
    @Inject
    public Car(Engine engine) { ... }
 }</pre>
-</div></div>
-<p>In Tapestry the @Inject annotation for constructors is optional. All available constructors are candidates for injection: the constructor with the most parameters will be invoked.</p>
-<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+</div></div><p>In Tapestry the @Inject annotation for constructors is optional. All available constructors are candidates for injection: the constructor with the most parameters will be invoked.</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
 <pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">public class Car {
 
    public Car() { ... }
@@ -159,9 +167,7 @@ public class AuthenticationFilter implem
    public Car(Engine engine) { ... }
 
 }</pre>
-</div></div>
-<p>When several constructors are available and you don&#8217;t want the constructor with most&#160; parameters to be injectable, you need to place the&#160;<a shape="rect" class="external-link" href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/annotations/Inject.html">@Inject</a>&#160;annotation.</p>
-<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+</div></div><p>When several constructors are available and you don&#8217;t want the constructor with most&#160; parameters to be injectable, you need to place the&#160;<a shape="rect" class="external-link" href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/annotations/Inject.html">@Inject</a>&#160;annotation.</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
 <pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">public class Car {
 
    public Car() { ... }
@@ -172,12 +178,7 @@ public class AuthenticationFilter implem
    public Car(Engine engine, Logger logger) { ... }
 
 }</pre>
-</div></div>
-
-<h1 id="UsingJSR330standardannotations-InjectionIntoPagesandComponents">Injection Into Pages and Components</h1>
-
-<p>Inside Tapestry components, injection occurs exclusively on fields. So far the injection was triggered by the&#160;<a shape="rect" class="external-link" href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/annotations/Inject.html">@Inject</a>&#160;or&#160;<a shape="rect" class="external-link" href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/annotations/InjectService.html">@InjectService</a>&#160;annotations. As of version 5.3 the injection points can also be marked with JSR-330 annotations. The following example demonstrates that.</p>
-<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+</div></div><h1 id="UsingJSR330standardannotations-InjectionIntoPagesandComponents">Injection Into Pages and Components</h1><p>Inside Tapestry components, injection occurs exclusively on fields. So far the injection was triggered by the&#160;<a shape="rect" class="external-link" href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/annotations/Inject.html">@Inject</a>&#160;or&#160;<a shape="rect" class="external-link" href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/annotations/InjectService.html">@InjectService</a>&#160;annotations. As of version 5.3 the injection points can also be marked with JSR-330 annotations. The following example demonstrates that.</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
 <pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">public class Index {
 
    @Inject
@@ -197,14 +198,7 @@ public class AuthenticationFilter implem
    void onActivate() { ... }
 
 }</pre>
-</div></div>
-
-
-
-<h1 id="UsingJSR330standardannotations-Marker/QualifierAnnotations">Marker/Qualifier Annotations</h1>
-
-<p>Both JSR-330 and Tapestry IoC allow you to disambiguate services by marker or qualifier annotations, as shown in the following example.</p>
-<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+</div></div><h1 id="UsingJSR330standardannotations-Marker/QualifierAnnotations">Marker/Qualifier Annotations</h1><p>Both JSR-330 and Tapestry IoC allow you to disambiguate services by marker or qualifier annotations, as shown in the following example.</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
 <pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">public class Index {
 
    @Inject
@@ -212,22 +206,14 @@ public class AuthenticationFilter implem
    private Greeter greeter;
 
 }</pre>
-</div></div>
-<p>Again, there is a slight difference. In JSR-330 a qualifier annotation like&#160;<em>@French</em>&#160;in the example above needs to be annotated by the&#160;<a shape="rect" class="external-link" href="http://atinject.googlecode.com/svn/trunk/javadoc/javax/inject/Qualifier.html" >@Qualifier</a>&#160;annotation.</p>
-<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+</div></div><p>Again, there is a slight difference. In JSR-330 a qualifier annotation like&#160;<em>@French</em>&#160;in the example above needs to be annotated by the&#160;<a shape="rect" class="external-link" href="http://atinject.googlecode.com/svn/trunk/javadoc/javax/inject/Qualifier.html" >@Qualifier</a>&#160;annotation.</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
 <pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">@Documented
 @Retention(RetentionPolicy.RUNTIME)
 @Target(ElementType.FIELD)
 @javax.inject.Qualifier
 public @interface French {
 }</pre>
-</div></div>
-<p>In Tapestry any annotation can be a marker annotation. You don&#8217;t need to place something like the&#160;<a shape="rect" class="external-link" href="http://atinject.googlecode.com/svn/trunk/javadoc/javax/inject/Qualifier.html" >@Qualifier</a>&#160;annotation on your marker annotation.</p>
-
-<h1 id="UsingJSR330standardannotations-MethodInjection">Method Injection</h1>
-
-<p>Injectable methods is a next slight difference. In JSR-330 a method is injectable if the @Inject annotation is present. In Tapestry the @Inject annotation is optional. An ordinary setter method is a candidate to perform injection.</p>
-<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+</div></div><p>In Tapestry any annotation can be a marker annotation. You don&#8217;t need to place something like the&#160;<a shape="rect" class="external-link" href="http://atinject.googlecode.com/svn/trunk/javadoc/javax/inject/Qualifier.html" >@Qualifier</a>&#160;annotation on your marker annotation.</p><h1 id="UsingJSR330standardannotations-MethodInjection">Method Injection</h1><p>Injectable methods is a next slight difference. In JSR-330 a method is injectable if the @Inject annotation is present. In Tapestry the @Inject annotation is optional. An ordinary setter method is a candidate to perform injection.</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
 <pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">public class Car {
 
    private Engine engine;
@@ -237,11 +223,7 @@ public @interface French {
    }
 
 }</pre>
-</div></div>
-<p>When building a&#160;<em>Car</em>&#160;instance, Tapestry IoC will try to resolve a service of type&#160;<em>Engine</em>. If available, Tapestry will perform injection by invoking the setter method.</p>
-
-<p>Besides that, module methods are injectable. Again, there is no need to mark the methods with @Inject annotation as Tapestry explicitly knows which module methods to invoke. In the following example you can see how to use&#160;<a shape="rect" class="external-link" href="http://atinject.googlecode.com/svn/trunk/javadoc/javax/inject/Named.html" >@Named</a>&#160;annotation to inject a service by id into a&#160;<em>contribute method</em>.</p>
-<div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
+</div></div><p>When building a&#160;<em>Car</em>&#160;instance, Tapestry IoC will try to resolve a service of type&#160;<em>Engine</em>. If available, Tapestry will perform injection by invoking the setter method.</p><p>Besides that, module methods are injectable. Again, there is no need to mark the methods with @Inject annotation as Tapestry explicitly knows which module methods to invoke. In the following example you can see how to use&#160;<a shape="rect" class="external-link" href="http://atinject.googlecode.com/svn/trunk/javadoc/javax/inject/Named.html" >@Named</a>&#160;annotation to inject a service by id into a&#160;<em>contribute method</em>.</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
 <pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">public class TapestryModule {
 
    @Contribute(BindingSource.class)
@@ -263,16 +245,7 @@ public @interface French {
 
    ...
 }</pre>
-</div></div>
-
-
-
-<h1 id="UsingJSR330standardannotations-Scopes">Scopes</h1>
-
-<p>By default, a JSR-330 injector creates an instance, uses the instance for one injection, and then forgets it. By placing the&#160;<a shape="rect" class="external-link" href="http://atinject.googlecode.com/svn/trunk/javadoc/javax/inject/Scope.html" >@Scope</a>&#160;annotation you can tell the injector to retain the instance for possible reuse in a later injection. If you want a service to be a singleton, you need to use the&#160;<a shape="rect" class="external-link" href="http://atinject.googlecode.com/svn/trunk/javadoc/javax/inject/Singleton.html" >@Singleton</a>&#160;annotation.</p>
-
-<p>In Tapestry, it is exactly the other way around. By default a service is a singleton. Once an instance is created, it is reused for injection. Another available scope is <em>perthread</em>, which exists primarily to help multi-threaded servlet applications. If a service has <em>perthread</em>&#160;scope, it is recreated for every incoming request.</p>
-</div>
+</div></div><h1 id="UsingJSR330standardannotations-Scopes">Scopes</h1><p>By default, a JSR-330 injector creates an instance, uses the instance for one injection, and then forgets it. By placing the&#160;<a shape="rect" class="external-link" href="http://atinject.googlecode.com/svn/trunk/javadoc/javax/inject/Scope.html" >@Scope</a>&#160;annotation you can tell the injector to retain the instance for possible reuse in a later injection. If you want a service to be a singleton, you need to use the&#160;<a shape="rect" class="external-link" href="http://atinject.googlecode.com/svn/trunk/javadoc/javax/inject/Singleton.html" >@Singleton</a>&#160;annotation.</p><p>In Tapestry, it is exactly the other way around. By default a service is a singleton. Once an instance is created, it is reused for injection. Another available scope is <em>perthread</em>, which exists primarily to help multi-threaded servlet applications. If a service has <em>perthread</em>&#160;scope, it is recreated for every
  incoming request.</p></div>
 </div>
 
 <div class="clearer"></div>