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就算介绍完了,欢迎交流。
通过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就算介绍完了,欢迎交流。
相关文章推荐
- 如何使用DockerHub官方的mysql镜像生成容器
- Docker如何使用link建立容器之间的连接
- 在EasyJWeb使用spring容器
- map等关联容器 vector等序列容器 如何防止迭代器失效 即erase()的使用
- 模板函数中使用未知类型的容器--如何遍历一个未知容器
- 【idea】如何安装jetty容器,并使用。
- 在EasyJWeb使用spring容器
- 使用代码 如何限定一个layout中主容器(父布局)的大小
- 如何在自定义Listener(监听器)中使用Spring容器管理的bean
- 如何使用Azure Container Service Engine在Azure中国区部署容器服务(一):DC/OS篇
- 如何在自定义Listener(监听器)中使用Spring容器管理的bean
- C++中防止STL中迭代器失效__map/set等关联容器vector/list/deque等序列容器_如何防止迭代器失效_即erase()的使用
- 在EasyJWeb使用spring容器
- 如何使用容器内的组件
- 如何使用迭代器iterator循环删除容器中的元素?
- 如何在自定义Listener(监听器)中使用Spring容器管理的bean
- 如何使用 stack?- 每天5分钟玩转 Docker 容器技术(112)
- Flex中如何在某一容器通过visible属性或者使用hideEffect以及showEffect效果时触发一
- 淺談如何使用Delphi 2009的泛型容器類別
- Rancher中的容器如何使用固定的IP地址?-使用io.rancher.container.requested_ip标签