tapestry-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Tapestry > Using JSR 330 standard annotations
Date Mon, 04 Mar 2013 11:24:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/12/_/styles/combined.css?spaceKey=TAPESTRY&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/TAPESTRY/Using+JSR+330+standard+annotations">Using
JSR 330 standard annotations</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~bobharner">Bob
Harner</a>
    </h4>
        <div id="versionComment">
        <b>Comment:</b>
        Added missing {scrollbar} to top &amp; bottom<br />
    </div>
        <br/>
                         <h4>Changes (2)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-added-lines" style="background-color: #dfd;">{scrollbar}
<br> <br></td></tr>
            <tr><td class="diff-unchanged" >{since:since=5.3} <br>{float:right|background=#eee}
<br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br>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 _perthread_, which exists primarily to
help multi-threaded servlet applications. If a service has _perthread_&amp;nbsp;scope,
it is recreated for every incoming request. <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">
<br>{scrollbar} <br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <style type='text/css'>/*<![CDATA[*/
table.ScrollbarTable  {border: none;padding: 3px;width: 100%;padding: 3px;margin: 0px;background-color:
#f0f0f0}
table.ScrollbarTable td.ScrollbarPrevIcon {text-align: center;width: 16px;border: none;}
table.ScrollbarTable td.ScrollbarPrevName {text-align: left;border: none;}
table.ScrollbarTable td.ScrollbarParent {text-align: center;border: none;}
table.ScrollbarTable td.ScrollbarNextName {text-align: right;border: none;}
table.ScrollbarTable td.ScrollbarNextIcon {text-align: center;width: 16px;border: none;}

/*]]>*/</style><div class="Scrollbar"><table class='ScrollbarTable'><tr><td
class='ScrollbarPrevIcon'><a href="/confluence/display/TAPESTRY/Logging+in+Tapestry"><img
border='0' align='middle' src='/confluence/images/icons/back_16.gif' width='16' height='16'></a></td><td
width='33%' class='ScrollbarPrevName'><a href="/confluence/display/TAPESTRY/Logging+in+Tapestry">Logging
in Tapestry</a>&nbsp;</td><td width='33%' class='ScrollbarParent'><sup><a
href="/confluence/display/TAPESTRY/IoC"><img border='0' align='middle' src='/confluence/images/icons/up_16.gif'
width='8' height='8'></a></sup><a href="/confluence/display/TAPESTRY/IoC">IoC</a></td><td
width='33%' class='ScrollbarNextName'>&nbsp;</td></tr></table></div>



<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>Added in
5.3</b><br /></td></tr></table></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'><table
class="tableview" width="100%">
            <tr><th style="padding: 3px 3px 3px 0px">Related Articles</th></tr>
                        <tr>
            <td>
                                 <span class="icon icon-page" title=Page>Page:</span>
                         <a href="/confluence/display/TAPESTRY/Tapestry+Inversion+of+Control+FAQ">Tapestry
Inversion of Control FAQ</a>
        
                                            </td>
        </tr>
                <tr>
            <td>
                                 <span class="icon icon-page" title=Page>Page:</span>
                         <a href="/confluence/display/TAPESTRY/IoC">IoC</a>
        
                                            </td>
        </tr>
                <tr>
            <td>
                                 <span class="icon icon-page" title=Page>Page:</span>
                         <a href="/confluence/display/TAPESTRY/IoC+cookbook">IoC cookbook</a>
        
                                            </td>
        </tr>
                <tr>
            <td>
                                 <span class="icon icon-page" title=Page>Page:</span>
                         <a href="/confluence/display/TAPESTRY/Tapestry+IoC+Overview">Tapestry
IoC Overview</a>
        
                                            </td>
        </tr>
            </table>
</div>

<p>Starting from Tapestry 5.3, it's possible to use JSR-330 annotations for injection.&nbsp;</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 class='confluenceTh'> JSR-330 <br class="atl-forced-newline" />
javax.inject </th>
<th class='confluenceTh'> Tapestry <br class="atl-forced-newline" />
org.apache.tapestry5.ioc.annotations </th>
<th class='confluenceTh'> Comment </th>
</tr>
<tr>
<td class='confluenceTd'> @Inject </td>
<td class='confluenceTd'> @Inject </td>
<td class='confluenceTd'> &#45; </td>
</tr>
<tr>
<td class='confluenceTd'> @Inject <br class="atl-forced-newline" />
@Named </td>
<td class='confluenceTd'> @InjectService </td>
<td class='confluenceTd'> &#45; </td>
</tr>
<tr>
<td class='confluenceTd'> @Scope </td>
<td class='confluenceTd'> @Scope </td>
<td class='confluenceTd'> &#45; </td>
</tr>
<tr>
<td class='confluenceTd'> @Qualifier </td>
<td class='confluenceTd'> &#45; </td>
<td class='confluenceTd'> Tapestry marker annotations don’t need any qualifier annotations
</td>
</tr>
<tr>
<td class='confluenceTd'> @Singleton </td>
<td class='confluenceTd'> &#45; </td>
<td class='confluenceTd'> By default all Tapestry services are singletons </td>
</tr>
</tbody></table>
</div>


<h1><a name="UsingJSR330standardannotations-FieldInjection"></a>Field Injection</h1>

<p>Let’s start with field injection. In Tapestry the injection into fields is triggered
by&nbsp;<a href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/annotations/Inject.html"
class="external-link" rel="nofollow">@Inject</a>&nbsp;or&nbsp;<a href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/annotations/InjectService.html"
class="external-link" rel="nofollow">@InjectService</a>&nbsp;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&nbsp;<a href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/annotations/InjectService.html"
class="external-link" rel="nofollow">@InjectService</a>&nbsp;annotation on the
injection point.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">import</span> org.apache.tapestry5.ioc.annotations.Inject;
<span class="code-keyword">import</span> org.apache.tapestry5.ioc.annotations.InjectService;

...

<span class="code-keyword">public</span> class AuthenticationFilter <span class="code-keyword">implements</span>
ComponentRequestFilter {

   @InjectService(<span class="code-quote">"HttpBasic"</span>)
   <span class="code-keyword">private</span> AuthenticationService basicAuthService;

   @InjectService(<span class="code-quote">"HttpDigest"</span>)
   <span class="code-keyword">private</span> AuthenticationService digestAuthService;

   @Inject
   <span class="code-keyword">private</span> Response response;

   ...

}</pre>
</div></div>
<p>Now let’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&nbsp;<a href="http://atinject.googlecode.com/svn/trunk/javadoc/javax/inject/Named.html"
class="external-link" rel="nofollow">@Named</a>&nbsp;annotation which accompanies
the @Inject annotation.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">import</span> javax.inject.Inject;
<span class="code-keyword">import</span> javax.inject.Named;

...

<span class="code-keyword">public</span> class AuthenticationFilter <span class="code-keyword">implements</span>
ComponentRequestFilter {

   @Inject @Named(<span class="code-quote">"HttpBasic"</span>)
   <span class="code-keyword">private</span> AuthenticationService basicAuthService;

   @Inject @Named(<span class="code-quote">"HttpDigest"</span>)
   <span class="code-keyword">private</span> AuthenticationService digestAuthService;

   @Inject
   <span class="code-keyword">private</span> Response response;

   ...

}</pre>
</div></div>

<h1><a name="UsingJSR330standardannotations-ConstructorInjection"></a>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" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> class Car
{

   <span class="code-keyword">public</span> Car() { ... }

   @Inject
   <span class="code-keyword">public</span> 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" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> class Car
{

   <span class="code-keyword">public</span> Car() { ... }

   <span class="code-keyword">public</span> Car(Engine engine) { ... }

}</pre>
</div></div>
<p>When several constructors are available and you don’t want the constructor with
most&nbsp; parameters to be injectable, you need to place the&nbsp;<a href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/annotations/Inject.html"
class="external-link" rel="nofollow">@Inject</a>&nbsp;annotation.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> class Car
{

   <span class="code-keyword">public</span> Car() { ... }

   @Inject
   <span class="code-keyword">public</span> Car(Engine engine) { ... }

   <span class="code-keyword">public</span> Car(Engine engine, Logger logger)
{ ... }

}</pre>
</div></div>

<h1><a name="UsingJSR330standardannotations-InjectionIntoPagesandComponents"></a>Injection
Into Pages and Components</h1>

<p>Inside Tapestry components, injection occurs exclusively on fields. So far the injection
was triggered by the&nbsp;<a href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/annotations/Inject.html"
class="external-link" rel="nofollow">@Inject</a>&nbsp;or&nbsp;<a href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/annotations/InjectService.html"
class="external-link" rel="nofollow">@InjectService</a>&nbsp;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" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> class Index
{

   @Inject
   <span class="code-keyword">private</span> Request request;

   @javax.inject.Inject
   <span class="code-keyword">private</span> ComponentResources resources;

   @javax.inject.Inject
   @Named(<span class="code-quote">"FrenchGreeter"</span>)
   <span class="code-keyword">private</span> Greeter greeter;

   @javax.inject.Inject
   @Symbol(SymbolConstants.PRODUCTION_MODE)
   <span class="code-keyword">private</span> <span class="code-object">boolean</span>
productionMode;

   void onActivate() { ... }

}</pre>
</div></div>



<h1><a name="UsingJSR330standardannotations-Marker%2FQualifierAnnotations"></a>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" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> class Index
{

   @Inject
   @French
   <span class="code-keyword">private</span> Greeter greeter;

}</pre>
</div></div>
<p>Again, there is a slight difference. In JSR-330 a qualifier annotation like&nbsp;<em>@French</em>&nbsp;in
the example above needs to be annotated by the&nbsp;<a href="http://atinject.googlecode.com/svn/trunk/javadoc/javax/inject/Qualifier.html"
class="external-link" rel="nofollow">@Qualifier</a>&nbsp;annotation.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@javax.inject.Qualifier
<span class="code-keyword">public</span> @<span class="code-keyword">interface</span>
French {
}</pre>
</div></div>
<p>In Tapestry any annotation can be a marker annotation. You don’t need to place
something like the&nbsp;<a href="http://atinject.googlecode.com/svn/trunk/javadoc/javax/inject/Qualifier.html"
class="external-link" rel="nofollow">@Qualifier</a>&nbsp;annotation on your marker
annotation.</p>

<h1><a name="UsingJSR330standardannotations-MethodInjection"></a>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" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> class Car
{

   <span class="code-keyword">private</span> Engine engine;

   <span class="code-keyword">public</span> void setEngine(Engine engine) {
      <span class="code-keyword">this</span>.engine = engine;
   }

}</pre>
</div></div>
<p>When building a&nbsp;<em>Car</em>&nbsp;instance, Tapestry IoC
will try to resolve a service of type&nbsp;<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&nbsp;<a href="http://atinject.googlecode.com/svn/trunk/javadoc/javax/inject/Named.html"
class="external-link" rel="nofollow">@Named</a>&nbsp;annotation to inject a service
by id into a&nbsp;<em>contribute method</em>.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> class TapestryModule
{

   @Contribute(BindingSource.class)
   <span class="code-keyword">public</span> <span class="code-keyword">static</span>
void provideBindings(
         MappedConfiguration&lt;<span class="code-object">String</span>, BindingFactory&gt;
cfg,

         @Named(<span class="code-quote">"PropBindingFactory"</span>)
         BindingFactory propBindingFactory,

         @Named(<span class="code-quote">"MessageBindingFactory"</span>)
         BindingFactory messageBindingFactor ) {

      cfg.add(BindingConstants.PROP,
               propBindingFactory);
      cfg.add(BindingConstants.MESSAGE,
               messageBindingFactory);

   }

   ...
}</pre>
</div></div>



<h1><a name="UsingJSR330standardannotations-Scopes"></a>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&nbsp;<a href="http://atinject.googlecode.com/svn/trunk/javadoc/javax/inject/Scope.html"
class="external-link" rel="nofollow">@Scope</a>&nbsp;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&nbsp;<a href="http://atinject.googlecode.com/svn/trunk/javadoc/javax/inject/Singleton.html"
class="external-link" rel="nofollow">@Singleton</a>&nbsp;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>&nbsp;scope,
it is recreated for every incoming request.</p>

<style type='text/css'>/*<![CDATA[*/
table.ScrollbarTable  {border: none;padding: 3px;width: 100%;padding: 3px;margin: 0px;background-color:
#f0f0f0}
table.ScrollbarTable td.ScrollbarPrevIcon {text-align: center;width: 16px;border: none;}
table.ScrollbarTable td.ScrollbarPrevName {text-align: left;border: none;}
table.ScrollbarTable td.ScrollbarParent {text-align: center;border: none;}
table.ScrollbarTable td.ScrollbarNextName {text-align: right;border: none;}
table.ScrollbarTable td.ScrollbarNextIcon {text-align: center;width: 16px;border: none;}

/*]]>*/</style><div class="Scrollbar"><table class='ScrollbarTable'><tr><td
class='ScrollbarPrevIcon'><a href="/confluence/display/TAPESTRY/Logging+in+Tapestry"><img
border='0' align='middle' src='/confluence/images/icons/back_16.gif' width='16' height='16'></a></td><td
width='33%' class='ScrollbarPrevName'><a href="/confluence/display/TAPESTRY/Logging+in+Tapestry">Logging
in Tapestry</a>&nbsp;</td><td width='33%' class='ScrollbarParent'><sup><a
href="/confluence/display/TAPESTRY/IoC"><img border='0' align='middle' src='/confluence/images/icons/up_16.gif'
width='8' height='8'></a></sup><a href="/confluence/display/TAPESTRY/IoC">IoC</a></td><td
width='33%' class='ScrollbarNextName'>&nbsp;</td></tr></table></div>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/TAPESTRY/Using+JSR+330+standard+annotations">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=26120928&revisedVersion=6&originalVersion=5">View
Changes</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message