`
bluebijou
  • 浏览: 8187 次
  • 性别: Icon_minigender_1
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

Spring Bean注入Tapestry page(学习中)

阅读更多

http://www.springframework.org/docs/reference/webintegration.html#view-tapestry

 

15.5.1.4. Dependency Injecting Spring Beans into Tapestry pages - Tapestry 4.0+ style

Effecting the dependency injection of Spring-managed beans into Tapestry pages in Tapestry version 4.0+ is so much simpler. All that is needed is a single add-on library, and some (small) amount of (essentially boilerplate) configuration. Simply package and deploy this library with the (any of the) other libraries required by your web application (typially in WEB-INF/lib).

You will then need to create and expose the Spring container using the method detailed previously. You can then inject Spring-managed beans into Tapestry very easily; if we are using Java5, consider the Login page from above: we simply need to annotate the appropriate getter methods in order to dependency inject the Spring-managed userService and authenticationService objects (lots of the class definition has been elided for clarity)...

package com.whatever.web.xportal.pages;

public abstract class Login extends BasePage implements ErrorProperty, PageRenderListener {
    
    @InjectObject("spring:userService")
    public abstract UserService getUserService();
    
    @InjectObject("spring:authenticationService")
    public abstract AuthenticationService getAuthenticationService();

}

We are almost done... all that remains is the HiveMind configuration that exposes the Spring container stored in the ServletContext as a HiveMind service; for example:

<!---->
<module id="com.javaforge.tapestry.spring" version="0.1.1"></module>

    <service-point id="SpringApplicationInitializer" interface="org.apache.tapestry.services.ApplicationInitializer" visibility="private"></service-point>
        <invoke-factory></invoke-factory>
            <construct class="com.javaforge.tapestry.spring.SpringApplicationInitializer"></construct>
                <set-object value="service:hivemind.lib.DefaultSpringBeanFactoryHolder" property="beanFactoryHolder"></set-object>
            
        
    

    <!---->
    <contribution configuration-id="tapestry.init.ApplicationInitializers"></contribution>
        <command id="spring-context" object="service:SpringApplicationInitializer"></command>
    


If you are using Java5 (and thus have access to annotations), then that really is it.

If you are not using Java5, then one obviously doesn't annotate one's Tapestry page classes with annotations; instead, one simply uses good old fashioned XML to declare the dependency injection; for example, inside the .page or .jwc file for the Login page (or component):

<inject property="userService" object="spring:userService"></inject>
<inject property="authenticationService" object="spring:authenticationService"></inject>

In this example, we've managed to allow service beans defined in a Spring container to be provided to the Tapestry page in a declarative fashion. The page class does not know where the service implementations are coming from, and in fact it is easy to slip in another implementation, for example, during testing. This inversion of control is one of the prime goals and benefits of the Spring Framework, and we have managed to extend it all the way up the J2EE stack in this Tapestry application.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics