亚洲色图在线播放,久久99国产精品自在自在app,一二三区不卡http://www.aygfsteel.com/shiwenfeng/category/43438.html在不斷模仿、思考、總結中一步一步進步!zh-cnFri, 08 Jan 2010 05:11:08 GMTFri, 08 Jan 2010 05:11:08 GMT60guice和dwr的整合http://www.aygfsteel.com/shiwenfeng/archive/2010/01/06/308483.htmlshiwfshiwfWed, 06 Jan 2010 10:46:00 GMThttp://www.aygfsteel.com/shiwenfeng/archive/2010/01/06/308483.htmlhttp://www.aygfsteel.com/shiwenfeng/comments/308483.htmlhttp://www.aygfsteel.com/shiwenfeng/archive/2010/01/06/308483.html#Feedback0http://www.aygfsteel.com/shiwenfeng/comments/commentRss/308483.htmlhttp://www.aygfsteel.com/shiwenfeng/services/trackbacks/308483.html

本文轉自: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>

我們通過兩個按鈕來獲取我們的遠程調用的結果。



shiwf 2010-01-06 18:46 發表評論
]]>
guice和spring的整合http://www.aygfsteel.com/shiwenfeng/archive/2010/01/06/308459.htmlshiwfshiwfWed, 06 Jan 2010 08:39:00 GMThttp://www.aygfsteel.com/shiwenfeng/archive/2010/01/06/308459.htmlhttp://www.aygfsteel.com/shiwenfeng/comments/308459.htmlhttp://www.aygfsteel.com/shiwenfeng/archive/2010/01/06/308459.html#Feedback0http://www.aygfsteel.com/shiwenfeng/comments/commentRss/308459.htmlhttp://www.aygfsteel.com/shiwenfeng/services/trackbacks/308459.html閱讀全文

shiwf 2010-01-06 16:39 發表評論
]]>
guice和struts2的整合http://www.aygfsteel.com/shiwenfeng/archive/2010/01/06/308455.htmlshiwfshiwfWed, 06 Jan 2010 08:16:00 GMThttp://www.aygfsteel.com/shiwenfeng/archive/2010/01/06/308455.htmlhttp://www.aygfsteel.com/shiwenfeng/comments/308455.htmlhttp://www.aygfsteel.com/shiwenfeng/archive/2010/01/06/308455.html#Feedback0http://www.aygfsteel.com/shiwenfeng/comments/commentRss/308455.htmlhttp://www.aygfsteel.com/shiwenfeng/services/trackbacks/308455.html

Guice可真輕啊,所需的3Jar包才不到600k。但缺點就是必須JDK1.5以上,像我們公司有幾十個大大小小的Java項目,沒有一個是1.5的,有點感慨啊。廢話少說

 先建立一個service:

 

IHelloService.java

Java代碼
  1. package com.leo.service;  
  2.   
  3. import com.google.inject.ImplementedBy;  
  4. import com.leo.service.impl.HelloServiceImpl;  
  5.   
  6. /* 
  7.  * 采用annotation進行接口與實現類之間的綁定 
  8.  * 注意:接口與實現類之間綁定是必須的,如果只是單獨一個類,沒有接口, 
  9.  * 那么Guice會隱式的自動幫你注入。并且接口此是不應該聲明注入域范圍的, 
  10.  * 應該在其實現地方聲明 
  11.  * 
  12.  */  
  13. @ImplementedBy(HelloServiceImpl.class)  
  14. public interface IHelloService {  
  15.     public String sayHello(String str);  
  16. }  

 

 

再來一個簡單的實現:

 

HelloServiceImpl.java

Java代碼
  1. package com.leo.service.impl;  
  2.   
  3. import com.google.inject.Singleton;  
  4. import com.leo.service.IHelloService;  
  5.   
  6. /* 
  7.  * 這里如果默認不用annotation標注其作用域,或在自定義的module也不指定的話 
  8.  * 默認的創建對象的方式是類似于Spring的prototype,在此處因為僅僅是一個stateless service 
  9.  * 我們用@Singleton來標注它,更多的作用域可看Guice文檔 
  10.  *  
  11.  * 注意:與標注@Singleton等效的工作也可以在自定義的module里來實現 
  12.  */  
  13.   
  14. @Singleton  
  15. public class HelloServiceImpl implements IHelloService {  
  16.   
  17.     public String sayHello(String str) {  
  18.         return new StringBuilder("Hello " + str + " !").toString();  
  19.     }  
  20.   
  21. }  

 

 

Struts2的配置相信大家都會了,這里需要注意的是Struts2的工廠已經變了,默認是Spring現在我們要改成Guice,請看:

 

struts.properties

Java代碼
  1. struts.objectFactory = guice  
  2. #如果自已想實現Module接口,則下面注釋請去掉  
  3. #guice.module=com.leo.module.MyModule  
  4. struts.action.extension=  

 

 

再來看看調用代碼,稍微比Spring簡潔了些:

 

HelloAction.java

Java代碼
  1. package com.leo.action;  
  2.   
  3. import com.google.inject.Inject;  
  4. import com.leo.service.IHelloService;  
  5. import com.opensymphony.xwork2.ActionSupport;  
  6.   
  7. public class HelloAction extends ActionSupport {  
  8.   
  9.     private static final long serialVersionUID = -338076402728419581L;  
  10.   
  11.     /* 
  12.      * 通過field字段進行注入,除此之外,還有construct, method注入均可 
  13.      */  
  14.     @Inject  
  15.     private IHelloService helloService;  
  16.   
  17.     private String message;  
  18.   
  19.     public String getMessage() {  
  20.         return message;  
  21.     }  
  22.   
  23.     public void setMessage(String message) {  
  24.         this.message = message;  
  25.     }  
  26.   
  27.     public String execute() {  
  28.         message = helloService.sayHello("leo");  
  29.         return SUCCESS;  
  30.     }  
  31.   
  32. }  

 

 

struts.xml配置也是非常簡單:

 

struts.xml

Xml代碼
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.     <package name="default" extends="struts-default">  
  8.         <action name="hello" class="com.leo.action.HelloAction">  
  9.             <result>index.jsp</result>  
  10.         </action>  
  11.     </package>  
  12. </struts>  

 

 

到這里,算是大功告成了,Guice文檔在與Struts2整合部分例子有誤,而且郁悶的是,竟然連Guice的Filter需要在web.xml配置都沒有說,我把配好的web.xml弄出來給大家看看

 

web.xml

Xml代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  5.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  6.   
  7.     <filter>  
  8.         <filter-name>guice</filter-name>  
  9.         <filter-class>  
  10.             com.google.inject.servlet.GuiceFilter  
  11.         </filter-class>  
  12.     </filter>  
  13.   
  14.     <filter>  
  15.         <filter-name>struts</filter-name>  
  16.         <filter-class>  
  17.             org.apache.struts2.dispatcher.FilterDispatcher  
  18.         </filter-class>  
  19.     </filter>  
  20.   
  21.     <filter-mapping>  
  22.         <filter-name>guice</filter-name>  
  23.         <url-pattern>/*</url-pattern>  
  24.     </filter-mapping>  
  25.   
  26.     <filter-mapping>  
  27.         <filter-name>struts</filter-name>  
  28.         <url-pattern>/*</url-pattern>  
  29.     </filter-mapping>  
  30.   
  31.     <welcome-file-list>  
  32.         <welcome-file>index.jsp</welcome-file>  
  33.     </welcome-file-list>  
  34. </web-app>  

 

 

可以布署,運行了,輸入http://localhost:8080/struts2_guice/hello 就可以看到結果了。

 

如果你覺得Annotation太麻煩,或不喜歡,也可以嘗試自己實現Guice的Module,以下是一個簡單的實現:

MyModule.java

Java代碼
  1. package com.leo.module;  
  2.   
  3. import com.google.inject.Binder;  
  4. import com.google.inject.Module;  
  5. import com.google.inject.Scopes;  
  6. import com.leo.service.IHelloService;  
  7. import com.leo.service.impl.HelloServiceImpl;  
  8.   
  9. /* 
  10.  * 如果你覺得Annotation有種支離破碎的感覺,別急,Guice還為你提供一種統一 
  11.  * 注入管理的自定義實現。在本例中,先前的IHelloService, HelloServiceImpl 
  12.  * 你現在可以完全將所有的Annotation去掉,然后實現Module接口的唯一一個方法 
  13.  * 實現如下 
  14.  */  
  15. public class MyModule implements Module {  
  16.   
  17.     public void configure(Binder binder) {  
  18.   
  19.         /* 
  20.          * 將接口IHelloService 與其實現HelloServiceImpl 綁定在一起 并且作用域為Scopes.SINGLETON 
  21.          * 在這里有多種配置方法,但因為是入門實例,不想說的太復雜。其中如果不配置作用域,默認就是類似于Spring 
  22.          * 的Scope="prototype" 
  23.          */  
  24.         binder.bind(IHelloService.class).to(HelloServiceImpl.class).in(  
  25.                 Scopes.SINGLETON);  
  26.     }  
  27.   
  28. }  

 

 

運行效果完全一模一樣,因此團隊開發如果統一風格的話Guice確實能快速不少。但目前Guice僅僅只是一個IoC,遠遠沒有Spring所涉及的那么廣,但又正如Rod Johnson反復在其《J2EE without EJB》里強調:架構要永遠 simplest, simplest 再 simplest,因此你覺得夠用,就是最好的。

 

總的來說,開發,運行的速度似乎又快了不少,但Guice真的能不能扛起其所說的下一代IoC容器,我們拭目以待吧。




shiwf 2010-01-06 16:16 發表評論
]]>
主站蜘蛛池模板: 桓台县| 绍兴市| 大新县| 墨玉县| 双峰县| 射阳县| 盐津县| 镇远县| 莎车县| 海林市| 瑞安市| 广丰县| 砀山县| 松阳县| 鄂托克旗| 沈丘县| 高安市| 广灵县| 察哈| 措勤县| 惠州市| 克东县| 台中市| 安乡县| 兴城市| 兴业县| 大田县| 仁布县| 长岭县| 章丘市| 曲松县| 南和县| 城口县| 德令哈市| 九寨沟县| 扎囊县| 桑日县| 嘉黎县| 兴仁县| 红原县| 郸城县|