struts2.2 与 guice3.0 整合

最近看到 Guice 出3.0,以前看过一点guice ,对guice 的感觉还不错。之前一直都在搞SSH,有一点腻。所以想换换口味。网上关于Spring与Struts 整合的例子铺天盖地,可是 Guice与Struts 整合的例子少的可怜。找了好长时间,还是没找到满意的答案,只能靠自己了,自已来吧。
因为之前做过guice2.0与struts2整合的小例子,所以觉得guice3.0的整合方法应该差不多。想着只要在 struts.xml 里面加上
<constant name="struts.objectFactory" value="guice" />
,经过一翻尝试之后,结果这种方法是错误的。guice3.0变化比较大, 参考 guice官网 [url]http://code.google.com/p/google-guice/wiki/Struts2Integration[/url] 的教程。

依赖:如果是maven只需要加下面的依赖

<!--struts-->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${org.apache.struts.version}</version>
</dependency>

<!--guice-->
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
</dependency>
<!--guice-struts-plugin-->
<dependency>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-struts2</artifactId>
<version>3.0</version>
</dependency>


如果不是maven则首先是struts2的jar包和guice的jar包

guice-3.0.jar
guice-struts2-plugin-3.0.jar
guice-servlet-3.0.jar
aopalliance.jar
javax.inject.jar


然后是 配置web.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">

<listener>
<listener-class>com.example.MyGuiceServletContextListener</listener-class>
</listener>
<filter>
<filter-name>guice</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>

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

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>


struts.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>
<constant name="struts.devMode" value="true"/>

<package name="struts" extends="struts-default">
<action name="test" class="com.example.TestAction">
<result name="success">/test.html</result>
</action>
</package>
</struts>


实现自己的 GuiceServetContextListener

package com.exmaple;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Singleton;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule;
import com.google.inject.struts2.Struts2GuicePluginModule;
import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter;


/**
*
* @author Basten Gao
*/
public class MyGuiceServletContextListener extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
return Guice.createInjector(
new Struts2GuicePluginModule(),
new ServletModule() {
@Override
protected void configureServlets() {
// Struts 2 setup
bind(StrutsPrepareAndExecuteFilter.class).in(Singleton.class);
filter("/*").through(StrutsPrepareAndExecuteFilter.class);
//注册自己的Module,对Guice了解的都懂得
install(new MyModule());
}
});

}
}



配置到此结束。

接下来就可以利用Guice进行注入了。



import javax.inject.Inject;

/**
*
* @author Basten Gao
*/
public class TestAction {

@Inject
private TestService testService;

public String execute() throws Exception {
testService.doSomeThing();
return Action.SUCCESS;
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Acai 是 JUnit4 和 Guice 的测试库,可以更容易的编写应用功能测试。主要特性:注入测试需要的助手类启动测试需要的任意的服务 运行测试之间的服务清理按照正确顺序启动多个服务 创建测试作用域绑定Acai 主要针对的是应用大型功能测试。安装<dependency>   <groupId>com.google.acai</groupId>   <artifactId>acai</artifactId>   <version>0.1</version>   <scope>test</scope> </dependency>使用 Acai 进行测试注入@RunWith(JUnit4.class) public class SimpleTest {   @Rule public Acai acai = new Acai(MyTestModule.class);   @Inject private MyClass foo;   @Test   public void checkSomethingWorks() {     // Use the injected value of foo here   }   private static class MyTestModule extends AbstractModule {     @Override protected void configure() {       bind(MyClass.class).to(MyClassImpl.class);     }   } }使用 Acai 启动服务@RunWith(JUnit4.class) public class ExampleFunctionalTest {   @Rule public Acai acai = new Acai(MyTestModule.class);   @Inject private MyServerClient serverClient;   @Test   public void checkSomethingWorks() {     // Call the running server and test some behaviour here.     // Any state will be cleared by MyFakeDatabaseWiper after each     // test case.   }   private static class MyTestModule extends AbstractModule {     @Override protected void configure() {       // Normal Guice modules which configure your       // server with in-memory versions of backends.       install(MyServerModule());       install(MyFakeDatabaseModule());       install(new TestingServiceModule() {         @Override protected void configureTestingServices() {           bindTestingService(MyServerRunner.class);           bindTestingService(MyFakeDatabaseWiper.class);         }       });     }   }   private static class MyServerRunner implements TestingService {     @Inject private MyServer myServer;     @BeforeSuite void startServer() {       myServer.start().awaitStarted();     }   }   private static class MyFakeDatabaseWiper implements TestingService {     @Inject private MyFakeDatabse myFakeDatabase;     @AfterTest void wipeDatabase() {       myFakeDatabase.wipe();     }   } } 标签:Acai
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值