由于要迁移到jersey 2,我需要从guice迁移到HK2。对于某些依赖项,我有一种辅助注入方法,这使我无法在HK2中实施。看来应该通过Custom Injection Resolvers来解决,但我真的不知道如何解决。这些例子对我来说还不够清楚。
这是在Guice上的外观:
public interface MyFactory {
public MyClass createMyClass(@Assisted String dynamicParameter);
public HisClass createHisClass(@Assisted String dynamicParameter);
...
}
binder.install(new FactoryModuleBuilder().build(MyFactory.class));
public class MyClass {
...
@Inject
public MyClass(@Assisted String dynamicParameter, SomeService someOtherServiceInjectedAutomatically){
...
}
}
如何在HK2上实现此功能?
最佳答案
发布问题后,我想到了这样做:
public class MyFactoryImpl implements MyFactory{
private final SomeService someService;
@Inject
public MyFactoryImpl(SomeService someService){
this.someService = someService;
}
public MyClass createMyClass(String dynamicParameter){
return new MyClass(dynamicParameter, someService);
}
...
}