简体   繁体   English

如何将Guice 2集成到Wicket中?

[英]How to integrate Guice 2 into Wicket?

I want to use Guice 2 with Wicket 1.4. 我想在Wicket 1.4中使用Guice 2。 There is a "wicket-guice" package, which uses Guice 1. Can someone give me an example how to configure Wicket to use Guice 2 for injection (with Maven). 有一个使用“ Guice 1”的“ wicket-guice”软件包。有人可以给我一个示例,如何配置Wicket以使用Guice 2进行注入(与Maven一起使用)。

As you can see blow, I've found a solution, but I wonder, if it would be better to use Guice Servlets and register the whole Wicket Application as a ServletFilter with Guice. 如您所见,我找到了一个解决方案,但是我想知道,使用Guice Servlet并将整个Wicket应用程序注册为带有Guice的ServletFilter会更好。 But I think this would conflict with wickets object creation strategy. 但是我认为这将与检票口对象创建策略冲突。

To answer myself I post the solution, which I found with the help of AtomicGamer Dev Blog . 为了回答自己,我发布了解决方案,该解决方案是在AtomicGamer Dev Blog的帮助下找到的。

Since wicket-guice supports only Guice 1, Guice needs to be excluded from the wicket-guice extension. 由于wicket-guice仅支持Guice 1,因此必须从wicket-guice扩展中排除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>

The actual integrations happens in the init method, which calls the addComponentInstantiationListener method. 实际的集成发生在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));    
    }

}

I have successfully implemented a solution where wicket's configuration and startup are written purely in java code using Guice's ServletModule - no xml used for wicket at all. 我已经成功实现了一个解决方案,其中,使用Guice的ServletModule,仅用Java代码编写了wicket的配置和启动-根本没有xml用于wicket。

All the details are described here in a blog post I've written. 我写的一篇博客文章中介绍所有详细信息。

Full source (zip/svn) and a working example eclipse project are available for download as well (links are at the end of the post). 完整的源代码(zip / svn)和一个有效的eclipse示例项目也可以下载(链接在文章末尾)。

I think you'll find it great to once again forget about web.xml maintenance :) 我想您会发现再次忘记web.xml维护非常好:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM