使用TypeLiteral的Java注入实现 [英] Java inject implementation using TypeLiteral

查看:71
本文介绍了使用TypeLiteral的Java注入实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个提供接口的项目,我们称它为IImplementMe,我想将其注入到我的项目中.该接口将由各种生产者实现,因此我需要注入所有实现.我正在尝试为此使用TypeLiteral.

I have a project that provides an interface, let's call it IImplementMe, which i want to inject into my project. This interface will be implemented by various producers, so I need to inject all implementations. I am trying to use TypeLiteral for this.

这是生产者的代码:

@Singleton
public class SomeImplementation implements IImplementMe {

private final String value;

@Inject
public SomeImplementation(final SomeOtherConfig configuration) {
    this.value= configuration.getValue();
}

@Override
public String getValue() {
    return value;
}
}

在我的注册表类中,我有register(IImplementMe.class).to(SomeImplementation.class);

And in my registry class I have register(IImplementMe.class).to(SomeImplementation.class);

然后,在我的项目中,我像这样注入它:

Then, in my project I inject it like this :

@Inject
public SomeEndpoint(final List<IImplementMe> implementations){
///
}

我像这样绑定它

private static class MarketDataSetTypeLiteral extends TypeLiteral<List<IImplementMe>> {
}
bind(new MarketDataSetTypeLiteral()).toRegistry();

我确保调用了SomeIMplementation构造函数,但是在我的端点中,列表为空,因此未提供任何实现.我正在用Guice注射剂.有什么想法吗?

I made sure my SomeIMplementation constructor gets called, but in my endpoint the List is empty, so no implementation is provided. I'm using guice for injection. Any ideas ?

LE:事实证明,所提供的实现是在创建我的端点类之后创建的(在创建时,它会注入一个空列表的引用).在生命周期的后期,该引用会随实现进行更新,因此,在guice处理完之后,我实际上可以访问它.

LE: It turns out that the provided implementation is created after my endpoint class is created (at creation time it injects a reference of an empty list). Later in the lifecycle the reference is updated with the implementation, so I actually have access to it after guice does it's stuff.

我猜这是由于Maven依赖关系以及guice如何处理实例化.由于生产者必须对我的项目有依赖性,所以我认为它最后被实例化是有道理的,从而引起了我的最初问题.

I'm guessing it's due to the maven dependencies, and how guice handles the instantiations. Since the producer must have a dependency on my project, I guess it makes sense it gets instantiated last, thus causing my initial problem.

推荐答案

您正在寻找多重绑定-> https://github.com/google/guice/wiki/Multibindings

You are looking for multibindings -> https://github.com/google/guice/wiki/Multibindings

public class IImplementMeModule extends AbstractModule {
  public void configure() {
    Multibinder< IImplementMe > uriBinder = Multibinder.newSetBinder(binder(), IImplementMe.class);
    uriBinder.addBinding().to(SomeImplementationOfIImplementMe.class);
    uriBinder.addBinding().to(AnotherImplementationOfIImplementMe.class);

    ... // bind plugin dependencies, such as our Flickr API key
  }
}

然后您可以按以下方式注入IImplemetnMe的集合

Then you can inject the set of IImplemetnMe as following

@Inject TweetPrettifier(Set<IImplemetnMe> implementations)

我建议您先看看MapBindings,它允许您为每个实现提供密钥,然后您就可以将绑定作为Map注入

I would suggest you to have a look at MapBindings which allows you provide keys for each implementation and then you will be able to inject your bindings as a Map

这篇关于使用TypeLiteral的Java注入实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆