对于我当前的项目,我使用的是 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)包含仅包含一个模块的模块列表; 这是模块:
@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 中显示的结果):关于kotlin - 注入(inject)不适用于第二个构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45599332/