如何让泽西与Dagger依赖注入一起工作? [英] How to make Jersey work with Dagger dependency injection?
问题描述
泽西岛通常使用HK2依赖注入,但我想使用泽西与Dagger 2. Dagger和HK2都是JSR 330 complient,我已经把它作为证据,这应该是可能的,没有太多的努力。我找到了让泽西与CDI(例如Weld),Spring DI和Guice合作的方法,但我在Dagger上找不到任何东西。
提供一些背景:I在SE环境中运行Grizzly-Jersey服务器,而不是在EE容器中运行。我的Maven项目有 com.google.dagger:dagger
和 org.glassfish.jersey.containers:jersey-container-grizzly2-http
作为依赖项,但不 org.glassfish.jersey.inject:jersey-hk2
,因为我想用Dagger取代HK2。 / p>
资源类如下所示:
@Path(/示例)
public final class ExampleResource {
private final Dependency dependency;
@Inject
public ExampleResource(final Dependency dependency){
this.dependency = Objects.requireNonNull(dependency);
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public示例getExample(){
return this.dependency.giveExample();
}
}
Dagger组件可以例如定义如下:
@Component
public interface Application {
public ExampleResource exampleEndpoint ();
public XyzResource xyzEndpoint();
//等
}
所以主要是方法看起来类似于:
public final class Main {
public static void main(final String [] args){
final Application application = DaggerApplication.create();
final URI baseUri = UriBuilder.fromUri(http://0.0.0.0/)。port(80).build();
final ResourceConfig resourceConfig = new ResourceConfig();
//如何使用`application`初始化`resourceConfig`?
final HttpServer httpServer = GrizzlyHttpServerFactory
.createHttpServer(baseUri,resourceConfig,false);
try {
httpServer.start();
} catch(最终IOException ex){
...
}
}
}
立即运行应用程序会导致异常: IllegalStateException:找不到InjectionManagerFactory。
似乎是匕首需要实施这个工厂。
我的问题是:如何将Dagger与泽西整合?
你需要实现一个 InjectionManagerFactory
,它将返回 InjectionManager
委托Dagger并通过在 META-INF / services
中输入一个条目将其注册为服务,类似于此处的hk2:
https://github.com/jersey/jersey/blob/master/inject/hk2/src/main/resources/ META-INF / services / org.glassfish.jersey.internal.inject.InjectionManagerFactory
但引用您自己的实现。
Jersey normally uses HK2 dependency injection, but I would like to use Jersey with Dagger 2. Dagger and HK2 are both JSR 330 complient, which I have taken as evidence that this should be possible without too much effort. I found ways to make Jersey work with CDI (e.g. Weld), Spring DI and Guice, but I can't find anything on Dagger.
To provide some context: I'm running a Grizzly–Jersey server in an SE environment, not in an EE container. My Maven project has com.google.dagger:dagger
and org.glassfish.jersey.containers:jersey-container-grizzly2-http
as dependencies, but not org.glassfish.jersey.inject:jersey-hk2
, since I want to replace HK2 with Dagger.
The resource classes look like this:
@Path("/example")
public final class ExampleResource {
private final Dependency dependency;
@Inject
public ExampleResource(final Dependency dependency) {
this.dependency = Objects.requireNonNull(dependency);
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public Example getExample() {
return this.dependency.giveExample();
}
}
And the Dagger component could e.g. be defined as follows:
@Component
public interface Application {
public ExampleResource exampleEndpoint();
public XyzResource xyzEndpoint();
// etc.
}
So that the main method would look similar to:
public final class Main {
public static void main(final String[] args) {
final Application application = DaggerApplication.create();
final URI baseUri = UriBuilder.fromUri("http://0.0.0.0/").port(80).build();
final ResourceConfig resourceConfig = new ResourceConfig();
// how to initialize `resourceConfig` using `application`?
final HttpServer httpServer = GrizzlyHttpServerFactory
.createHttpServer(baseUri, resourceConfig, false);
try {
httpServer.start();
} catch (final IOException ex) {
...
}
}
}
Running the application immediately results in an exception: IllegalStateException: InjectionManagerFactory not found.
It seems that a Dagger implementation of this factory is needed.
My question is: how to integrate Dagger with Jersey?
You need to implement an InjectionManagerFactory
that will return an InjectionManager
delegating to Dagger and have it registered as a service by putting an entry in META-INF/services
, similar to the hk2 one here:
https://github.com/jersey/jersey/blob/master/inject/hk2/src/main/resources/META-INF/services/org.glassfish.jersey.internal.inject.InjectionManagerFactory
but referencing your own implementation.
这篇关于如何让泽西与Dagger依赖注入一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!