架构进阶,Dagger2的原理及使用详解

架构进阶,Dagger2的原理及使用详解

https://www.jianshu.com/p/04d9db541155


解决Android 8.0和9.0无法获取SSID (unknow ssid)
https://blog.csdn.net/weixin_39706415/article/details/84571421

@Named @Scope @Inject  @Provides注解提供,但是呢,这个@Provides只能在固定的模块中,也就是@Module注解,我们查找的时候,不是直接去找模块,而是去找@Component

->@Module

Activity / Fragment ->Component-> Module->Providers注解,去寻找A的实例化对象。

Providers注解,去寻找A的实例化对象。

->annotationProcessor compiler

@Module
MainModule


 //第二步 使用Provider 注解 实例化对象
 @Provides
void inject (MainActivity activity)


将Component与Activity/Fragment绑定关系


DaggerMainComponent.create.injectthis

3. 高级用法
(1)构造方法需要其他参数时候

@Module(includes = {45})


dependencies 依赖其他Component

一个Component 应用多个 module

dependencies 依赖其他Component

@Named注解使用


DaggerMainComponent.builder
.build.inject
(4) @Singleton注解

module Component 

@Singleton
@Component
单利对象只能在同一个Activity中有效。不同的Activity 持有的对象不同


Scoped


Retention RUNTIME


DaggerMainComponent
.builder
.mainModule 
.build  
.add 
.inject 
this
PresentForName

lazy.get provider.get

其中Lazy(懒加载)的作用

Lazy component present  get

procider(强制加载)

@Singleton
@Component ()


SplashPresenter,也就是启动页的Presneter,业务逻辑

SplashPresenter ,也就是启动页的Presneter,业务逻辑

@Provides

依赖注入框架 ----Dagger2 使用详解及源码分析
https://blog.csdn.net/zengke1993/article/details/80591427

依赖注入:
IoC不是一种技术

@Inject

Dagger就是一款基于Java注解实现完全在编译阶段完成依赖注入的开源库,

主要用于模块间解耦,提高代码的健壮性和可维护性。

Java注解实现  编译阶段完成 依赖注入


@Inject标记了的变量提供依赖。 @Module 

@Module : 用于标记提供依赖的类(可以理解为仓库类,工厂类)

@Module
@Provides-> @Module

Component

DaggerComputerComponent

@Qualifier

@Qulifer : 用于自定义注解。被依赖对象被注入到依赖对象中时,依赖对象对被依赖对象的创建过程是不关心的。

Qualifier 用于自定义注解。被依赖对象被注入到依赖对象中时,依赖对象对被依赖对象的创建过程是不关心的。


这时使用@Qulifer来定义自己的注解,然后通过自定义注解去标注提供依赖的方法和依赖需求方,这样就可以精准地拿到所需要的被依赖实例。

@Scoped 限定注解作用域 实现局部单例的。


@SingleTon
 

 

/使用@Inject标记 被依赖对象 Cpu的构造方法。 这是注入时就会通过@Inject找到该构造函数并把实例构造出来

@Inject

DaggerComputerComponent

@component
inject

   //注入被依赖对象
    @Inject
    Intel_CPU mCPU;
    
      public Computer() {
        //把依赖使用方传入到桥梁中
        DaggerComputerComponent.builder().build().inject(this);
    }
.builder.build
    

@Module @Provide


34被依赖对象 CPU :依赖使用方

 //这时构造函数已不需要使用@Inject来标记
    public CPU(String brand) {
        this.brand = brand;
    }
    @Inject
    
被依赖对象的仓库类,被@Module标记:


@Component modules = CpuModule.class

@Qualifier

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface QualifilerAmd {
}

 //使用   @QualifilerIntel标记拿到指定牌子的CPU
    @QualifilerIntel->使用@QualifierIntel标记拿到指定
    @Inject
    CPU mCPU;

    
    依赖使用方  Computer
    


那现在我要这个CPU是单例的,不管注入多少次,它只创建一次,这是就用到了@Scope注解。
@Scope是通过限定作用域,实现局
23部单例的。

首先我们需要通过@Scope定义一个CPUScope注解:

CPUScope注解

@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface CPUScope {

}
同时还需要使用@Scope来标注注入器Component:

@Scope

依赖对象Computer:

public class Computer {

    @Inject
    CPU cpu1;

    @Inject
    CPU cpu2;


    public Computer() {
        DaggerComputerComponent.builder().build().inject(this);
    }

    public void run() {
        cpu1.run();
        cpu2.run();
    }
}

@Retention (RetentionPolicy.RUNTIME)

@interface CpuScope

依赖对象Computer:


DaggerComputerComponent.builder().build.inject(this)


生成DaggerComputerComponent这个注入器,通过这个注入器把被依赖对象(CPU)注入到这个类中。


@CPUScope
@Component(34)
public @interface AppComponent {


}
//34

DaggerApplicationCompoment.builder().build

.cpuModule().build()

@SingleTon 就是一个现成的被@Scope标注的注解,用来限定作用域。使用和上面的@CPUScope一样。

46
@SingleTon->


当某个Module包含另外一个Module时,可以使用includes字段:
includes
@Module()
includes = 

Disk的提供方法的参数就可以使用DiskModule中提供的依赖对象DiskBrand:


@Module(includes = DiskBrandModule.class)

@Provides-

@QualifierAmd


栗子8:dependencies = dependencies


@QualifierA

被依赖的桥梁, ApplicationCpmponent:

.appComponent(MyApplication).inject

Android开发-RecyclerView控件高级的使用(含下拉刷新上拉加载分页)
https://blog.csdn.net/fukaimei/article/details/90446316


Computer_MembersInjector.in


  public static final class Builder {
    private CpuModule1 cpuModule;

    private Builder() {}

    public ComputerComponent build() {
    //实例化CpuModule
      if (cpuModule == null) {
        this.cpuModule = new CpuModule();
      }
      return new DaggerComputerComponent(this);
    }

    public Builder cpuModule(CpuModule cpuModule) {
      this.cpuModule = Preconditions.checkNotNull(cpuModule);
      return this;
    }
  }
}

Builder cpuModule->build  Builder return this

Computer_MembersInjector.injectMCPU


yinxiucheng
/
RecyclerBarChart

RecyclerBarChart
基于Recyclerview实现 动态X/Y轴的运动步数 柱状图

https://github.com/yinxiucheng/RecyclerBarChart

proxyProvideCPU

Factory<CPU>

CPU  get 
provideInstance  cpuModule

get  provideInstance create 

create
proxyProvideCPU
instance.provideCPU()

ComputeComponent,那么编译期间生成的实现类名为DaggerComputerComponent),那么你可以通过调用这个实现类的方法完成注入。


Computer_MembersInjector.injectMCPU

MembersInjector<Computer>


Provider<CPU>

create mCPUProvider

injectMembers instance

injectMCPU 
咦,这不就是对Computer的mCpu变量进行赋值操作吗。综上所述,CpuModule生成的实例就已经传递给Computer对象了。

DaggerComputerComponent.builder.build

inject


Android Studio Xml 文件无代码提示
https://juejin.cn/post/6844904117261828109

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值