Guice没有注入Jersey的资源 [英] Guice don't inject to Jersey's resources

查看:119
本文介绍了Guice没有注入Jersey的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全面解析整个互联网,但无法弄清楚为什么会这样。我有一个最简单的项目(通过jersey-quickstart-grizzly2原型)和一个Jersey资源。我使用Guice作为DI,因为CDI也不想和Jersey一起工作。问题是Guice无法解析在Jersey资源中注入时要使用的类。它在外面很好用,但不适用于泽西岛。
以下是泽西岛资源:

Parsed allover the whole internet, but can't figure out why this happens. I've got a simplest possible project (over jersey-quickstart-grizzly2 archetype) with one Jersey resource. I'm using Guice as DI because CDI doesn't want to work with Jersey either. The problem is that Guice can't resolve the class to use when injecting in Jersey's resources. It works great outside, but not with Jersey. Here is the Jersey resource:

import com.google.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("api")
public class MyResource {

    private Transport transport;

    @Inject
    public void setTransport(Transport transport) {
        this.transport = transport;
    }

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt() {
        return transport.encode("Got it!");
    }
}

传输接口:

public interface Transport {
    String encode(String input);
}

它的实现:

public class TransportImpl implements Transport {
    @Override
    public String encode(String input) {
        return "before:".concat(input).concat(":after");
    }
}

按照Google的GettingStarted手册,我继承了 AbstractModule 并绑定我的类如下:

Following Google's GettingStarted manual, I've inherited AbstractModule and bound my classes like this:

public class TransportModule extends AbstractModule {
    @Override
    protected void configure() {
        bind(Transport.class).to(TransportImpl.class);
    }
}

我在 main获得注入()有了这个,但在这里真的不需要它:

I get injector in main() with this, but don't really need it here:

Injector injector = Guice.createInjector(new TransportModule());

顺便说一句,当我尝试像这样做时,没有问题:

Btw, there's no problem when I try to do smth like this:

Transport transport = injector.getInstance(Transport.class);


推荐答案

泽西岛2已经有一个DI框架, HK2 。您可以使用它,或者如果您愿意,可以使用HK2 / Guice桥来使用HK2为您的Guice模块添加新娘。

Jersey 2 already has a DI framework, HK2. You can either use it, or if you want, you can use the HK2/Guice bridge to bride your Guice module with HK2.

如果您想使用HK2,在最基本的层面上,它与Guice模块没什么不同。例如,在您当前的代码中,您可以执行此操作

If you want to work with HK2, at the most basic level, it's not much different from the Guice module. For example, in your current code, you could do this

public class Binder extends AbstractBinder {
    @Override
    public void configurer() {
        bind(TransportImpl.class).to(Transport.class);
    }
}

然后只需在Jersey上注册活页夹

Then just register the binder with Jersey

new ResourceConfig().register(new Binder());

一个区别是绑定声明。使用Guice,它约束合同到实现,而对于HK2,它是绑定实现合同。你可以看到它与Guice模块相反。

One difference is the the binding declarations. With Guice, it "bind contract to implementation", while with HK2, it's "bind implementation to contract". You can see it's reversed from the Guice module.

如果你想要连接Guice和HK2,那就复杂一点了。您需要了解更多关于HK2的信息。以下是如何使其工作的示例

If you want to bridge Guice and HK2, it's little more complicated. You need to understand a little more about HK2. Here's an example of how you can get it to work

@Priority(1)
public class GuiceFeature implements Feature {

    @Override
    public boolean configure(FeatureContext context) {
        ServiceLocator locator = ServiceLocatorProvider.getServiceLocator(context);
        GuiceBridge.getGuiceBridge().initializeGuiceBridge(locator);
        Injector injector = Guice.createInjector(new TransportModule());
        GuiceIntoHK2Bridge guiceBridge = locator.getService(GuiceIntoHK2Bridge.class);
        guiceBridge.bridgeGuiceInjector(injector);
        return true;
    }
} 

然后注册功能

new ResourceConfig().register(new GuiceFeature());

就个人而言,如果您打算使用泽西岛,我建议您熟悉HK2。

Personally, I would recommend getting familiar with HK2, if you're going to use Jersey.

另见:

  • HK2 Documentation
  • Custom Injection and Lifecycle Management

抱歉,我忘了添加使用Guice Bridge,你需要依赖。

Sorry, I forgot to add that to use the Guice Bridge, you need to dependency.

<dependency>
    <groupId>org.glassfish.hk2</groupId>
    <artifactId>guice-bridge</artifactId>
    <version>2.4.0-b31</version>
</dependency>

请注意,这是Jersey 2.22.1的依赖关系。如果您使用的是不同版本的HK2,则应确保使用与Jersey版本相同的HK2版本。

Note that this is the dependency that goes with Jersey 2.22.1. If you are using a different version of HK2, you should make sure to use the same HK2 version that your Jersey version is using.

这篇关于Guice没有注入Jersey的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆