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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值