java 使用 Guice 的构造函数注入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2105895/
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
Constructor Injection using Guice
提问by Jordan Allan
I have some sample code which is using factories. I'd like to clean up the code by removing the factories and use Guice instead. I attempted to do this but I hit a small roadblock. I am really new to Guice, so I am hoping someone can help me out here.
我有一些使用工厂的示例代码。我想通过删除工厂并使用 Guice 来清理代码。我试图这样做,但我遇到了一个小障碍。我对 Guice 真的很陌生,所以我希望有人能在这里帮助我。
Existing client code (Using factories):
现有客户端代码(使用工厂):
public class MailClient {
public static void main(String[] args) {
MailConfig config = MailConfigFactory.get();
config.setHost("smtp.gmail.com");
Mail mail = MailFactory.get(config);
mail.send();
}
}
My attempt to refactor using Guice:
我尝试使用 Guice 进行重构:
//Replaces existing factories
public class MailModule extends AbstractModule {
@Override
protected void configure() {
bind(Mail.class)
.to(MailImpl.class);
bind(MailConfig.class)
.to(MailConfigImpl.class);
}
}
public class MailImpl implements Mail {
private final MailConfig config;
@Inject
public MailImpl(MailConfig config) {
this.config = config;
}
public void send() { ... }
}
public class MailClient {
public static void main(String[] args) {
MailModule mailModule = new MailModule();
Injector injector = Guice.createInjector(mailModule);
MailConfig config = injector.getInstance(MailConfig.class);
config.setHost("smtp.gmail.com");
Mail mail = //??
mail.send();
}
}
How would I construct an instance of MailImplusing the object configin my revised MailClient? Should I be using Guice in this way?
我将如何在修改后的 MailClient 中构造MailImpl使用该对象的实例config?我应该以这种方式使用 Guice 吗?
采纳答案by Jesse Wilson
Take a look at AssistedInject. It appears to address this problem.
看看AssistedInject。它似乎解决了这个问题。
回答by Andreas Petersson
2 solutions are possible: 1) bind the config as a guice object also, including its host parameter. then just inject Mail, in your main method you cna ignore the fact that mail has further dependencies.
可能有 2 种解决方案:1) 将配置也绑定为 guice 对象,包括其主机参数。然后只需注入邮件,在您的主要方法中,您可以忽略邮件具有进一步依赖性的事实。
2) mail must be configured individually for each send (recipient?). then you have no choice, but create it yourself using MailFactory.
2)必须为每次发送(收件人?)单独配置邮件。那么你别无选择,只能使用 MailFactory 自己创建它。
回答by Eric Mintz
You can do everything in MailModule as follows:
您可以按如下方式在 MailModule 中执行所有操作:
public class MailModule extends AbstractModule {
@Override
protected void configure() {
... // other bindings
}
@Provides
MailConfig getMailConfig( ... ) {
MailConfig config = new MailConfig( ... );
config.setHost("smtp.gmail.com");
config;
}
}
If you want a singleton MailConfig, add the @Singleton annotation to getMailConfig(), and Bob's your uncle.
如果你想要一个单独的 MailConfig,在 getMailConfig() 中添加 @Singleton 注释,Bob 是你的叔叔。
Note that arguments to getMailConfig must be bound. When you bind commonly used types like String, be sure to add a binding annotation.
请注意,必须绑定 getMailConfig 的参数。绑定String等常用类型时,一定要加绑定注解。

