我想用Guice 2和Wicket 1.4。有一個「wicket-guice」包,它使用Guice 1.有人可以給我一個例子如何配置Wicket使用Guice 2進行注入(使用Maven)。如何將Guice 2整合到Wicket中?
正如你所看到的那樣,我找到了一個解決方案,但是我想知道,如果使用Guice Servlets並將整個Wicket Application註冊爲ServletFilter和Guice,會更好。但我認爲這會與小門對象創建策略相沖突。
我想用Guice 2和Wicket 1.4。有一個「wicket-guice」包,它使用Guice 1.有人可以給我一個例子如何配置Wicket使用Guice 2進行注入(使用Maven)。如何將Guice 2整合到Wicket中?
正如你所看到的那樣,我找到了一個解決方案,但是我想知道,如果使用Guice Servlets並將整個Wicket Application註冊爲ServletFilter和Guice,會更好。但我認爲這會與小門對象創建策略相沖突。
爲了回答我自己,我發佈瞭解決方案,我在AtomicGamer Dev Blog的幫助下找到了解決方案。
由於wicket-guice只支持Guice 1,所以Guice需要從wicket-guice擴展中排除。
<dependencies>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-guice</artifactId>
<version>${wicket.version}</version>
<exclusions>
<exclusion>
<groupId>com.google.code.guice</groupId>
<artifactId>guice</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket</artifactId>
<version>${wicket.version}</version>
</dependency>
<dependencies>
實際的整合發生在init
方法,它調用的方法addComponentInstantiationListener
。
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.apache.wicket.Page;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.guice.GuiceComponentInjector;
public class NavigatorApplication extends WebApplication {
@Override
public Class<? extends Page> getHomePage() {
return Startpage.class;
}
@Override
protected void init() {
super.init();
Injector injector = Guice.createInjector(new WebAppModule());
addComponentInstantiationListener(
new GuiceComponentInjector(this, injector));
}
}
我已經成功實現,其中檢票口的配置和啓動使用Guice的servlet模塊純Java代碼編寫的一個解決方案 - 沒有用於檢票口在所有的XML。
所有的細節在我撰寫的博客文章中被描述爲here。
完整源代碼(zip/svn)和一個工作示例eclipse項目也可以下載(鏈接在帖子末尾)。
我想你會發現它很好,再次忘記web.xml的維護:)