閮ㄥ垎錛?錛夛細GUICE 涓?Servlet 闆嗘垚
http://code.google.com/p/google-guice/wiki/Servlets
Guice 鎻愪緵浜嗕笌 Servlet 鐨勯泦鎴愶紝鍙互瀹屽叏鏇夸唬 web.xml錛屼嬌鐢ㄧ被鍨嬪畨鍏紝Java紼嬪簭鍛樻墍涔犳儻鐨勬柟寮忓 servlet 鍜?filter 榪涜閰嶇疆銆?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>
棣栧厛錛屼笅杞芥渶鏂扮増鏈殑 guice-servlet jar 鍖咃紝灝嗗叾涓?guice 鐨勫寘鍏卞悓鏀懼湪 classpath 璺緞涓嬨傜劧鍚庡皢 GuiceFilter 鏀懼湪 web.xml 鐨勮搗濮嬩綅緗紝榪欐牱錛屽浜庝換浣曡礬寰勶紝guiceFilter 閮戒細琚嬌鐢紝寰楀埌鎺у埗銆?br />
涓嬮潰錛岃瀵?Guice 榪涜閰嶇疆錛屾柟娉曟槸錛?nbsp; Guice.createInjector(newServletModule());
榪欏彞璇濆彲浠ュ湪浠諱綍鏃跺欒璋冪敤錛屼絾鎺ㄨ崘鐨勫仛娉曟槸鍦ㄤ竴涓?ServletContextListener 涓璋冪敤錛岃繖涓?Listener 鍦?Web 搴旂敤鍚姩鏃惰璋冪敤錛屽湪浠諱綍璇鋒眰鍒版潵涔嬪墠銆傚仛娉曞涓嬶紝棣栧厛鎵╁睍 Google 鎻愪緵鐨勫熀綾伙細
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 涓繘琛岀殑銆備互涓嬫槸涓涓尶鍚嶇被鐨勪緥瀛愶細
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);
娉ㄦ剰錛氭瘡涓?servlet 鍜?filter 閮藉繀欏繪槸 @Singleton錛屽繀欏誨湪鏍囨敞鎴栬?module 閲岄潰鎸囨槑錛屾墍鏈変笉鏄?Singleton 鐨?scope 閮芥槸閿欒銆傝繖涓?Servlet 鐨勮鑼冧竴鑷淬?br />
瀹夎浜?ServletModule 妯″潡鍚庯紝浠諱綍 Guice 娉ㄥ叆鐨勫疄渚嬮兘鍙互琚敞鍏ヤ笅闈㈢殑瀵硅薄錛?br />
@RequestScoped
class SomeNonServletPojo {
@Inject
public SomeNonServletPojo(HttpServletRequest request, HttpServletResponse response, HttpSession session) {

}
}
姝ゅ錛宧ttp璇鋒眰鐨勫弬鏁板彲浠ュ姝ゆ敞鍏ワ細 @Inject @RequestParameters Map<String, String[]> params;
鍒嗛厤欏哄簭錛?br />
filter 鍜?servlet 灝嗕細鎸夌収鍦?nbsp;ServletModule 閲岄潰鍑虹幇鐨勯『搴忓垎閰嶃?br />
姝ゅ錛屽彲浠ヤ竴嬈″尮閰嶅涓礬寰勶細 serve("*.html", "/my/*").with(MyServlet.class);
姝e垯鏂囨硶鍖歸厤錛歴erveRegex("(.)*ajax(.)*").with(MyAjaxServlet.class)
鎸囧畾鍒濆鍖栧弬鏁幫細
Map<String, String> params = new HashMap<String, String>();
params.put("coffee", "Espresso");
params.put("site", "google.com");


serve("/*").with(MyServlet.class, params)
榪欎簺鍙傛暟鍙互鐢?getInitParams 寰楀埌銆?br />
鍏朵粬楂樼駭鐗規х渷鐣ヤ笉鍐嶈璁恒?br />
閮ㄥ垎錛?錛夛細GUICE 涓?nbsp;Vaadin 闆嗘垚
http://vaadin.com/wiki/-/wiki/Main/Integrating Vaadin with Guice 2.0
棣栧厛錛屼笅杞?guice-2.0.jar, guice-servlet-2.0.jar, aopalliance.jar錛屽皢榪欎簺鍖呮嫹璐濆埌 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();
}
}
涓婇潰榪欎釜class 鍙互鐩存帴鎷瘋礉鍒伴」鐩腑涓嶉渶瑕佹敼鍙樸侾rovider 鐢?Guice 娉ㄥ叆錛屼負姣忎釜鐢ㄦ埛鍒涘緩涓涓?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鏉ラ厤緗?ServletModule錛岄厤緗?Servlet錛孉pplication錛屼互鍙婂叾浠?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 鍜屼笂闈㈢殑閭d釜閰嶇疆妯″潡 (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>

]]>