您的位置:首页 > 产品设计 > UI/UE

EasyJWeb中如何使用Google Guice容器

2007-08-22 00:33 477 查看
本文力求简洁,希望通过一个简单的demo应用讲解EasyJWeb与Guice容器的集成。

通过EasyJWeb提供的超级IoC容器,你可以非常轻松的把Guice容器集成进来,让Guice来管理业务层的依赖关系,EasyJWeb只负责表现。我们看下面的配置:


<?xml version="1.0" encoding="utf-8"?>


<easyjf-web>


<modules inject="auto">


<module name="guice" path="/guice" form="" scope="request" action="com.easyjf.demo.action.GuiceAction" defaultPage="index" >


<page name="index" url="/guice.html" type="template"/>


</module>


</modules>


<beans>


<bean name="GuiceContainer" class="com.easyjf.container.impl.GuiceContainer" scope="singleton">


<property name="modules">


<list>


<value>com.easyjf.demo.module.GuiceModule</value>


</list>


</property>


<property name="stage">




<value>DEVELOPMENT</value>




</property>


</bean>


</beans>


</easyjf-web>

熟悉EasyJWeb的朋友都知道,上面的配置通过/guice.ejf便可以访问GuiceAction业务。特别之处在于我们在这里配置了一个“GuiceContainer”的bean,该bean负责集成EasyJWeb与Guice。属性modules的每个类都要继承Guice的AbstractModule类,以实现客户化的装配逻辑;stage属性代表Guice容器的运行环境,既生产环境或开发环境。下面我们看一下上面配置文件中GuiceModule的实现:




/** *//**


*


* @author ecsoftcn@hotmail.com


*


* @version $Id: GuiceModule.java, 2007-4-23 上午03:31:33 Tony Exp $


*/




public class GuiceModule extends AbstractModule

{






/**//*


* @see com.google.inject.AbstractModule#configure()


*/


@Override




protected void configure()

{




this.bindInterceptor(any(), annotatedWith(Logging.class), new LoggingInterceptor());




}




}
至于这个类的具体装配逻辑我在此不详细描述,读者只要知道是绑定拦截器就可以了,有兴趣的可以参阅Guice的文档。在configure方法中,你可以随心所欲的实现任何客户化的装配逻辑。接下来我们再看一下上面GuiceAction的实现:




/** *//**


*


* @author ecsoftcn@hotmail.com


*


* @version $Id: GuiceAction.java, 2007-4-23 上午03:15:47 Tony Exp $


*/


@SessionScoped




public class GuiceAction implements IWebAction

{




@Inject


private GuiceService guiceService;




private int count = 0;






/**//*


* @see com.easyjf.web.IWebAction#execute(com.easyjf.web.WebForm, com.easyjf.web.Module)


*/


@Logging




public Page execute(WebForm form, Module module) throws Exception

{




count++;




form.addResult("message", guiceService.sayHelloToGuice());




form.addResult("count", count);




return module.findPage("index");


}




}
注意到上面的代码中,GuiceAction实现了EasyJWeb的IWebAction接口,不同的地方是这个类中多了几个Annotation:@SessionScoped,@Inject和@Logging,:@SessionScoped代表该类的作用域;@Inject代表该类引用了Guice容器中的一个bean[GuiceService];@Logging代表这个方法需要记录日志,就是通过刚才的拦截器实现的。下面给出GuiceService极其实现类:




/** *//**


*


* @author ecsoftcn@hotmail.com


*


* @version $Id: GuiceService.java, 2007-4-23 上午03:16:20 Tony Exp $


*/


@ImplementedBy(GuiceServiceImpl.class)




public interface GuiceService

{




public String sayHelloToGuice();




}
实现:




/** *//**


*


* @author ecsoftcn@hotmail.com


*


* @version $Id: GuiceServiceImpl.java, 2007-4-23 上午03:17:24 Tony Exp $


*/


@Singleton




public class GuiceServiceImpl implements GuiceService

{






/**//*


* @see com.easyjf.demo.GuiceService#sayHelloToGuice()


*/


@Logging




public String sayHelloToGuice()

{




System.out.println("Execute GuiceServiceImpl#sayHelloToGuice()");




return "Hello, Guice!";




}




}
这两个类中都使用了Guice的Annotation来声明装配原则,具体含义请参考Guice文档。

到此为止,一个简单的Demo就算介绍完了,欢迎交流。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: