kotlin - 注入(inject)不适用于第二个构造函数

标签 kotlin dagger-2 dagger

对于我当前的项目,我使用的是 Kotlin 和 Dagger 2。
我想在辅助构造函数中注入(inject)依赖项,但构造函数永远不会被初始化。

class SelectionFragmentModel ():ViewModel(){
   lateinit var channelInfosRepository: ChannelInfosRepository
   @Inject constructor(channelInfosRepository: ChannelInfosRepository) : this(){
      this.channelInfosRepository = channelInfosRepository
   }
   ...
}

作为一种解决方法,我目前正在注入(inject)主构造函数,但这不是最佳的。
class SelectionFragmentModel @Inject constructor(private val channelInfosRepository: ChannelInfosRepository):ViewModel(){
   constructor() : this(ChannelInfosRepository())
   ...
}

我错过了什么吗?

最佳答案

确保您的 SelectionFragmentModel类(class) 只有一个构造函数 .就 Kotlin 语言习语而言,构造函数是主要的还是次要的并不重要。 SelectionFragmentModel 中将只使用一个构造函数.
以下代码没有为初始化程序留下关于使用哪个构造函数的选项,因为只有一个!

class SelectionFragmentModel: ViewModel {
    lateinit var channelInfosRepository: ChannelInfosRepository

    @Inject constructor(channelInfosRepository: ChannelInfosRepository) : super() {
        this.channelInfosRepository = channelInfosRepository
    }
}
示例(有效)
在此示例中,我们使用 dagger 进行默认设置:
  • @Module为我们提供 ChannelInfosRepository 的带注释的类;
  • 修改SelectionFragmentModel (代码位于示例上方);
  • 带有 @Component 注释的接口(interface)包含仅包含一个模块的模块列表;
  • 其他一切都只是 Android 的东西。

  • 这是模块:
    @Module
    class AppModule {
        @Provides
        @Singleton
        fun providesChannelInfosRepository(): ChannelInfosRepository {
            return ChannelInfosRepository()
        }
    }
    
    带有 @Component 注释的接口(interface):
    @Singleton
    @Component(modules = [AppModule::class])
    interface AppComponent {
        fun inject(fragment: MainFragment)
    }
    
    以下是 AppComponent 的方法被实例化。 MyApplication必须在 AndroidManifest.xml 中提及.
    class MyApplication : Application() {
        var appComponent: AppComponent? = null
            private set
    
        override fun onCreate() {
            super.onCreate()
    
            appComponent = DaggerAppComponent.builder()
                    .appModule(AppModule())
                    .build()
        }
    }
    
    注入(inject)的片段 SelectionFragmentModel使用 AppComponent :
    class MainFragment : Fragment() {
    
        @Inject
        lateinit var selectionFragmentModel: SelectionFragmentModel
    
        companion object {
            fun newInstance() = MainFragment()
        }
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            // Ugly but fine for a test
            (activity?.application as? MyApplication)?.appComponent?.inject(this)
        }
    
        // onCreateView and other stuff ...
    
        @SuppressLint("SetTextI18n")
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
            println("ViewModel address = ${selectionFragmentModel}")
            println("ChannelInfosRepository address = ${selectionFragmentModel.channelInfosRepository}")
        }
    }
    
    结果(而不是打印我在 TextView 中显示的结果):
    The result of the code in example

    关于kotlin - 注入(inject)不适用于第二个构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45599332/

    相关文章:

    java - Dagger2 构造函数注入(inject)不起作用

    android - 我在使用 Kotlin 的 android 中有错误

    kotlin - Kotlin 中的内部类创建失败

    kotlin - Kotlin 中的 Websocket 客户端

    amazon-web-services - 使用@SqsListener时,Spring Boot应用程序启动时失败

    android - 如何使用 Dagger 2 将上下文注入(inject) Presenter

    android - 如何让 Dagger 1 和 Dagger 2 在一个 Android 项目中共存?

    android - 使用 Dagger 2 在单元测试中进行字段注入(inject)

    java - 如何避免在 Activity 中设置我所有组件的模块以注入(inject) Dagger 2 的依赖项?

    java - Dagger 注入(inject)的类在 Dagger 中保持为 null