亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

牙簽不會在注入的實例中注入依賴項

牙簽不會在注入的實例中注入依賴項

慕哥6287543 2022-05-21 13:47:17
我為 Toothpick 和 Authenticator 創建了 Activity、ViewModel、Modules。@Singletonpublic class GetSmsViewModel {    @Inject Application app;    @Inject Authenticator authenticator;...}public class GetSmsActivity extends AppCompatActivity {    private Scope appScope;    @Inject GetSmsViewModel mGetSmsViewModel;...    @Override    protected void onCreate(Bundle savedInstanceState) {        appScope = Toothpick.openScope(getApplication());        appScope.installModules(new DIModule(getApplication()), new DataModule());        super.onCreate(savedInstanceState);        Toothpick.inject(this, appScope);   ...   }}public class DIModule extends Module {    public MagicDeliveryMainModule(Application application) {        bind(GetSmsViewModel.class).toInstance(new GetSmsViewModel());        bind(Application.class).toInstance(application);        bind(Authenticator.class).toInstance(new Authenticator());    }}在 Toothpick 的文檔中寫道:“如果 Toothpick 創建一個實例,它將始終注入其依賴項?!?, 但在 Toothpick.inject(this, appScope); 之后mGetSmsViewModel.app == null 和 mGetSmsViewModel.authenticator == null 。在 Toothpick.inject(mGetSmsViewModel, appScope); 之后 app 和 authenticationator 字段被注入。應該是這樣嗎?
查看完整描述

1 回答

?
至尊寶的傳說

TA貢獻1789條經驗 獲得超10個贊

在您的示例中,牙簽沒有創建實例,您正在創建實例并綁定它。

要讓牙簽創建實例,需要綁定目標實現類并提供注入構造函數。


查看完整回答
反對 回復 2022-05-21
  • 1 回答
  • 0 關注
  • 96 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號