本文轉自:http://www.aygfsteel.com/xylz/
DWR作為Ajax遠程調用的服務端得到了很多程序員的追捧,在DWR的2.x版本中已經集成了Guice的插件。
老套了,我們還是定義一個HelloWorld的服務吧,哎,就喜歡HelloWorld,不怕被別人罵!
1 public interface HelloWorld {
2
3 String sayHello();
4
5 Date getSystemDate();
6 }
7
然后寫一個簡單的實現吧。
1 public class HelloWorldImpl implements HelloWorld {
2
3 @Override
4 public Date getSystemDate() {
5 return new Date();
6 }
7
8 @Override
9 public String sayHello() {
10 return "Hello, guice";
11 }
12 }
13
然后是與dwr有關的東西了,我們寫一個dwr的listener來注入我們的模塊。
1 package cn.imxylz.study.guice.web.dwr;
2
3 import org.directwebremoting.guice.DwrGuiceServletContextListener;
4
5 /**
6 * @author xylz (www.imxylz.cn)
7 * @version $Rev: 105 $
8 */
9 public class MyDwrGuiceServletContextListener extends DwrGuiceServletContextListener{
10
11 @Override
12 protected void configure() {
13 bindRemotedAs("helloworld", HelloWorld.class).to(HelloWorldImpl.class).asEagerSingleton();
14 }
15 }
16
這里使用bindRemotedAs來將我們的服務開放出來供dwr遠程調用。
剩下的就是修改web.xml,需要配置一個dwr的Servlet并且將我們的listener加入其中。看看全部的內容。
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
4 version="2.5">
5
6 <display-name>guice-dwr</display-name>
7 <description>xylz study project - guice</description>
8
9 <listener>
10 <listener-class>cn.imxylz.study.guice.web.dwr.MyDwrGuiceServletContextListener
11 </listener-class>
12 </listener>
13 <servlet>
14 <servlet-name>dwr-invoker</servlet-name>
15 <servlet-class>org.directwebremoting.guice.DwrGuiceServlet</servlet-class>
16 <init-param>
17 <param-name>debug</param-name>
18 <param-value>true</param-value>
19 </init-param>
20 </servlet>
21 <servlet-mapping>
22 <servlet-name>dwr-invoker</servlet-name>
23 <url-pattern>/dwr/*</url-pattern>
24 </servlet-mapping>
25
26 </web-app>
27
非常簡單,也非常簡潔,其中DwrGuiceServlet的debug參數只是為了調試方便才開放的,實際中就不用寫了。
好了,看看我們的效果。
1 <html>
2 <head><title>dwr - test (www.imxylz.cn) </title>
3 <script type='text/javascript' src='/guice-dwr/dwr/interface/helloworld.js'></script>
4 <script type='text/javascript' src='/guice-dwr/dwr/engine.js'></script>
5 <script type='text/javascript' src='/guice-dwr/dwr/util.js'></script>
6 <script type='text/javascript'>
7 var showHello = function(data){
8 dwr.util.setValue('result',dwr.util.toDescriptiveString(data,1));
9 }
10 var getSystemDate = function(data){
11 dwr.util.setValue('systime',dwr.util.toDescriptiveString(data,2));
12 }
13 </script>
14 <style type='text/css'>
15 input.button { border: 1px outset; margin: 0px; padding: 0px; }
16 span { background: #ffffdd; white-space: pre; padding-left:20px;}
17 </style>
18 </head>
19 <body onload='dwr.util.useLoadingMessage()'>
20 <p>
21 <h2>Guice and DWR</h2>
22 <input class='button' type='button' value="Call HelloWorld 'sayHello' service" onclick="helloworld.sayHello(showHello)" />
23 <span id='result' ></span>
24 </p>
25 <p>
26 <input class='button' type='button' value="Call HelloWorld 'getSystemDate' service" onclick="helloworld.getSystemDate(getSystemDate)" />
27 <span id='systime' ></span>
28 </P>
29 </body>
30 </html>
我們通過兩個按鈕來獲取我們的遠程調用的結果。
Guice可真輕啊,所需的3個Jar包才不到600k。但缺點就是必須JDK1.5以上,像我們公司有幾十個大大小小的Java項目,沒有一個是1.5的,有點感慨啊。廢話少說
先建立一個service:
IHelloService.java
再來一個簡單的實現:
HelloServiceImpl.java
Struts2的配置相信大家都會了,這里需要注意的是Struts2的工廠已經變了,默認是Spring現在我們要改成Guice,請看:
struts.properties
再來看看調用代碼,稍微比Spring簡潔了些:
HelloAction.java
struts.xml配置也是非常簡單:
struts.xml
到這里,算是大功告成了,Guice文檔在與Struts2整合部分例子有誤,而且郁悶的是,竟然連Guice的Filter需要在web.xml配置都沒有說,我把配好的web.xml弄出來給大家看看
web.xml
可以布署,運行了,輸入http://localhost:8080/struts2_guice/hello 就可以看到結果了。
如果你覺得Annotation太麻煩,或不喜歡,也可以嘗試自己實現Guice的Module,以下是一個簡單的實現:
MyModule.java
運行效果完全一模一樣,因此團隊開發如果統一風格的話Guice確實能快速不少。但目前Guice僅僅只是一個IoC,遠遠沒有Spring所涉及的那么廣,但又正如Rod Johnson反復在其《J2EE without EJB》里強調:架構要永遠 simplest, simplest 再 simplest,因此你覺得夠用,就是最好的。
總的來說,開發,運行的速度似乎又快了不少,但Guice真的能不能扛起其所說的下一代IoC容器,我們拭目以待吧。