简体   繁体   English

HK2相当于Guice的静态注入绑定和ProviderBinding

[英]HK2 equivalent to Guice's static injection binding and ProviderBinding

I have code like the following 我有如下代码

public class ProcessorFactory {
  @Inject static Provider<Processor> processorProvider;
  ...
}

public class ProcessorProvider implements Provider<Processor> {
  ...
  @Override
  public Processor get() {
    ...
  }
}

and in Guice the binding is done as follows 在Guice中,绑定如下进行

bind(Processor.class).toProvider(ProcessorProvider.class).in(ServletScopes.REQUEST);

requestStaticInjection(ProcessorFactory.class);

However my project was set up by someone else using HK2. 然而,我的项目是由其他人使用HK2设立的。 Can somebody help me with the equivalent binding in HK2? 有人可以用HK2中的等效绑定来帮助我吗? It seems there is only bind, bindAsContract, and bindFactory options... 似乎只有bind,bindAsContract和bindFactory选项......

HK2 uses Factories where Guice uses Providers. HK2使用Guice使用提供商的工厂。 If it's prohibitively difficult to reimplement your providers as factories, HK2 devs recommend creating a class that implements Factory<T> and accepts a Provider as a constructor arg. 如果将提供者重新实现为工厂非常困难, HK2开发人员建议创建一个实现Factory<T>的类,并接受Provider作为构造函数arg。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM