Inject不适用于第二个构造函数

use*_*658 5 kotlin dagger dagger-2

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

class SelectionFragmentModel ():ViewModel(){
   lateinit var channelInfosRepository: ChannelInfosRepository
   @Inject constructor(channelInfosRepository: ChannelInfosRepository) : this(){
      this.channelInfosRepository = channelInfosRepository
   }
   ...
}
Run Code Online (Sandbox Code Playgroud)

作为一种解决方法,我目前正在主要构造函数中注入,但这不是最佳的.

class SelectionFragmentModel @Inject constructor(private val channelInfosRepository: ChannelInfosRepository):ViewModel(){
   constructor() : this(ChannelInfosRepository())
   ...
}
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?

Jen*_*anu 0

确保你的SelectionFragmentModel只有一个构造函数。就 Kotlin 语言习惯而言,构造函数是主要还是次要并不重要。中只能使用一个构造函数SelectionFragmentModel

以下代码没有为初始化程序留下关于使用哪个构造函数的选项,因为只有一个!

class SelectionFragmentModel: ViewModel {
    lateinit var channelInfosRepository: ChannelInfosRepository

    @Inject constructor(channelInfosRepository: ChannelInfosRepository) : super() {
        this.channelInfosRepository = channelInfosRepository
    }
}
Run Code Online (Sandbox Code Playgroud)

示例(有效)

在此示例中,我们使用 dagger 进行默认设置:

  1. @Module带注释的类为我们提供了ChannelInfosRepository
  2. 修改SelectionFragmentModel(代码位于示例上方);
  3. 接口注释为@Component仅包含一个模块的模块列表;
  4. 其他一切都只是 Android 的东西。

这是模块:

@Module
class AppModule {
    @Provides
    @Singleton
    fun providesChannelInfosRepository(): ChannelInfosRepository {
        return ChannelInfosRepository()
    }
}
Run Code Online (Sandbox Code Playgroud)

接口注释为@Component

@Singleton
@Component(modules = [AppModule::class])
interface AppComponent {
    fun inject(fragment: MainFragment)
}
Run Code Online (Sandbox Code Playgroud)

这是AppComponent实例化的方式。MyApplication中必须提到AndroidManifest.xml

class MyApplication : Application() {
    var appComponent: AppComponent? = null
        private set

    override fun onCreate() {
        super.onCreate()

        appComponent = DaggerAppComponent.builder()
                .appModule(AppModule())
                .build()
    }
}
Run Code Online (Sandbox Code Playgroud)

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}")
    }
}
Run Code Online (Sandbox Code Playgroud)

结果(我没有打印结果,而是将其显示在 TextView 中):

示例中代码的结果