2017-03-04 53 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 

我試圖在互聯網上搜索了很多,但找不到弄清楚。我想問題是在不同的軟件包中有文件。如果你可以請讓我知道。