Guice没有注入Jersey的资源

daz*_*ell 2 java rest dependency-injection jersey guice

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

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!");
    }
}
Run Code Online (Sandbox Code Playgroud)

传输接口:

public interface Transport {
    String encode(String input);
}
Run Code Online (Sandbox Code Playgroud)

它的实现:

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

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

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

main()有这个注入器,但这里不需要它:

Injector injector = Guice.createInjector(new TransportModule());
Run Code Online (Sandbox Code Playgroud)

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

Transport transport = injector.getInstance(Transport.class);
Run Code Online (Sandbox Code Playgroud)

Pau*_*tha 8

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

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

public class Binder extends AbstractBinder {
    @Override
    public void configurer() {
        bind(TransportImpl.class).to(Transport.class);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

new ResourceConfig().register(new Binder());
Run Code Online (Sandbox Code Playgroud)

一个区别是绑定声明.使用Guice,它"将契约与实现绑定",而对于HK2,它是"将实现绑定到契约".你可以看到它与Guice模块相反.

如果你想搭建Guice和HK2,那就复杂一点了.你需要更多地了解HK2.这是一个如何让它工作的例子

@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;
    }
} 
Run Code Online (Sandbox Code Playgroud)

然后注册该功能

new ResourceConfig().register(new GuiceFeature());
Run Code Online (Sandbox Code Playgroud)

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

也可以看看:


UPDATE

对不起,我忘了添加使用Guice Bridge,你需要依赖.

<dependency>
    <groupId>org.glassfish.hk2</groupId>
    <artifactId>guice-bridge</artifactId>
    <version>2.4.0-b31</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

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