tapestry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Tapestry > Integrating with Spring Framework
Date Sat, 01 Jan 2011 23:15:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1810/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/Integrating+with+Spring+Framework">Integrating
with Spring Framework</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 link to new Spring Security page, added "Related Articles" box (using "spring"
label), moved Contents down a notch, fixed heading levels for better TOC<br />
    </div>
        <br/>
                         <h4>Changes (11)</h4>
                                 
    
<div id="page-diffs">
            <table class="diff" cellpadding="0" cellspacing="0">
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">{toc:type=list|maxLevel=3}
<br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">{float:right|background=#eee}
<br>{contentbylabel:title=Related Articles|showLabels=false|showSpace=false|space=@self|labels=spring}
<br>{float}  <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">h1.
Integrate Spring into Tapestry <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">Tapestry
easily integrates with Spring Framework, allowing beans defined by Spring to be injected into
Tapestry IoC services, and into Tapestry components.  In addition, with Tapestry 5.2 and later,
you can also go the other way, injecting Tapestry services in Spring beans. <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">We
provide an integration between Tapestry and Spring, allowing beans defined by Spring to be
injected into Tapestry IoC services, and into Tapestry components. <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">*Contents*
<br>{toc:type=list|maxLevel=3} <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">For
integrating Spring Security applications, see [Spring Security]. <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h2. Spring Version <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >This module is compiled and tested
against Spring <span class="diff-added-words"style="background-color: #dfd;">Framework</span>
2.5.6. It should be reasonable to override the dependency to earlier versions of Spring, though
the code makes use of some APIs that were added to Spring to support JDK 1.5 annotations.
<br></td></tr>
            <tr><td class="diff-unchanged" > <br>h2. Usage <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >A new chain-of-command service, [ApplicationContextCustomizer|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/spring/ApplicationContextCustomizer.html]
allows the application context, created by Tapestry, to be customized as it is created. You
may contribute your own ApplicationContextCustomizer instances as needed. <br> <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words">h<span
class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">1</span><span
class="diff-added-chars"style="background-color: #dfd;">2</span>.</span> 5.0
Compatibility Mode <br></td></tr>
            <tr><td class="diff-unchanged" > <br>In some circumstances,
it is desirable to configure the Spring ApplicationContext externally. The context &lt;config-param&gt;
&quot;tapestry.use-external-spring-context&quot; can be configured to &quot;true&quot;.
Tapestry will then use an existing ApplicationContext, provided by a Spring ContextLoaderListener.
You will still be able to inject Spring beans into Tapestry components and services, and the
ApplicationContext service will be visible ... but you will not be able to inject Tapestry
IoC services into Spring beans. <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >This option provides compatibility
with the tapestry-spring 5.0, including exposing Spring beans as Tapestry IoC services (something
that no longer occurs unless compatibility mode is enabled). <br> <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words">h<span
class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">1</span><span
class="diff-added-chars"style="background-color: #dfd;">2</span>.</span> Changes
From 5.0 <br></td></tr>
            <tr><td class="diff-unchanged" > <br>The changes below represent
an unfortunate backwards compatibility issue. If necessary, you can still use tapestry-spring
version 5.0.18 with the rest of Tapestry. <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >- You no longer create a ContextLoaderListener.
<br> <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words">h<span
class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">1</span><span
class="diff-added-chars"style="background-color: #dfd;">2</span>.</span> Limitations
<br></td></tr>
            <tr><td class="diff-unchanged" > <br>Non-singleton beans are
not handled properly. Tapestry will request the beans from the application context in a manner
unsuitable for their life cycle. For the moment, you should consider the non-singleton beans
to be not injectable. Instead, inject the ApplicationContext service and obtain the non-singleton
beans as needed. <br></td></tr>
        </table>
</div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <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>
                                 <img src="/confluence/s/1810/9/_/images/icons/docs_16.gif"
height="16" width="16" border="0" align="absmiddle" title="Integrating with Spring Framework"/>
                         <a href="/confluence/display/TAPESTRY/Integrating+with+Spring+Framework">Integrating
with Spring Framework</a>
        
                                            </td>
        </tr>
                <tr>
            <td>
                                 <img src="/confluence/s/1810/9/_/images/icons/docs_16.gif"
height="16" width="16" border="0" align="absmiddle" title="Spring Security"/>
                         <a href="/confluence/display/TAPESTRY/Spring+Security">Spring
Security</a>
        
                                            </td>
        </tr>
            </table>
</div> 

<p>Tapestry easily integrates with Spring Framework, allowing beans defined by Spring
to be injected into Tapestry IoC services, and into Tapestry components.  In addition, with
Tapestry 5.2 and later, you can also go the other way, injecting Tapestry services in Spring
beans.</p>

<p><b>Contents</b></p>
<div>
<ul>
    <li><a href='#IntegratingwithSpringFramework-SpringVersion'>Spring Version</a></li>
    <li><a href='#IntegratingwithSpringFramework-Usage'>Usage</a></li>
<ul>
    <li><a href='#IntegratingwithSpringFramework-Updateyourweb.xmlfile'>Update
your web.xml file</a></li>
    <li><a href='#IntegratingwithSpringFramework-AccessingtheSpringApplicationContext'>Accessing
the Spring Application Context</a></li>
    <li><a href='#IntegratingwithSpringFramework-Injectingbeans'>Injecting beans</a></li>
    <li><a href='#IntegratingwithSpringFramework-InjectingTapestryservicesinSpringbeans'>Injecting
Tapestry services in Spring beans</a></li>
    <li><a href='#IntegratingwithSpringFramework-ConfiguringSpringwithTapestrySymbols'>Configuring
Spring with Tapestry Symbols</a></li>
</ul>
    <li><a href='#IntegratingwithSpringFramework-ApplicationContextCustomizer'>ApplicationContextCustomizer</a></li>
    <li><a href='#IntegratingwithSpringFramework-5.0CompatibilityMode'>5.0 Compatibility
Mode</a></li>
    <li><a href='#IntegratingwithSpringFramework-ChangesFrom5.0'>Changes From
5.0</a></li>
    <li><a href='#IntegratingwithSpringFramework-Limitations'>Limitations</a></li>
</ul></div>

<p>For integrating Spring Security applications, see <a href="/confluence/display/TAPESTRY/Spring+Security"
title="Spring Security">Spring Security</a>.</p>

<h2><a name="IntegratingwithSpringFramework-SpringVersion"></a>Spring Version</h2>

<p>This module is compiled and tested against Spring Framework 2.5.6. It should be reasonable
to override the dependency to earlier versions of Spring, though the code makes use of some
APIs that were added to Spring to support JDK 1.5 annotations.</p>

<h2><a name="IntegratingwithSpringFramework-Usage"></a>Usage</h2>

<p>The integration is designed to be a very thin layer on top of Spring's normal configuration
for a web application.</p>

<p>Detailed instructions are available in the <a href="http://static.springframework.org/spring/docs/2.5.x/reference/beans.html#context-create"
class="external-link" rel="nofollow">Spring documentation</a>. Please omit the part
about creating a ContextLoaderListener: this is now done automatically by Tapestry.</p>

<h3><a name="IntegratingwithSpringFramework-Updateyourweb.xmlfile"></a>Update
your web.xml file</h3>

<p>The short form is that you must make two small changes to your application's web.xml.</p>

<p>First, a special filter is used in replace of the standard TapestryFilter:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
  &lt;filter&gt;
    &lt;filter-name&gt;app&lt;/filter-name&gt;
    &lt;!-- Special filter that adds in a T5 IoC module derived from the Spring WebApplicationContext.
--&gt;
    &lt;filter-class&gt;org.apache.tapestry5.spring.TapestrySpringFilter&lt;/filter-class&gt;
  &lt;/filter&gt;</pre>
</div></div>

<p>Secondly, you may add the normal Spring configuration, consisting of an optional
&lt;context-param&gt; identifying which Spring bean configuration file(s) to load:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&lt;context-param&gt;
  &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
  &lt;param-value&gt;/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml&lt;/param-value&gt;
&lt;/context-param&gt;
</pre>
</div></div>

<p>The &lt;context-param&gt; lists the Spring bean configuration file. It is
optional and defaults to just /WEB-INF/applicationContext.xml if omitted.</p>

<h3><a name="IntegratingwithSpringFramework-AccessingtheSpringApplicationContext"></a>Accessing
the Spring Application Context</h3>

<p>By integrating Spring in Tapestry, you get full access on Spring ApplicationContext
as if you were accessing to any Tapestry service. Simply <a href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/annotations/Inject.html"
class="external-link" rel="nofollow">Inject</a> into your pages and components.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
  @Inject
  <span class="code-keyword">private</span> ApplicationContext springContext;
</pre>
</div></div>

<h3><a name="IntegratingwithSpringFramework-Injectingbeans"></a>Injecting
beans</h3>

<p>Inside your component classes, you may use the <a href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/annotations/Inject.html"
class="external-link" rel="nofollow">Inject</a> annotation. Typically, just <a
href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/annotations/Inject.html"
class="external-link" rel="nofollow">Inject</a> the field type is sufficient to identify
the Spring bean to inject:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
  @Inject
  <span class="code-keyword">private</span> UserDAO userDAO;</pre>
</div></div>

<p>Searching for Spring beans is threaded into the <a href="/confluence/display/TAPESTRY/Injection"
title="Injection">MasterObjectProvider service</a>. The Spring context becomes one
more place that Tapestry searches when determining the injection for a injected field or method
parameter.</p>



<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.2</b><br /></td></tr></table></div>
<div style="border-right: 20px solid #D8E4F1;border-left: 20px solid #D8E4F1;">
<h3><a name="IntegratingwithSpringFramework-InjectingTapestryservicesinSpringbeans"></a>Injecting
Tapestry services in Spring beans</h3>

<p>If you have configured Spring to allow annotation based injection, then you will
be able to inject Tapestry services into your Spring Beans.</p>

<p>This feature is only available when Spring ApplicationContext is not configured and
loaded externally.</p>

<p>Inside your Spring beans, you may use <a href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/annotations/Inject.html"
class="external-link" rel="nofollow">Inject</a> and <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/beans/factory/annotation/Autowired.html"
class="external-link" rel="nofollow">Autowired</a> annotations.</p>

<p>Simply add these two annotations on top the field you want to inject in your Spring
bean.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
  @Inject
  @Autowired
  <span class="code-keyword">private</span> MyService myService;
</pre>
</div></div>

<p>or use @Inject on top of arguments in @Autowired bean constructor methods</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
  <span class="code-keyword">private</span> <span class="code-keyword">final</span>
MyService myService;

  @Autowired
  <span class="code-keyword">public</span> UserDAOImpl(@Inject MyService myService)
  {
    <span class="code-keyword">this</span>.myService = myService;
  }
</pre>
</div></div>

<h3><a name="IntegratingwithSpringFramework-ConfiguringSpringwithTapestrySymbols"></a>Configuring
Spring with Tapestry Symbols</h3>

<p>This is accomplished by a BeanFactoryPostProcessors that resolves the values of 'placeholders'
from symbol values. In the following example the value of the Bean's property 'productionMode'
is the value of the Tapestry's  symbol <a href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/SymbolConstants.html#PRODUCTION_MODE"
class="external-link" rel="nofollow">tapestry.production-mode</a></p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
  <span class="code-tag">&lt;bean id=<span class="code-quote">"myBean"</span>
class=<span class="code-quote">"org.example.MyBean"</span>&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"productionMode"</span>
value=<span class="code-quote">"${tapestry.production-mode}"</span>/&gt;</span>
  <span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>

</div>

<h2><a name="IntegratingwithSpringFramework-ApplicationContextCustomizer"></a>ApplicationContextCustomizer</h2>

<p>A new chain-of-command service, <a href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/spring/ApplicationContextCustomizer.html"
class="external-link" rel="nofollow">ApplicationContextCustomizer</a> allows the
application context, created by Tapestry, to be customized as it is created. You may contribute
your own ApplicationContextCustomizer instances as needed.</p>

<h2><a name="IntegratingwithSpringFramework-5.0CompatibilityMode"></a>5.0
Compatibility Mode</h2>

<p>In some circumstances, it is desirable to configure the Spring ApplicationContext
externally. The context &lt;config-param&gt; "tapestry.use-external-spring-context"
can be configured to "true". Tapestry will then use an existing ApplicationContext, provided
by a Spring ContextLoaderListener. You will still be able to inject Spring beans into Tapestry
components and services, and the ApplicationContext service will be visible ... but you will
not be able to inject Tapestry IoC services into Spring beans.</p>

<p>This option provides compatibility with the tapestry-spring 5.0, including exposing
Spring beans as Tapestry IoC services (something that no longer occurs unless compatibility
mode is enabled).</p>

<h2><a name="IntegratingwithSpringFramework-ChangesFrom5.0"></a>Changes
From 5.0</h2>

<p>The changes below represent an unfortunate backwards compatibility issue. If necessary,
you can still use tapestry-spring version 5.0.18 with the rest of Tapestry.</p>

<ul class="alternate" type="square">
	<li>You may now use the @Inject or @InjectService annotations inside Spring beans;
these will be resolved to Tapestry services or other objects available via the MasterObjectProvider.
Please see the <a href="/confluence/display/TAPESTRY/Injection" title="Injection">detailed
guide to Injection</a>.</li>
	<li>The dependency on Spring is no longer scope "provider" and has changed to 2.5.6.</li>
	<li>Spring Beans are no longer exposed as services, unless 5.0 compatibility mode is
enabled.</li>
	<li>You no longer create a ContextLoaderListener.</li>
</ul>


<h2><a name="IntegratingwithSpringFramework-Limitations"></a>Limitations</h2>

<p>Non-singleton beans are not handled properly. Tapestry will request the beans from
the application context in a manner unsuitable for their life cycle. For the moment, you should
consider the non-singleton beans to be not injectable. Instead, inject the ApplicationContext
service and obtain the non-singleton beans as needed.</p>
    </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/Integrating+with+Spring+Framework">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=23338503&revisedVersion=11&originalVersion=10">View
Changes</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message