依赖关系图的构建是否在Dagger 2中的编译时或运行时发生?>

问题描述 投票:0回答:1

关于Dagger 2,我有一个愚蠢的问题要问。因为我想确定。我知道在编译时,Dagger会生成代码。但是对于以下代码:

class MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()
          //build main component along with core component
         mainComponent = DaggerMainComponent
                .builder()
                .myModule(MyModule())
                .coreComponent(DaggerCoreComponent.builder().build())
                .build()
     }
}

Dagger是否在编译时或运行时调用build()来构建组件以绘制依赖关系图?我的理解是在运行时,对吗?

如果上面我是正确的,那么下面的代码呢?:

@Module(subcomponents = MySubComponent.class)
abstract class MyModule {
  ...
}

我的理解是,使用Dagger会自动检测是否请求了MySubComponent,如果不是,它不会生成MySubComponent中托管的依赖项。但是,此过程是在运行时还是在编译时进行?

关于Dagger 2,我有一个愚蠢的问题要问。因为我想确定。我知道在编译时,Dagger会生成代码。但是对于以下代码:类MyApplication:Application(){...

android dagger-2
1个回答
0
投票

依赖关系图是在编译时通过注释处理构建的。 Dagger将根据您添加到类中的注释(例如DaggerMainComponent@Module@Component等),从您的示例生成类似@Inject的类。

我建议您阅读有关注释处理的文章,例如one

© www.soinside.com 2019 - 2024. All rights reserved.