我有一種使用服務器來運行其應用程序類的邏輯 實現下面的應用程序類如下:依賴於三個接口吉斯
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);
}
}
我不知道爲什麼我的模塊無法正常工作。編寫綁定語句時仍然存在錯誤。無法弄清楚爲什麼 我收到以下錯誤:
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
你還沒有說明你收到了什麼錯誤。 – Jeremy
@JeremyHeiler添加了他們。如果你可以請查看相同的 – StevieG