一个class对三个接口Guice的依赖
Dependency of one class on three interfaces Guice
我有以下应用程序 class,它使用服务器来 运行 它的逻辑
应用class的实现如下:
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();
}
}
服务器class如下,依赖三个接口:
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;
}
}
我的webguice模块如下:
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
,使得 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);
"name" 消除了要创建和注入 Jar
的哪个实现的歧义。否则它不会知道,它会出错。
谢谢@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
我尝试在互联网上搜索了很多,但找不到答案。我想问题是文件在不同的包中。如果可以请告诉我。
我有以下应用程序 class,它使用服务器来 运行 它的逻辑 应用class的实现如下:
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();
}
}
服务器class如下,依赖三个接口:
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;
}
}
我的webguice模块如下:
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
,使得 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);
"name" 消除了要创建和注入 Jar
的哪个实现的歧义。否则它不会知道,它会出错。
谢谢@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
我尝试在互联网上搜索了很多,但找不到答案。我想问题是文件在不同的包中。如果可以请告诉我。