我有以下应用程序类,它使用服务器来运行其逻辑 应用程序类的实现如下:
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
答案 0 :(得分:2)
您没有正确使用bind()
。您已配置WebGuiceModule
,FrontEnd
,Middleware
和Persistance
是WebServer
的子类。但是,编译器错误表明情况并非如此。
你只需要说:
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);
&#34;名称&#34;消除创建和注入Jar
的实现的歧义。否则它就不会知道,而且会出错。
答案 1 :(得分:0)
谢谢@JeremyHeiler。此前端接口恰好位于不同的包中。现在,前端依赖于称为身份验证的接口。当我尝试使用与网络服务器类似的代码实现它时,我遇到了错误。我写的代码如下:
package edu.umd.fcmd.guice.interfaces;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import edu.umd.fcmd.guice.application.WebServer;
import edu.umd.fcmd.guice.interfaces.Authentication;
public interface Frontend{
private final Authentication authentication;
@Inject
public interface(Authentication authentication) {
System.out.println("5");
this.authentication = authentication;
}
public static String getType(){
return "Frontend";
}
public default boolean run(){
System.out.println("in frontend");
authentication.run();
return true;
}
}
错误如下:
Multiple markers at this line
- Duplicate field Frontend.authentication
- Illegal modifier for the interface field Frontend.authentication; only public, static & final are
permitted
Syntax error on token "interface", Identifier expected
The static field Frontend.authentication should be accessed in a static way
我试过在互联网上搜索了很多但是找不到噱头。我想问题是在不同的包中有文件。如果可以,请告诉我。