seasun  
在不断模仿、思考、总结中一步一步进步!
公告
  •     我的blog中的部分资源是来自于网络上,如果您认为侵犯了您的权利,请及时联系我我会尽快删除!E-MAIL:shiwenfeng@aliyun.com和QQ:281340916,欢迎交流。

日历
<2010年1月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

常用链接

随笔分类

good blog author

积分与排名

  • 积分 - 80681
  • 排名 - 698

最新评论

阅读排行榜

 

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容器,我们拭目以待吧。


posted on 2010-01-06 16:16 shiwf 阅读(647) 评论(0)  编辑  收藏 所属分类: 1.15 guice

只有注册用户登录后才能发表评论。


网站导航:
 
 
Copyright © shiwf Powered by: 博客园 模板提供:沪江博客