java 如何使用 Google Guice 将一个实现绑定到几个接口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4791908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to bind one implementation to a few interfaces with Google Guice?
提问by Pavel
I need to bind one class as implementation of two interfaces. And it should be binded in a singleton scope.
我需要绑定一个类作为两个接口的实现。并且它应该绑定在单例范围内。
What I've done:
我所做的:
bind(FirstSettings.class).
to(DefaultSettings.class).
in(Singleton.class);
bind(SecondSettings.class).
to(DefaultSettings.class).
in(Singleton.class);
But, obviously, it leads to creation of two different instances, because they are binded to the different keys.
但是,很明显,这会导致创建两个不同的实例,因为它们绑定到不同的键。
My question is how can I do that?
我的问题是我该怎么做?
回答by Olivier Grégtheitroade
Guice's wiki has a documentation about this use case.
Guice 的 wiki 有关于这个用例的文档。
Basically, this is what you should do:
基本上,这是你应该做的:
// Declare that the provider of DefaultSettings is a singleton
bind(DefaultSettings.class).in(Singleton.class);
// Bind the providers of the interfaces FirstSettings and SecondSettings
// to the provider of DefaultSettings (which is a singleton as defined above)
bind(FirstSettings.class).to(DefaultSettings.class);
bind(SecondSettings.class).to(DefaultSettings.class);
There is no need to specify any additional classes: just think in terms of Provider
s and the answer comes rather naturally.
不需要指定任何额外的类:只要从Provider
s 的角度考虑,答案就会很自然地出现。
回答by Pavel
The solution, I've found is:
我发现的解决方案是:
bind(FirstSettings.class).
to(DefaultSettings.class).
in(Singleton.class);
bind(SecondSettings.class).
to(FirstSettings.class).
in(Singleton.class);
It works in my case, because SecondSettings extends FirstSettings.
它适用于我的情况,因为 SecondSettings 扩展了 FirstSettings。
回答by Boris Pavlovi?
So, DefaultSettings
is an implementation of both the FirstSettings
and SecondSettings
, ie:
那么,DefaultSettings
是双方的一种实现FirstSettings
和SecondSettings
,即:
public class DefaultSettings implements FirstSettings, SecondSettings
{
}
Let's say you have a class EngineA
and EngineB
假设你有一堂课EngineA
并且EngineB
public class EngineA
{
@Inject
private FirstSettings settings;
}
public class EngineB
{
@Inject
private SecondSettings settings;
}
and you would like to inject the same implementation.
并且您想注入相同的实现。
You can declare an interface DefaultSettingsInterface
你可以声明一个接口 DefaultSettingsInterface
public interface DefaultSettingsInterface extends FirstSettings, SecondSettings
{
}
and make the DefaultSettings
implement DefaultSettingsInterface
并制作DefaultSettings
工具DefaultSettingsInterface
public class DefaultSettings implements DefaultSettingsInterface
{
}
Then the configuration may be:
那么配置可能是:
bind(FirstSettings.class).to(DefaultSettingsInterface.class);
bind(SecondSettings.class).to(DefaultSettingInterface.class);
bind(DefaultSettingsInterface.class).to(DefaultSettings.class).in(Singleton.class);