注入匿名内部类(GIN)
我有这样的东西:
request.findAllProjects().fire(new ExtReceiver<List<ProjectProxy>>() {
@Override
public void onSuccess(List<ProjectProxy> response) {
view.setProjects(response);
}
});
它是抽象类 ExtReceiver 的匿名内部类。 ExtReceiver 用于使用我想提供的 errorHandler 处理错误。
public abstract class ExtReceiver<T> extends Receiver<T> {
private ErrorHandler errorHandler;
public ExtReceiver() {
}
@Inject
public void setErrorHandler(ErrorHandler errorHandler)
{
this.errorHandler = errorHandler;
}
@Override
public abstract void onSuccess(T response);
@Override
public void onFailure(ServerFailure error) {
errorHandler.exception(error);
}
@Override
public void onViolation(Set<Violation> errors) {
ValidationUtils.processViolation(errors);
}
}
我明白为什么这不起作用,因为我使用新的运算符。但我怎么能做这样的事情呢。我想要那个匿名类,而不是把它放在自己的文件或其他东西中。 我怎样才能注入那个errorHandler?考虑过 staticInjections,但看起来这也不起作用(也许是因为我通过执行匿名类创建的继承)
与正常的 Guice 相反,我不知道injector.getInstance() 调用。
信息:这是一个 requestFactory 调用
I have something like this:
request.findAllProjects().fire(new ExtReceiver<List<ProjectProxy>>() {
@Override
public void onSuccess(List<ProjectProxy> response) {
view.setProjects(response);
}
});
It is anonymous inner class of the abstract class ExtReceiver. The ExtReceiver is for handling the errors with an errorHandler which i want to provide.
public abstract class ExtReceiver<T> extends Receiver<T> {
private ErrorHandler errorHandler;
public ExtReceiver() {
}
@Inject
public void setErrorHandler(ErrorHandler errorHandler)
{
this.errorHandler = errorHandler;
}
@Override
public abstract void onSuccess(T response);
@Override
public void onFailure(ServerFailure error) {
errorHandler.exception(error);
}
@Override
public void onViolation(Set<Violation> errors) {
ValidationUtils.processViolation(errors);
}
}
I understand why this can't work, because i use the new Operator. But how could i do something like this. I want to have that anonymous class and not put it in an own file or something.
How could I inject that errorHandler? Thought about staticInjections, but it looked like this does not work too (Maybe because of the inheritance i create with doing an anonymous class)
In the opposite to normal Guice i don't know an injector.getInstance() call.
For information: That is a requestFactory call
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不将 errorHandler 参数放入抽象类的构造函数中,而是创建一个单独的 setErrorHandler setter,如下所示:
声明绑定:
为 ErrorHandler 类声明一个 Ginjector 并对其进行注释与模块:
然后像这样使用它:
我认为这应该可行。
Why don't you put the errorHandler parameter into the constructor of your abstract class instead creating a separate setErrorHandler setter, something like this:
Declare the bindings:
Declare a Ginjector for your ErrorHandler class annotating it with the Module:
and then use it like this:
I think this should work.