Board logo

标题: [原创] Struts2与Guice整合 [打印本页]

作者: 风灵风之子    时间: 2008-5-18 20:07     标题: Struts2与Guice整合

Guice是一个IoC的框架,传说中它的速度比Spring100倍,很欣赏它无需配置只需注解就可以使用。
Struts2Guice整合需要Guice的插件guice-struts2-plugin-1.0.1.jar以及guice相关的jar包。下面是整个项目所需要的jar
[attach]23942[/attach]


第一步,配置在web.xmlStruts2以及Guicefilter


<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>




第二步,建立一个Service的接口:MyService

import com.google.inject.ImplementedBy;

import com.hopeteam.service.impl.MyServiceImpl;

/*


*
采用annotation进行接口与实现类之间的绑定


*
接口与实现类之间绑定是必须的


*


*/

@ImplementedBy(MyServiceImpl.class)

public
interface MyService {


boolean valid(String username,String password);

}


第三步,创建Service的实现类:MyServiceImpl

import com.google.inject.Singleton;

import com.hopeteam.service.MyService;

/*

* 我们用@Singleton来标注它


*

*/

@Singleton

public
class MyServiceImpl implements MyService{


public
boolean valid(String username,String password)


{


if(username.equals("fengzhizi")


&& password.equals(
"123456"))


return
true;


else


return
false;


}

}


Guice通过Module来配置而不同于Spring框架通过xml进行配置。Guice通过绑定器Binder来传递我们的模块。一个绑定通常包含一个从接口到具体实现的映射

第四步,创建Module:MyModule

import com.google.inject.Binder;

import com.google.inject.Module;

import com.google.inject.Scopes;

import com.hopeteam.service.MyService;

import com.hopeteam.service.impl.MyServiceImpl;

public
class MyModule implements Module {


public
void configure(Binder binder) {


/*



*
将接口MyService 与其实现MyServiceImpl 绑定在一起
并且作用域为Scopes.SINGLETON



*
其中如果不配置作用域,默认就是类似于SpringScope="prototype"



*/


binder.bind(MyService.
class).to(MyServiceImpl.class).in(


Scopes.
SINGLETON);


}

}

第五步,创建Action:LoginAction

import com.google.inject.Inject;

import com.hopeteam.service.MyService;

import com.opensymphony.xwork2.Action;

public
class LoginAction implements Action{


private String username;


private String password;



private String tip;



/*



*
通过field字段进行注入



*/


@Inject


private MyService ms;


public String getUsername() {


return
username;


}


public
void setUsername(String username) {


this.username = username;


}


public String getPassword() {


return
password;


}


public
void setPassword(String password) {


this.password = password;


}


public String getTip() {


return
tip;


}


public
void setTip(String tip) {


this.tip = tip;


}



public String execute() throws Exception


{


if(ms.valid(getUsername(),getPassword()))


{


setTip(
"GuiceStruts2整合成功!");


return
SUCCESS;


}


else


return
ERROR;


}

}

配置struts的配置文件:struts.xml

<struts>


<constant name="struts.objectFactory" value="guice" />



<package name="fengzhizi" extends="struts-default">


<action name="login" class="com.hopeteam.action.LoginAction">


<result name="error">/error.jsp</result>


<result name="success">/welcome.jsp</result>


</action>


</package>

</struts>
其中,<constant name="struts.objectFactory" value="guice" />是将Struts2的对象进行注入,包括动作和拦截器。

好了大功告成,我们看一下登陆页面吧
[attach]23943[/attach]

登陆后的页面:
[attach]23944[/attach]

作者: wwwwwrqq    时间: 2008-5-18 22:39

不错 传说中它的速度比Spring快100倍
作者: starlight    时间: 2008-5-19 09:04

LS的,现在数字这些都是噱头,关键是要和项目整合得当。
作者: 风灵风之子    时间: 2008-5-19 22:22

Guice目前可能只是一个玩具只提供了IoC,没有提供事务处理等等,但是它的思想不错,说不定可以成为下一代IoC容器。

目前已经有了一个基于guice的框架:warp
它的warp-persist提供了事务处理,并封装了Hibernate
我们可以看到流行的Struts+Spring+Hibernate完全可以由Struts+warp+Hibernate替代

由此 看好Guice 看好warp
作者: starlight    时间: 2008-5-19 22:45

拭目以待吧




欢迎光临 黑色海岸线论坛 (http://bbs.thysea.com/) Powered by Discuz! 7.2