- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要为一个新项目学习 Dagger 2,并且我正在努力理解这一切。
我看过一些教程,这些教程提供了一些清晰度,但我仍然对很多内容感到困惑,例如各种移动部分(组件、模块、注入(inject)器、提供程序)如何相互关联。
我在想,也许如果有人可以向我展示使用 Kodein 进行依赖注入(inject)的以下代码的 Dagger 等效实现,那将有助于弥合我在理解上的差距:
注入(inject).kt
fun depInject(app: Application): Kodein {
return lazy {
bind<Application>() with instance(app)
bind<Context>() with instance(app.applicationContext)
bind<AnotherClass>() with instance(AnotherClass())
}
}
BaseApplication.kt
class BaseApplication: Application() {
companion object {
lateinit var kodein: Kodein
private set
}
override fun onCreate() {
super.onCreate()
kodein = depInject(this)
}
}
然后在我需要注入(inject)的任何地方我只使用:
private val context: Context by BaseApplication.kodein.instance()
谢谢!
最佳答案
fun depInject(app: Application): AppComponent {
return lazy {
DaggerAppComponent.factory().create(app)
}
}
和
@Singleton
@Component(modules = [AppModule::class])
interface AppComponent {
fun context(): Context
@Component.Factory
interface Factory {
fun create(@BindsInstance app: Application): AppComponent
}
}
@Module
object AppModule {
@Provides
@JvmStatic
fun context(app: Application): Context = app.applicationContext
}
然后
class BaseApplication: Application() {
companion object {
lateinit var component: AppComponent
private set
}
override fun onCreate() {
super.onCreate()
component = depInject(this)
}
}
和
private val context: Context by lazy { BaseApplication.component.context() }
编辑:
@Singleton class AnotherClass @Inject constructor() {
}
@Singleton
@Component(/*...*/)
interface AppComponent {
...
fun anotherClass(): AnotherClass
...
}
关于android - 我很难理解 Dagger。有人能告诉我这个 Kodein 实现在 Dagger 中的样子吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57203985/
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
我有一个带有输入字段的表单,使用javascript,当用户输入超过2个字符时,它会在第一个输入字段下方创建相同的输入字段。其代码是: Optie 1: 1 && treated[this.na
这是我的: char userInput; int position; vector userVector(7); vector someVector(7,1); cin >> userInput;
尝试使用 typescript 和 redux 构建一个简单的 react crud 应用程序并遇到以下问题。我有一个具有指定签名的函数,它将一个人对象作为参数,如此处所示。 export defau
哦,我多么希望 TCP 像 UDP 一样基于数据包! [查看评论] 但是,唉,事实并非如此,所以我正在尝试实现我自己的数据包层。这是到目前为止的事件链(忽略写入数据包) 哦,我的数据包结构非常简单:两
我想在我的页面底部放置一个包含不同数量图片的栏,这些图片(如果比页面宽)可以左右滚动。 页面宽度在变化,我希望 Pane 的宽度为 100%。 我试图通过让中间的 div 溢出并使用 jquery.a
我曾尝试在工作时将我的 Rails 应用程序 bundle 到我的 Mac 上。在家里它运行良好,我之前已经设法自己解决了它,但这次无论我尝试什么似乎都无法解决它。 我在运行 bundle/bundl
所以我有一个旧的网络表单站点,并且正在努力使其更易于维护。把它扔掉并重写它不是一种选择。 IoC 显然是它首先得到的东西之一,但这给我留下了服务定位器模式和糟糕的品味,并且想知道它是否可以做得更好。
我是一名优秀的程序员,十分优秀!