2016-08-11 5 views
0

でオプションバインディング私はChildPluginと呼ばれる子モジュールを持っているし、次のように私はメインモジュールからクラスを注入:子モジュールのフェイルセーフインジェクタ。 GoogleのGuiceの

public class ChildPlugin { 
    private ExampleClass demo; 

    @Inject 
    public void setDemo(ExampleClass demo) { 
     this.demo = demo; 
    } 
} 

問題は、私は、メインモジュールがExampleClassをバインドするかどうかわからないし、そうでない場合ということですGuiceはインジェクタを作成するときに例外をスローします。私がしたいことは、GuiceがExampleClassがバインドされていない場合、nullまたはOptional.emptyを渡すことです。

私はOptionalBinderExampleClass用バインダーを変更することはできませんので、私は、メインモジュールへのアクセスを持っていない、私はChildPlugin.setDemo方法で@NullableOptional<ExampleClass>を試してみましたが、それはうまくいきませんでした。

答えて

1

これを行うには2つの方法があります。

オプションの注入

com.google.inject.Injectアノテーションを使用します。これにより、アノテーションにオプションを指定することができます。次の例を参照してください。

注釈は、Aがオプションであることを示します。それは注入されません。スニペットの印刷を実行します。

Object is: null 

方法2(これはguice 4+の後で行います)。オプションのバインディングを指定できます。これらのバインディングでは、必要に応じてデフォルト値を定義することもできます。

public class GuiceInjectOptional extends AbstractModule { 

    @Override 
    protected void configure() { 
     // set up optional binding for A. 
     OptionalBinder.newOptionalBinder(binder(), A.class); 

     bind(B.class).in(Singleton.class); 
    } 

    public static class A { 

     private String name; 
     // non null constructor so that A can't be instantiated automatically by guice 
     public A(String name) { 
      this.name = name; 
     } 

     @Override 
     public String toString() { 
      return "I am: " + name; 
     } 
    } 

    public static class B { 

     @Inject 
     Optional<A> obj; 

     void run() { 
      System.out.println("Object is present: " + obj.isPresent()); 
      System.out.println("Object is: " + obj); 
     } 
    } 

    public static void main(String[] args) { 
     Injector injector = Guice.createInjector(new GuiceInjectOptional()); 
     injector.getInstance(B.class).run();; 
    } 

} 

を注入アノテーションは今や非オプションですが、GuiceのがクラスAがかないかもしれないバインドされた可能性があることを認識している:あなたは、私が書いたこのスニペットのように任意の値を注入することができます。 は、スニペットを実行すると、印刷されます:

Object is present: false 
Object is: Optional.empty 

を最後に、その後、普通に結合することができ、Guiceには、それを注入します:

public class GuiceInjectOptional extends AbstractModule { 

    @Override 
    protected void configure() { 
     // set up optional binding for A. 
     OptionalBinder.newOptionalBinder(binder(), A.class); 

     bind(A.class).toInstance(new A("Pandaa!")); 
     bind(B.class).in(Singleton.class); 
    } 

    public static class A { 

     private String name; 
     // non null constructor so that A can't be instantiated automatically by guice 
     public A(String name) { 
      this.name = name; 
     } 

     @Override 
     public String toString() { 
      return "I am: " + name; 
     } 
    } 

    public static class B { 

     @Inject 
     Optional<A> obj; 

     void run() { 
      System.out.println("Object is present: " + obj.isPresent()); 
      System.out.println("Object is: " + obj); 
     } 
    } 

    public static void main(String[] args) { 
     Injector injector = Guice.createInjector(new GuiceInjectOptional()); 
     injector.getInstance(B.class).run();; 
    } 

} 

そして、上記の意志の印刷:

Object is present: true 
Object is: Optional[I am: Pandaa!] 

そして、これはあなたがguiceとのフェイルセーフオプションバインディングを持っている方法です:私はこれが助けてくれることを願っています。

編集:私はguice-3タグを見たので、オプションのバインダーではなくオプションの注釈メソッドを使用したいと思うでしょう。オプションの注釈では、オプション値ではなくヌルのままです。

アルテール

関連する問題