2017-03-04 57 views
0

我有一种使用服务器来运行其应用程序类的逻辑 实现下面的应用程序类如下:依赖于三个接口吉斯

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

你还没有说明你收到了什么错误。 – Jeremy

+0

@JeremyHeiler添加了他们。如果你可以请查看相同的 – StevieG

回答

1

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

你只需要说:

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创建和注入其中。否则它不会知道,而且会出错。

+0

谢谢@JeremyHeiler的回复。 我仍然有在GuiceModule以下错误:在该行 多个标记 \t - 前端解决不了的类型 \t - 从类型AbstractModule的方法绑定(类)是指缺少类型 \t前端 – StevieG

+0

你有叫'FrontEnd'的班吗?如果是这样,你是否正确导入它? – Jeremy

+0

我想通了,它没有正确导入。如果'FrontEnd'接口依赖于另一个接口,是否必须创建另一个GuiceModule? @JeremyHeiler – StevieG

0

谢谢@JeremyHeiler。这个前端接口恰好处于不同的包中。现在,Frontend依赖于一个称为认证的接口。当我尝试用与web服务器类似的代码实现它时,我收到错误。我写的代码是如下:

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 

我试图在互联网上搜索了很多,但找不到弄清楚。我想问题是在不同的软件包中有文件。如果你可以请让我知道。