让 hk2 和 Jersey 注入 类
Getting hk2 and Jersey to inject classes
如何让 Jersey 注入 classes 而无需在一对一的基础上创建和注册工厂?
我有以下配置:
public class MyConfig extends ResourceConfig {
public MyConfig() {
register(new AbstractBinder() {
@Override
protected void configure() {
bindFactory(FooFactory.class).to(Foo.class);
bindFactory(BazFactory.class).to(Baz.class);
}
});
}
}
hk2 现在将成功注入 Foo 和 Baz:
// this works; Foo is created by the registered FooFactory and injected
@GET
@Path("test")
@Produces("application/json")
public Response getTest(@Context Foo foo) {
// code
}
但这不是我的目标。我的目标是注入包装这些 classes 的对象。有很多,它们各自使用不同的 Foo 和 Baz 组合。一些例子:
public class FooExtender implements WrapperInterface {
public FooExtender(Foo foo) {
// code
}
}
public class FooBazExtender implements WrapperInterface {
public FooBazExtender(Foo foo, Baz baz) {
// code
}
}
public class TestExtender implements WrapperInterface {
public TestExtender(Foo foo) {
// code
}
// code
}
以此类推
以下无效:
// this does not work
@GET
@Path("test")
@Produces("application/json")
public Response getTest(@Context TestExtender test) {
// code
}
我可以为每个创建一个工厂并在我的应用程序配置 class 中注册它,使用 bindFactory
语法就像我对 Foo 和 Baz 所做的那样。但由于所涉及对象的数量,这不是一个好的方法。
我已经阅读了很多hk2文档,并尝试了多种方法。我只是不太了解 hk2 的实际工作原理,无法得出答案,这似乎是一个很常见的问题,应该有一个简单的解决方案。
工厂实际上只需要用于更复杂的初始化。如果你不需要这个,你需要做的就是绑定服务
@Override
protected void configure() {
// bind service and advertise it as itself in a per lookup scope
bindAsContract(TestExtender.class);
// or bind service as a singleton
bindAsContract(TestExtender.class).in(Singleton.class);
// or bind the service and advertise as an interface
bind(TestExtender.class).to(ITestExtender.class);
// or bind the service and advertise as interface in a scope
bind(TestExtender.class).to(ITestExtender.class).in(RequestScoped.class);
}
您还需要在构造函数上添加 @Inject
以便 HK2 知道注入 Foo
和 Baz
@Inject
public TestExtender(Foo foo, Baz baz) {}
我最终使用了 FastClasspathScanner to grab classes from the package(s) I was interested in. Then I called the appropriate bind methods (bindAsContract
or bind
) in batches, as mentioned in (在添加了适当的 @Inject
注释之后)。
这似乎是模拟自动扫描并避免手动注册每个 class 的最便捷方法。
感觉像是 hack,如果 hk2 没有更好的方法,我会感到惊讶。
如何让 Jersey 注入 classes 而无需在一对一的基础上创建和注册工厂?
我有以下配置:
public class MyConfig extends ResourceConfig {
public MyConfig() {
register(new AbstractBinder() {
@Override
protected void configure() {
bindFactory(FooFactory.class).to(Foo.class);
bindFactory(BazFactory.class).to(Baz.class);
}
});
}
}
hk2 现在将成功注入 Foo 和 Baz:
// this works; Foo is created by the registered FooFactory and injected
@GET
@Path("test")
@Produces("application/json")
public Response getTest(@Context Foo foo) {
// code
}
但这不是我的目标。我的目标是注入包装这些 classes 的对象。有很多,它们各自使用不同的 Foo 和 Baz 组合。一些例子:
public class FooExtender implements WrapperInterface {
public FooExtender(Foo foo) {
// code
}
}
public class FooBazExtender implements WrapperInterface {
public FooBazExtender(Foo foo, Baz baz) {
// code
}
}
public class TestExtender implements WrapperInterface {
public TestExtender(Foo foo) {
// code
}
// code
}
以此类推
以下无效:
// this does not work
@GET
@Path("test")
@Produces("application/json")
public Response getTest(@Context TestExtender test) {
// code
}
我可以为每个创建一个工厂并在我的应用程序配置 class 中注册它,使用 bindFactory
语法就像我对 Foo 和 Baz 所做的那样。但由于所涉及对象的数量,这不是一个好的方法。
我已经阅读了很多hk2文档,并尝试了多种方法。我只是不太了解 hk2 的实际工作原理,无法得出答案,这似乎是一个很常见的问题,应该有一个简单的解决方案。
工厂实际上只需要用于更复杂的初始化。如果你不需要这个,你需要做的就是绑定服务
@Override
protected void configure() {
// bind service and advertise it as itself in a per lookup scope
bindAsContract(TestExtender.class);
// or bind service as a singleton
bindAsContract(TestExtender.class).in(Singleton.class);
// or bind the service and advertise as an interface
bind(TestExtender.class).to(ITestExtender.class);
// or bind the service and advertise as interface in a scope
bind(TestExtender.class).to(ITestExtender.class).in(RequestScoped.class);
}
您还需要在构造函数上添加 @Inject
以便 HK2 知道注入 Foo
和 Baz
@Inject
public TestExtender(Foo foo, Baz baz) {}
我最终使用了 FastClasspathScanner to grab classes from the package(s) I was interested in. Then I called the appropriate bind methods (bindAsContract
or bind
) in batches, as mentioned in @Inject
注释之后)。
这似乎是模拟自动扫描并避免手动注册每个 class 的最便捷方法。
感觉像是 hack,如果 hk2 没有更好的方法,我会感到惊讶。