本文力求简洁,希望通过一个简单的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">
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
<value>DEVELOPMENT</value>
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
</property>
</bean>
</beans>
</easyjf-web>
熟悉EasyJWeb的朋友都知道,上面的配置通过/guice.ejf便可以访问GuiceAction业务。特别之处在于我们在这里配置了一个“GuiceContainer”的bean,该bean负责集成EasyJWeb与Guice。属性modules的每个类都要继承Guice的AbstractModule类,以实现客户化的装配逻辑;stage属性代表Guice容器的运行环境,既生产环境或开发环境。下面我们看一下上面配置文件中GuiceModule的实现:
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
/** *//**
*
* @author ecsoftcn@hotmail.com
*
* @version $Id: GuiceModule.java, 2007-4-23 上午03:31:33 Tony Exp $
*/
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class GuiceModule extends AbstractModule
{
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
* @see com.google.inject.AbstractModule#configure()
*/
@Override
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
protected void configure()
{
this.bindInterceptor(any(), annotatedWith(Logging.class), new LoggingInterceptor());
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
}
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
}
至于这个类的具体装配逻辑我在此不详细描述,读者只要知道是绑定拦截器就可以了,有兴趣的可以参阅Guice的文档。在configure方法中,你可以随心所欲的实现任何客户化的装配逻辑。接下来我们再看一下上面GuiceAction的实现:
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
/** *//**
*
* @author ecsoftcn@hotmail.com
*
* @version $Id: GuiceAction.java, 2007-4-23 上午03:15:47 Tony Exp $
*/
@SessionScoped
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class GuiceAction implements IWebAction
{
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
@Inject
private GuiceService guiceService;
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
private int count = 0;
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
* @see com.easyjf.web.IWebAction#execute(com.easyjf.web.WebForm, com.easyjf.web.Module)
*/
@Logging
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Page execute(WebForm form, Module module) throws Exception
{
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
count++;
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
form.addResult("message", guiceService.sayHelloToGuice());
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
form.addResult("count", count);
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
return module.findPage("index");
}
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
}
注意到上面的代码中,GuiceAction实现了EasyJWeb的IWebAction接口,不同的地方是这个类中多了几个Annotation:@SessionScoped,@Inject和@Logging,:@SessionScoped代表该类的作用域;@Inject代表该类引用了Guice容器中的一个bean[GuiceService];@Logging代表这个方法需要记录日志,就是通过刚才的拦截器实现的。下面给出GuiceService极其实现类:
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
/** *//**
*
* @author ecsoftcn@hotmail.com
*
* @version $Id: GuiceService.java, 2007-4-23 上午03:16:20 Tony Exp $
*/
@ImplementedBy(GuiceServiceImpl.class)
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
public interface GuiceService
{
public String sayHelloToGuice();
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
}
实现:
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
/** *//**
*
* @author ecsoftcn@hotmail.com
*
* @version $Id: GuiceServiceImpl.java, 2007-4-23 上午03:17:24 Tony Exp $
*/
@Singleton
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class GuiceServiceImpl implements GuiceService
{
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
* @see com.easyjf.demo.GuiceService#sayHelloToGuice()
*/
@Logging
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public String sayHelloToGuice()
{
System.out.println("Execute GuiceServiceImpl#sayHelloToGuice()");
return "Hello, Guice!";
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
}
![](http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif)
}
这两个类中都使用了Guice的Annotation来声明装配原则,具体含义请参考Guice文档。
到此为止,一个简单的Demo就算介绍完了,欢迎交流。