GUICE 與 Servlet 以及 Vaadin 的集成
部分(1):GUICE 與 Servlet 集成
http://code.google.com/p/google-guice/wiki/Servlets
Guice 提供了與 Servlet 的集成,可以完全替代 web.xml,使用類型安全,Java程序員所習(xí)慣的方式對(duì) servlet 和 filter 進(jìn)行配置。
首先,下載最新版本的 guice-servlet jar 包,將其與 guice 的包共同放在 classpath 路徑下。然后將 GuiceFilter 放在 web.xml 的起始位置,這樣,對(duì)于任何路徑,guiceFilter 都會(huì)被使用,得到控制。
下面,要對(duì) Guice 進(jìn)行配置,方法是: Guice.createInjector(newServletModule());
這句話可以在任何時(shí)候被調(diào)用,但推薦的做法是在一個(gè) ServletContextListener 中被調(diào)用,這個(gè) Listener 在 Web 應(yīng)用啟動(dòng)時(shí)被調(diào)用,在任何請(qǐng)求到來之前。做法如下,首先擴(kuò)展 Google 提供的基類:
然后在 web.xml 中進(jìn)行注冊(cè)
配置 servlet 是在 ServletModule 中進(jìn)行的。以下是一個(gè)匿名類的例子:
可以用 web.xml風(fēng)格的路徑限制法 serve("/my/*").with(MyServlet.class)。
也可以類似地配置 filter:
filter("/*").through(MyFilter.class);
注意:每個(gè) servlet 和 filter 都必須是 @Singleton,必須在標(biāo)注或者 module 里面指明,所有不是 Singleton 的 scope 都是錯(cuò)誤。這與 Servlet 的規(guī)范一致。
安裝了 ServletModule 模塊后,任何 Guice 注入的實(shí)例都可以被注入下面的對(duì)象:
此外,http請(qǐng)求的參數(shù)可以如此注入: @Inject @RequestParameters Map<String, String[]> params;
分配順序:
filter 和 servlet 將會(huì)按照在 ServletModule 里面出現(xiàn)的順序分配。
此外,可以一次匹配多個(gè)路徑: serve("*.html", "/my/*").with(MyServlet.class);
正則文法匹配:serveRegex("(.)*ajax(.)*").with(MyAjaxServlet.class)
指定初始化參數(shù):
Map<String, String> params = new HashMap<String, String>();
params.put("coffee", "Espresso");
params.put("site", "google.com");


serve("/*").with(MyServlet.class, params)
這些參數(shù)可以用 getInitParams 得到。
其他高級(jí)特性省略不再討論。
部分(2):GUICE 與 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/ 目錄。
上面這個(gè)class 可以直接拷貝到項(xiàng)目中不需要改變。Provider 由 Guice 注入,為每個(gè)用戶創(chuàng)建一個(gè) Application 實(shí)例。
下面是一個(gè)簡(jiǎn)單的 Application 的例子。其中參數(shù) text 是被注入的。
初始化,需要寫一個(gè) Guice 的 GuiceServletContextListener來配置 ServletModule,配置 Servlet,Application,以及其他 Guice 管理的實(shí)例,比如上面例子用到的 text。
web.xml 需要如此配置,來啟動(dòng) Guice Filter 和上面的那個(gè)配置模塊 (Listener)
http://code.google.com/p/google-guice/wiki/Servlets
Guice 提供了與 Servlet 的集成,可以完全替代 web.xml,使用類型安全,Java程序員所習(xí)慣的方式對(duì) servlet 和 filter 進(jìn)行配置。
<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>
<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 的起始位置,這樣,對(duì)于任何路徑,guiceFilter 都會(huì)被使用,得到控制。
下面,要對(duì) Guice 進(jìn)行配置,方法是: Guice.createInjector(newServletModule());
這句話可以在任何時(shí)候被調(diào)用,但推薦的做法是在一個(gè) ServletContextListener 中被調(diào)用,這個(gè) Listener 在 Web 應(yīng)用啟動(dòng)時(shí)被調(diào)用,在任何請(qǐng)求到來之前。做法如下,首先擴(kuò)展 Google 提供的基類:
public class MyGuiceServletConfig extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
return Guice.createInjector(new ServletModule());
}
}
@Override
protected Injector getInjector() {
return Guice.createInjector(new ServletModule());
}
}
然后在 web.xml 中進(jìn)行注冊(cè)
<listener>
<listener-class>com.example.MyGuiceServletConfig</listener-class>
</listener>
<listener-class>com.example.MyGuiceServletConfig</listener-class>
</listener>
配置 servlet 是在 ServletModule 中進(jìn)行的。以下是一個(gè)匿名類的例子:
Guice.createInjector(
, new ServletModule() {
@Override
protected void configureServlets() {
serve("*.html").with(MyServlet.class)
}
}

@Override
protected void configureServlets() {
serve("*.html").with(MyServlet.class)
}
}
可以用 web.xml風(fēng)格的路徑限制法 serve("/my/*").with(MyServlet.class)。
也可以類似地配置 filter:
filter("/*").through(MyFilter.class);
注意:每個(gè) servlet 和 filter 都必須是 @Singleton,必須在標(biāo)注或者 module 里面指明,所有不是 Singleton 的 scope 都是錯(cuò)誤。這與 Servlet 的規(guī)范一致。
安裝了 ServletModule 模塊后,任何 Guice 注入的實(shí)例都可以被注入下面的對(duì)象:
@RequestScoped
class SomeNonServletPojo {
@Inject
public SomeNonServletPojo(HttpServletRequest request, HttpServletResponse response, HttpSession session) {

}
}
class SomeNonServletPojo {
@Inject
public SomeNonServletPojo(HttpServletRequest request, HttpServletResponse response, HttpSession session) {

}
}
此外,http請(qǐng)求的參數(shù)可以如此注入: @Inject @RequestParameters Map<String, String[]> params;
分配順序:
filter 和 servlet 將會(huì)按照在 ServletModule 里面出現(xiàn)的順序分配。
此外,可以一次匹配多個(gè)路徑: serve("*.html", "/my/*").with(MyServlet.class);
正則文法匹配:serveRegex("(.)*ajax(.)*").with(MyAjaxServlet.class)
指定初始化參數(shù):







這些參數(shù)可以用 getInitParams 得到。
其他高級(jí)特性省略不再討論。
部分(2):GUICE 與 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/ 目錄。
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();
}
}
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();
}
}
上面這個(gè)class 可以直接拷貝到項(xiàng)目中不需要改變。Provider 由 Guice 注入,為每個(gè)用戶創(chuàng)建一個(gè) Application 實(shí)例。
下面是一個(gè)簡(jiǎn)單的 Application 的例子。其中參數(shù) text 是被注入的。
#!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);
}
}
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);
}
}
初始化,需要寫一個(gè) Guice 的 GuiceServletContextListener來配置 ServletModule,配置 Servlet,Application,以及其他 Guice 管理的實(shí)例,比如上面例子用到的 text。
#!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;
}
}
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 需要如此配置,來啟動(dòng) Guice Filter 和上面的那個(gè)配置模塊 (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>

<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>
posted on 2010-03-16 15:05 bing 閱讀(2191) 評(píng)論(0) 編輯 收藏 所屬分類: GUI