我有以下使用服务器运行其逻辑的应用程序类
应用程序类的实现如下:

    package edu.umd.fcmd.guice.application;

import com.google.inject.Guice;
import com.google.inject.Injector;


public class WebApplication {
    private WebServer server;

    public void run() {
        System.out.println("starting web application...");


        Injector injector = Guice.createInjector(new WebGuiceModule());
        server = injector.getInstance(WebServer.class);

        server.run();

        System.out.println("web application finished.");
    }

    public static void main(String[] args) {
        WebApplication app = new WebApplication();

        app.run();
    }
}


服务器类如下,它取决于三个接口:

public class WebServer{

    private final Frontend frontend;
    private final Middleware middleware;
    private final Persistance persistance;

    @Inject
    public WebServer(@Named("front")Frontend frontend, @Named("middle")Middleware middleware, @Named("pers")Persistance persistance) {

        this.frontend = frontend;
        this.middleware = middleware;
        this.persistance = persistance;
    }

    public String getType() {

        return "WebServer";
    }

    public boolean run() {

        System.out.println("running " + this.getType());

        Injector injector = Guice.createInjector();
        Frontend frontend = injector.getInstance(Frontend.class);
        frontend.run();
        Middleware middleware = injector.getInstance(Middleware.class);
        middleware.run();
        Persistance persistance = injector.getInstance(Persistance.class);
        persistance.run();

        return true;
    }
}


我的webguicemodule如下:

public class WebGuiceModule extends AbstractModule{
    @Override
    protected void configure(){
        bind(WebServer.class).annotatedWith(Names.named("front")).to(FrontEnd.class);
        bind(WebServer.class).annotatedWith(Names.named("middle")).to(Middleware.class);
        bind(WebServer.class).annotatedWith(Names.named("pers")).to(Persistance.class);
    }

}


我不确定为什么我的模块不能正常工作。当我编写bind语句时,它仍然存在错误。不知道为什么
我收到以下错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The method to(Class<? extends WebServer>) in the type LinkedBindingBuilder<WebServer> is not applicable for the arguments (Class<FrontEnd>)
    FrontEnd cannot be resolved to a type
    The method to(Class<? extends WebServer>) in the type LinkedBindingBuilder<WebServer> is not applicable for the arguments (Class<Middleware>)
    Middleware cannot be resolved to a type
    The method to(Class<? extends WebServer>) in the type LinkedBindingBuilder<WebServer> is not applicable for the arguments (Class<Persistance>)
    Persistance cannot be resolved to a type

最佳答案

您没有正确使用bind()。您已经将WebGuiceModule配置为使FrontEndMiddlewarePersistanceWebServer的子类。但是,编译器错误表明情况并非如此。

您只需要说:

bind(FrontEnd.class);
bind(Middleware.class);
bind(Persistance.class);


然后,当您向注入器询问WebServer的实例时,它将知道如何创建需要传递给构造函数的对象。

WebServer server = injector.getInstance(WebServer.class);




在这种情况下,您不需要@Named。这种情况是这样的:

public class Foo {

    @Inject
    public Foo(@Named("bar") Jar bar, @Named("tar") Jar tar) {
    }
}

public interface Jar {}
public class Bar extends Jar {}
public class Tar extends Jar {}


然后在一个模块中...

bind(Jar.class).annotatedWith(Names.named("bar")).to(Bar.class);
bind(Jar.class).annotatedWith(Names.named("tar")).to(Tar.class);


“名称”消除了要创建和注入的Jar实现的歧义。否则它将不知道,并且会出错。

09-13 01:04