Guice:强制将绑定作为提供者注入
我正在寻找一种方法来强制某些 Guice 绑定仅作为提供者注入。例如,当有像这样的配置时,
interface ResultLogger {
void log(String resultAsString);
}
class ResultLoggerProvider implements Provider<ResultLogger> {
// ...
}
class ResultDisplayModule extends AbstractModule {
@Override
protected void configure() {
bind(ResultLogger.class).toProvider(ResultLoggerProvider.class);
}
}
我希望有办法配置我的模块,以便
@Singleton
class ResultParser {
private final Provider<ResultLogger> loggerProvider;
@Inject
public ResultParser(Provider<ResultLogger> loggerProvider) {
this.loggerProvider = loggerProvider;
}
}
可以很好地注入这样的类,但是像这样的实现
@Singleton
class ResultParser {
private final ResultLogger resultLogger;
@Inject
public ResultParser(ResultLogger resultLogger) {
this.resultLogger = resultLogger;
}
}
应该抛出一个 RuntimeException ,通知开发人员 ResultLogger 只能通过提供商获得。理想情况下,应尽快抛出异常,例如在构造注入器期间。我正在寻找一种简单的方法来使用 Guice 3.0 中现有的 API 来实现这一点。
I'm looking for a way to force certain Guice bindings to be injected as providers only. For example, when there is a configuration like
interface ResultLogger {
void log(String resultAsString);
}
class ResultLoggerProvider implements Provider<ResultLogger> {
// ...
}
class ResultDisplayModule extends AbstractModule {
@Override
protected void configure() {
bind(ResultLogger.class).toProvider(ResultLoggerProvider.class);
}
}
I would like to have way to configure my module so that a class like
@Singleton
class ResultParser {
private final Provider<ResultLogger> loggerProvider;
@Inject
public ResultParser(Provider<ResultLogger> loggerProvider) {
this.loggerProvider = loggerProvider;
}
}
can be injected just fine, but an implementation like
@Singleton
class ResultParser {
private final ResultLogger resultLogger;
@Inject
public ResultParser(ResultLogger resultLogger) {
this.resultLogger = resultLogger;
}
}
should throw a RuntimeException which notifies the developer that ResultLogger
is only available via a provider. The exception would ideally be thrown as soon as possible, e.g. during construction of the injector. I'm looking for an easy way to achieve this using the existing API in Guice 3.0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许您根本不应该实现 Provider,而只需要一个
并删除其他绑定。
Maybe you should not implement Provider at all and just have a
and remove the other bindings.
我认为这不是正确的方式。我想你需要smt之类的
I think that it isn't right way. I guess you need smt like
它应该可以绑定 Provider 而不是 ResultLogger。那是在你的模块中
It should work to bind Provider instead of ResultLogger. That is in your module