`
bastengao
  • 浏览: 149370 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

struts2.2 与 guice3.0 整合

    博客分类:
  • java
阅读更多
最近看到 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官网 http://code.google.com/p/google-guice/wiki/Struts2Integration 的教程。

依赖:如果是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;
    }

}
分享到:
评论
2 楼 tian583391571 2013-06-26  
成功集成了guice容器,多谢!
1 楼 yingzhor 2011-05-05  
http://topic.csdn.net/u/20110504/21/ccfd924d-79b7-4ce8-b438-707cc073ef09.html?seed=804993049&r=73132619#r_73132619

相关推荐

Global site tag (gtag.js) - Google Analytics