注入匿名内部类(GIN)

发布于 2024-10-27 04:04:49 字数 1183 浏览 5 评论 0原文

我有这样的东西:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滥情空心 2024-11-03 04:04:49

为什么不将 errorHandler 参数放入抽象类的构造函数中,而是创建一个单独的 setErrorHandler setter,如下所示:

public abstract class ExtReceiver<T> extends Receiver<T> {

    private ErrorHandler errorHandler;

    @Inject
    public ExtReceiver(ErrorHandler errorHandler) {
         this.errorHandler = errorHandler;
    }

}

声明绑定:

public class MyClientModule extends AbstractGinModule {
  protected void configure() {
    bind(ErrorHandler.class).in(Singleton.class);
  }
}

为 ErrorHandler 类声明一个 Ginjector 并对其进行注释与模块:

@GinModules(MyClientModule.class)
public interface MyErrorHandlerInjector extends Ginjector {
  ErrorHandler getErrorHandler();
}

然后像这样使用它:

MyErrorHandlerGinjector injector = GWT.create(MyErrorHandlerGinjector.class);
ErrorHandler errorHandler = injector.getErrorHandler();
request.findAllProjects().fire(new ExtReceiver<List<ProjectProxy>>(errorHandler) {      

        @Override
        public void onSuccess(List<ProjectProxy> response) {
            view.setProjects(response);

        }           
    });

我认为这应该可行。

Why don't you put the errorHandler parameter into the constructor of your abstract class instead creating a separate setErrorHandler setter, something like this:

public abstract class ExtReceiver<T> extends Receiver<T> {

    private ErrorHandler errorHandler;

    @Inject
    public ExtReceiver(ErrorHandler errorHandler) {
         this.errorHandler = errorHandler;
    }

}

Declare the bindings:

public class MyClientModule extends AbstractGinModule {
  protected void configure() {
    bind(ErrorHandler.class).in(Singleton.class);
  }
}

Declare a Ginjector for your ErrorHandler class annotating it with the Module:

@GinModules(MyClientModule.class)
public interface MyErrorHandlerInjector extends Ginjector {
  ErrorHandler getErrorHandler();
}

and then use it like this:

MyErrorHandlerGinjector injector = GWT.create(MyErrorHandlerGinjector.class);
ErrorHandler errorHandler = injector.getErrorHandler();
request.findAllProjects().fire(new ExtReceiver<List<ProjectProxy>>(errorHandler) {      

        @Override
        public void onSuccess(List<ProjectProxy> response) {
            view.setProjects(response);

        }           
    });

I think this should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文