部分Q?Q:(x)GUICE ?Servlet 集成
http://code.google.com/p/google-guice/wiki/Servlets
Guice 提供了与 Servlet 的集成,可以完全替代 web.xmlQ用类型安全,JavaE序员所?fn)惯的方式?servlet ?filter q行配置?a name="Introduction">
<filter>
<filter-name>guiceFilter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>guiceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
首先Q下载最新版本的 guice-servlet jar 包,其?guice 的包共同攑֜ classpath 路径下。然后将 GuiceFilter 攑֜ web.xml 的v始位|,q样Q对于Q何\径,guiceFilter 都会(x)被用,得到控制?br />
下面Q要?Guice q行配置Q方法是Q?nbsp; Guice.createInjector(newServletModule());
q句话可以在M时候被调用Q但推荐的做法是在一?ServletContextListener 中被调用Q这?Listener ?Web 应用启动时被调用Q在Mh到来之前。做法如下,首先扩展 Google 提供的基c:(x)
public class MyGuiceServletConfig extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
return Guice.createInjector(new ServletModule());
}
}
然后?web.xml 中进行注?br />
<listener>
<listener-class>com.example.MyGuiceServletConfig</listener-class>
</listener>
配置 servlet 是在 ServletModule 中进行的。以下是一个匿名类的例子:(x)
Guice.createInjector(
, new ServletModule() {
@Override
protected void configureServlets() {
serve("*.html").with(MyServlet.class)
}
}
可以?web.xml风格的\径限制法 serve("/my/*").with(MyServlet.class)?br />
也可以类似地配置 filter:
filter("/*").through(MyFilter.class);
注意Q每?servlet ?filter 都必L @SingletonQ必d标注或?module 里面指明Q所有不?Singleton ?scope 都是错误。这?Servlet 的规范一致?br />
安装?ServletModule 模块后,M Guice 注入的实例都可以被注入下面的对象Q?br />
@RequestScoped
class SomeNonServletPojo {
@Inject
public SomeNonServletPojo(HttpServletRequest request, HttpServletResponse response, HttpSession session) {

}
}
此外Qhttph的参数可以如此注入:(x) @Inject @RequestParameters Map<String, String[]> params;
分配序Q?br />
filter ?servlet 会(x)按照?nbsp;ServletModule 里面出现的顺序分配?br />
此外Q可以一ơ匹配多个\径:(x) serve("*.html", "/my/*").with(MyServlet.class);
正则文法匚wQserveRegex("(.)*ajax(.)*").with(MyAjaxServlet.class)
指定初始化参敎ͼ(x)
Map<String, String> params = new HashMap<String, String>();
params.put("coffee", "Espresso");
params.put("site", "google.com");


serve("/*").with(MyServlet.class, params)
q些参数可以?getInitParams 得到?br />
其他高Ҏ(gu)省略不再讨论?br />
部分Q?Q:(x)GUICE ?nbsp;Vaadin 集成
http://vaadin.com/wiki/-/wiki/Main/Integrating Vaadin with Guice 2.0
首先Q下?guice-2.0.jar, guice-servlet-2.0.jar, aopalliance.jarQ将q些包拷贝到 WebContent/WEB-INF/lib/ 目录?br />
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.AbstractApplicationServlet;
@Singleton
public class GuiceApplicationServlet extends AbstractApplicationServlet {
protected final Provider<Application> applicationProvider;
@Inject
public GuiceApplicationServlet(Provider<Application> applicationProvider) {
this.applicationProvider = applicationProvider;
}
@Override
protected Class getApplicationClass() throws ClassNotFoundException {
return Application.class;
}
@Override
protected Application getNewApplication(HttpServletRequest request) throws ServletException {
return applicationProvider.get();
}
}
上面q个class 可以直接拯到项目中不需要改变。Provider ?Guice 注入Qؓ(f)每个用户创徏一?Application 实例?br />
下面是一个简单的 Application 的例子。其中参?text 是被注入的?br />
#!java
import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.vaadin.Application;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;
public class MyApplication extends Application {
@Inject @Named("welcome") protected String text;
@Override
public void init() {
Window window = new Window();
window.addComponent(new Label(text));
setMainWindow(window);
}
}
初始化,需要写一?Guice ?GuiceServletContextListener来配|?ServletModuleQ配|?ServletQApplicationQ以?qing)其?Guice 理的实例,比如上面例子用到?text?br />
#!java
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.name.Names;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule;
import com.google.inject.servlet.ServletScopes;
import com.vaadin.Application;
public class MyServletConfig extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
ServletModule module = new ServletModule() {
@Override
protected void configureServlets() {
serve("/*").with(GuiceApplicationServlet.class);
bind(Application.class).to(MyApplication.class).in(ServletScopes.SESSION);
bindConstant().annotatedWith(Names.named("welcome")).to("This is my first Vaadin/Guice Application");
}
};
Injector injector = Guice.createInjector(module);
return injector;
}
}
web.xml 需要如此配|,来启?Guice Filter 和上面的那个配置模块 (Listener)
<web-app>

<filter>
<filter-name>guiceFilter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>guiceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>de.timedout.vaadin.guice.MyServletConfig</listener-class>
</listener>
</web-app>

]]>