簡體   English   中英

@Nullable似乎不適用於@AssistedInject

[英]@Nullable doesn't seem to work with @AssistedInject

我有一個看起來像這樣的構造函數:

@Inject
public MyClass(
        @Named("config") String configFile,
        @Named("template") String templateFile,
        CachedSettings settings,
        @Assisted String channelId,
        @Nullable @Assisted("NetworkA") NetworkInterface localNetworkInterfaceA,
        @Nullable @Assisted("NetworkB") NetworkInterface localNetworkInterfaceB) {

我得到以下錯誤(兩次,每個參數一次)

1) null returned by binding at my.company.package.MyClassFactory.create()
 but parameter 4 of my.company.package.MyClass.<init>() is not @Nullable
  while locating java.net.NetworkInterface annotated with @com.google.inject.assistedinject.Assisted(value=NetworkA)
   for parameter 4 at my.company.package.MyClass.<init>(MyClass.java:24)
  while locating my.company.package.MyClass annotated with interface com.google.inject.assistedinject.Assisted

知道有什么問題嗎? 我在這個問題上發現了另外兩個問題,其中一個說這是我不認為有的依賴關系問題 ,另一個說這是我確實使用過的Eclipse問題 ,但是我刷新,清理並重建了我的問題。 Maven從頭開始項目,所以我不確定是什么問題。

我正在使用javax.annotation.Nullable ,應該將其保留在運行時。 我還應該嘗試什么?

@Nullable需要在構造函數和工廠聲明中都設置。 我們使用FactoryModuleBuilder聲明我們的工廠。 我正在粘貼對我們有用的相關代碼

構造函數:

@Inject
public AddressActions(EC2Service ec2Service,
                      RequestFactory requestFactory,
                      @Assisted("spiceManager") SpiceManager spiceManager,
                      @Assisted("parent") Context parent,
                      @Assisted("publicIp") @Nullable String publicIp) {

抽象工廠:

public static interface Factory {
    AddressActions create(@Assisted("spiceManager") SpiceManager spiceManager,
                          @Assisted("parent") Context context,
                          @Assisted("publicIp") @Nullable String publicIp);
}

工廠模塊生成器:

install(new FactoryModuleBuilder().build(AddressActions.Factory.class));

電話:

AddressActions actions = actionsFactory.create(spiceManager, getSherlockActivity(), null);

相關版本:

  • 吉斯: guice-3.0-no_aop.jar
  • AssistedInject: guice-assistedinject-3.0.jar
  • JSR305: jsr305-1.3.9.jar

-k

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM