`
CrazzyLee
  • 浏览: 26196 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

struts2和Guice整合

    博客分类:
  • Java
阅读更多
轻量级的东东。。做个Demo看看。
先建立一个service:

IHelloService.java
Java代码

  
 package com.leo.service;  
      
    import com.google.inject.ImplementedBy;  
    import com.leo.service.impl.HelloServiceImpl;  
      
    /*
    * 采用annotation进行接口与实现类之间的绑定
    * 注意:接口与实现类之间绑定是必须的,如果只是单独一个类,没有接口,
    * 那么Guice会隐式的自动帮你注入。并且接口此是不应该声明注入域范围的,
    * 应该在其实现地方声明
    *
    */  
   @ImplementedBy(HelloServiceImpl.class)  
   public interface IHelloService {  
       public String sayHello(String str);  
   }  

package com.leo.service;  

import com.google.inject.ImplementedBy; 
import com.leo.service.impl.HelloServiceImpl;  
/*  
 * 采用annotation进行接口与实现类之间的绑定  
 * 注意:接口与实现类之间绑定是必须的,如果只是单独一个类,没有接口,  
 * 那么Guice会隐式的自动帮你注入。并且接口此是不应该声明注入域范围的,  
 * 应该在其实现地方声明  
*  */ 
@ImplementedBy(HelloServiceImpl.class) 
public interface IHelloService 
{  
  public String sayHello(String str); 
}


再来一个简单的实现:

HelloServiceImpl.java
Java代码

   
package com.leo.service.impl;  
     
    import com.google.inject.Singleton;  
    import com.leo.service.IHelloService;  
      
    /*
    * 这里如果默认不用annotation标注其作用域,或在自定义的module也不指定的话
    * 默认的创建对象的方式是类似于Spring的prototype,在此处因为仅仅是一个stateless service
    * 我们用@Singleton来标注它,更多的作用域可看Guice文档
   *
   * 注意:与标注@Singleton等效的工作也可以在自定义的module里来实现
   */  
     
   @Singleton  
   public class HelloServiceImpl implements IHelloService {  
     
       public String sayHello(String str) {  
           return new StringBuilder("Hello " + str + " !").toString();  
        }  
     
   }  


package com.leo.service.impl;  
import com.google.inject.Singleton; 
import com.leo.service.IHelloService;  
/*  
 * 这里如果默认不用annotation标注其作用域,或在自定义的module也不指定的话  
 * 默认的创建对象的方式是类似于Spring的prototype,在此处因为仅仅是一个stateless service  
 * 我们用@Singleton来标注它,更多的作用域可看Guice文档  
 *   
 * 注意:与标注@Singleton等效的工作也可以在自定义的module里来实现  
 */  
@Singleton 
public class HelloServiceImpl implements IHelloService {   
    public String sayHello(String str) 
      {    
         return new StringBuilder("Hello " + str + " !").toString();  
      }  
}


Struts2的配置相信大家都会了,这里需要注意的是Struts2的工厂已经变了,默认是Spring现在我们要改成Guice,请看:

struts.properties
Java代码

  
struts.objectFactory = guice  
   #如果自已想实现Module接口,则下面注释请去掉  
   #guice.module=com.leo.module.MyModule  
   struts.action.extension=  


   struts.objectFactory = guice #如果自已想实现Module接口,则下面注释请去掉 #guice.module=com.leo.module.MyModule struts.action.extension=

再来看看调用代码,稍微比Spring简洁了些:

HelloAction.java
Java代码

  
package com.leo.action;  
      
    import com.google.inject.Inject;  
    import com.leo.service.IHelloService;  
    import com.opensymphony.xwork2.ActionSupport;  
      
    public class HelloAction extends ActionSupport {  
      
        private static final long serialVersionUID = -338076402728419581L;  
     
       /*
         * 通过field字段进行注入,除此之外,还有construct, method注入均可
         */  
      @Inject  
       private IHelloService helloService;  
     
       private String message;  
    
       public String getMessage() {  
           return message;  
        }  
     
       public void setMessage(String message) {  
           this.message = message;  
        }  
     
       public String execute() {  
            message = helloService.sayHello("leo");  
           return SUCCESS;  
        }  
     
   }

 

package com.leo.action;  
import com.google.inject.Inject; 
import com.leo.service.IHelloService; 
import com.opensymphony.xwork2.ActionSupport;  
public class HelloAction extends ActionSupport {   
    private static final long serialVersionUID = -338076402728419581L;   
  /*   
   * 通过field字段进行注入,除此之外,还有construct, method注入均可 */          
      @Inject  private IHelloService helloService;   
      
      private String message;   
      public String getMessage() 
       {   
          return message;  
       }   
      public void setMessage(String message) 
       {   
          this.message = message;  
       }   
       public String execute() 
        {   
           message = helloService.sayHello("leo");   return SUCCESS;  }  
        }
}

struts.xml配置也是非常简单:

struts.xml
Xml代码

  
<?xml version="1.0" encoding="UTF-8" ?>  
   <!DOCTYPE struts PUBLIC  
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
        "http://struts.apache.org/dtds/struts-2.0.dtd">  
    
    <struts>  
        <package name="default" extends="struts-default">  
            <action name="hello" class="com.leo.action.HelloAction">  
                <result>index.jsp</result>  
           </action>  
       </package>  
   </struts>  


<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE struts PUBLIC     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"     "http://struts.apache.org/dtds/struts-2.0.dtd">  
<struts>  
     <package name="default" extends="struts-default">   
         <action name="hello"class="com.leo.action.HelloAction">                 <result>index.jsp</result>   
         </action>  
     </package> 
</struts>

到这里,算是大功告成了,Guice文档在与Struts2整合部分例子有误,而且郁闷的是,竟然连Guice的Filter需要在web.xml配置都没有说,我把配好的web.xml弄出来给大家看看

web.xml
Xml代码

  
 <?xml version="1.0" encoding="UTF-8"?>  
    <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
      
        <filter>  
            <filter-name>guice</filter-name>  
            <filter-class>  
                com.google.inject.servlet.GuiceFilter  
           </filter-class>  
       </filter>  
     
       <filter>  
           <filter-name>struts</filter-name>  
           <filter-class>  
                org.apache.struts2.dispatcher.FilterDispatcher  
          </filter-class>  
       </filter>  
     
       <filter-mapping>  
           <filter-name>guice</filter-name>  
           <url-pattern>/*</url-pattern>  
       </filter-mapping>  
     
       <filter-mapping>  
           <filter-name>struts</filter-name>  
           <url-pattern>/*</url-pattern>  
       </filter-mapping>  
     
       <welcome-file-list>  
           <welcome-file>index.jsp</welcome-file>  
       </welcome-file-list>  
   </web-app>  

<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">   

<filter>   
     <filter-name>guice</filter-name>   
     <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>   
<filter>   
     <filter-name>struts</filter-name>   
     <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
</filter>   

<filter-mapping>   
     <filter-name>guice</filter-name>   
     <url-pattern>/*</url-pattern>  
</filter-mapping>   

<filter-mapping>   
     <filter-name>struts</filter-name>   
     <url-pattern>/*</url-pattern>  
</filter-mapping>   <welcome-file-list>   
<welcome-file>index.jsp</welcome-file> 

</welcome-file-list> 
</web-app>

可以布署,运行了,输入http://localhost:8080/struts2_guice/hello 就可以看到结果了。

如果你觉得Annotation太麻烦,或不喜欢,也可以尝试自己实现Guice的Module,以下是一个简单的实现:

MyModule.java
Java代码

  
package com.leo.module;  
      
    import com.google.inject.Binder;  
    import com.google.inject.Module;  
    import com.google.inject.Scopes;  
    import com.leo.service.IHelloService;  
    import com.leo.service.impl.HelloServiceImpl;  
      
    /*
   * 如果你觉得Annotation有种支离破碎的感觉,别急,Guice还为你提供一种统一
   * 注入管理的自定义实现。在本例中,先前的IHelloService, HelloServiceImpl
   * 你现在可以完全将所有的Annotation去掉,然后实现Module接口的唯一一个方法
   * 实现如下
   */  
   public class MyModule implements Module {  
    
       public void configure(Binder binder) {  
    
           /*
             * 将接口IHelloService 与其实现HelloServiceImpl 绑定在一起 并且作用域为Scopes.SINGLETON
             * 在这里有多种配置方法,但因为是入门实例,不想说的太复杂。其中如果不配置作用域,默认就是类似于Spring
             * 的Scope="prototype"
             */  
            binder.bind(IHelloService.class).to(HelloServiceImpl.class).in(  
                    Scopes.SINGLETON);  
        }  
     
   }  


package com.leo.module;  
import com.google.inject.Binder; 
import com.google.inject.Module; 
import com.google.inject.Scopes; 
import com.leo.service.IHelloService; 
import com.leo.service.impl.HelloServiceImpl;  
/*  
* 如果你觉得Annotation有种支离破碎的感觉,别急,Guice还为你提供一种统一  
* 注入管理的自定义实现。在本例中,先前的IHelloService, HelloServiceImpl  
* 你现在可以完全将所有的Annotation去掉,然后实现Module接口的唯一一个方法  * 实现如下  
*/ 
public class MyModule implements Module {   
      public void configure(Binder binder) {    
          /*   
           * 将接口IHelloService 与其实现HelloServiceImpl 绑定在一起 并且作用域为Scopes.SINGLETON    
           * 在这里有多种配置方法,但因为是入门实例,不想说的太复杂。其中如果不配置作用域,默认就是类似于Spring    * 的Scope="prototype"    
           */                                                                                        binder.bind(IHelloService.class).to(HelloServiceImpl.class).in(       Scopes.SINGLETON);  
     }  
}


运 行效果完全一模一样,因此团队开发如果统一风格的话Guice确实能快速不少。但目前Guice仅仅只是一个IoC,远远没有Spring所涉及的那么 广,但又正如Rod Johnson反复在其《J2EE without EJB》里强调:架构要永远 simplest, simplest 再 simplest,因此你觉得够用,就是最好的。
分享到:
评论
3 楼 bastengao 2011-05-04  
期待 struts2.2 与 guice3.0整合
2 楼 CrazzyLee 2011-02-25  
heavenslv 写道
这个是总结的还是收藏的?不知道Guice是干什么的。有时间再了解下。先把项目搞定先~


看文章就是专门搞依赖注入的啊,跟struts2整合,很简单.
配置web.xml和struts.xml,通过@的一些annotations就可以注入了
有@inject  @EmplementedBy @Singleton...例子上是,不知道深层有多难,能用就好了- - 
1 楼 heavenslv 2011-02-25  
这个是总结的还是收藏的?不知道Guice是干什么的。有时间再了解下。先把项目搞定先~

相关推荐

Global site tag (gtag.js) - Google Analytics