Dagger是什么

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hongchangfirst/article/details/88962002

Dagger是一种对Java和Android的静态的,编译时依赖注入(Dependency Injection)框架。

它主要用来解决很多由于大量使用反射reflection而带来的开发和性能问题。

Dagger可以用来帮你来建立之前需要写的Factory class。它主要用来代码生成。

Dagger也是根据@Inject注解来进行代码生成的。

Classes that lack @Inject annotations cannot be constructed by Dagger.

当有些时候@Inject有点不太够用的时候,Dagger可以使用@Provides,所有的Provides method必须在同一个Module里。

@Module
class DripCoffeeModule {
  @Provides static Heater provideHeater() {
    return new ElectricHeater();
  }

  @Provides static Pump providePump(Thermosiphon pump) {
    return pump;
  }
}

猜你喜欢

转载自blog.csdn.net/hongchangfirst/article/details/88962002