您的足迹:首页 > Android >Android Dagger2 笔记1 简单入门使用

Android Dagger2 笔记1 简单入门使用

实体类 :LocationInfo.class、UserInfo.class,LocationInfo 模拟第三方不方便更改源码的实例,来演示Module功能
public class LocationInfo {

    public LocationInfo(){
        AWLogUtils.e(" ---create LocationInfo:"+this);
    }
}
public class UserInfo {

    @Inject
    public UserInfo(){
        AWLogUtils.e(" --- UserInfo created:" +this);
    }
}
@Component(modules = {LoginModule.class})
public interface LoginComponent {
    void inject(LoginActivity loginActivity);
}
@Module
public class LoginModule {
    @Provides
    LocationInfo provideLocationInfo(){
        LocationInfo locationInfo=new LocationInfo();
        AWLogUtils.e("--- provideLocationInfo---:"+locationInfo);
        return locationInfo;
    }

}
public class LoginActivity extends AppCompatActivity {
    @Inject
    UserInfo userInfo;

    @Inject
    UserInfo userInfo2;

    @Inject
    LocationInfo locationInfo;

    @Inject
    LocationInfo locationInfo2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch);


        DaggerLoginComponent.builder().loginModule(new LoginModule()).build().inject(this);

        AWLogUtils.e(" userInfo:"+userInfo);
        AWLogUtils.e(" userInfo2:"+userInfo2);
        AWLogUtils.e(" locationInfo:"+locationInfo);
        AWLogUtils.e(" locationInfo2:"+locationInfo2);
    }
}
运行结果:
02-27 06:51:14.508 E/AWLogUtils:  --- UserInfo created:com.a1anwang.chihuo.entity.UserInfo@78785cf
02-27 06:51:14.508 E/AWLogUtils:  --- UserInfo created:com.a1anwang.chihuo.entity.UserInfo@c933b5c
02-27 06:51:14.508 E/AWLogUtils:  ---create LocationInfo:com.a1anwang.chihuo.entity.LocationInfo@bee7e65
02-27 06:51:14.508 E/AWLogUtils: --- provideLocationInfo---:com.a1anwang.chihuo.entity.LocationInfo@bee7e65
02-27 06:51:14.508 E/AWLogUtils:  ---create LocationInfo:com.a1anwang.chihuo.entity.LocationInfo@62cc73a
02-27 06:51:14.509 E/AWLogUtils: --- provideLocationInfo---:com.a1anwang.chihuo.entity.LocationInfo@62cc73a
02-27 06:51:14.509 E/AWLogUtils:  userInfo:com.a1anwang.chihuo.entity.UserInfo@78785cf
02-27 06:51:14.509 E/AWLogUtils:  userInfo2:com.a1anwang.chihuo.entity.UserInfo@c933b5c
02-27 06:51:14.509 E/AWLogUtils:  locationInfo:com.a1anwang.chihuo.entity.LocationInfo@bee7e65
02-27 06:51:14.509 E/AWLogUtils:  locationInfo2:com.a1anwang.chihuo.entity.LocationInfo@62cc73a

以上结果看出:产生的实体类对象 地址各不相同。

修改代码,UserInfo.class加入@Singleton注解:

@Singleton
public class UserInfo {

    @Inject
    public UserInfo(){
        AWLogUtils.e(" --- UserInfo created:" +this);
    }
}
Component也加入@Singleton注解:
@Singleton
@Component(modules = {LoginModule.class})
public interface LoginComponent {
    void inject(LoginActivity loginActivity);
}
Module里面的provide方法也加入@Singleton注解:
@Module
public class LoginModule {

    @Singleton
    @Provides
    LocationInfo provideLocationInfo(){
        LocationInfo locationInfo=new LocationInfo();
        AWLogUtils.e("--- provideLocationInfo---:"+locationInfo);
        return locationInfo;
    }

}
再次运行,结果:
02-27 07:01:59.350 E/AWLogUtils:  --- UserInfo created:com.a1anwang.chihuo.entity.UserInfo@78785cf
02-27 07:01:59.350 E/AWLogUtils:  ---create LocationInfo:com.a1anwang.chihuo.entity.LocationInfo@c933b5c
02-27 07:01:59.350 E/AWLogUtils: --- provideLocationInfo---:com.a1anwang.chihuo.entity.LocationInfo@c933b5c
02-27 07:01:59.350 E/AWLogUtils:  userInfo:com.a1anwang.chihuo.entity.UserInfo@78785cf
02-27 07:01:59.351 E/AWLogUtils:  userInfo2:com.a1anwang.chihuo.entity.UserInfo@78785cf
02-27 07:01:59.351 E/AWLogUtils:  locationInfo:com.a1anwang.chihuo.entity.LocationInfo@c933b5c
02-27 07:01:59.351 E/AWLogUtils:  locationInfo2:com.a1anwang.chihuo.entity.LocationInfo@c933b5c

可以看到实体类是单例了,但是此时的单例只对这个LoginActivity有效,不是全局(整个application)的单例,单例是基于Component的,所以不仅 Provides 的地方要加 @Singleton,Component上也需要加。并且如果有另外一个OtherActivity,并且创建了一个LoginComponent,也注入UserInfo,这个时候 LoginActivity和OtherActivity中的UserInfo是不构成单例的,因为它们的Component是不同的。

带有参数的依赖对象

假如LocationInfo 构造方法需要参数 :


public class LocationInfo {

    private Context context;
    public LocationInfo(Context context){
        this.context=context;
        AWLogUtils.e(" ---create LocationInfo:"+this);
    }
    public void doSomething(){
       // context......
    }
}
注入的时候只有一个 @Inject 注解,并不能带参数。所以需要在 Module 中提供context,并且由 providesXXX 函数自己去构造:
public class LocationInfo {

    private Context context;
    public LocationInfo(Context context){
        this.context=context;
        AWLogUtils.e(" ---create LocationInfo:"+this);
    }
    public void doSomething(){
       // context......
    }
}
@Module
public class LoginModule {

    Context context;

    public LoginModule(Context context){
        this.context=context;
    }

    @Provides
    Context provideContext(){
        AWLogUtils.e("--- provideContext---");
        return this.context;
    }

    @Singleton
    @Provides
    LocationInfo provideLocationInfo(Context context){
        LocationInfo locationInfo=new LocationInfo(context);
        AWLogUtils.e("--- provideLocationInfo---:"+locationInfo);
        return locationInfo;
    }

}
public class LoginActivity extends AppCompatActivity {
    @Inject
    UserInfo userInfo;

    @Inject
    UserInfo userInfo2;

    @Inject
    LocationInfo locationInfo;

    @Inject
    LocationInfo locationInfo2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch);


        DaggerLoginComponent.builder().loginModule(new LoginModule(getApplicationContext())).build().inject(this);

        AWLogUtils.e(" userInfo:"+userInfo);
        AWLogUtils.e(" userInfo2:"+userInfo2);
        AWLogUtils.e(" locationInfo:"+locationInfo);
        AWLogUtils.e(" locationInfo2:"+locationInfo2);
    }
}
运行结果如下:
02-27 07:38:30.423 E/AWLogUtils:  --- UserInfo created:com.a1anwang.chihuo.entity.UserInfo@78785cf
02-27 07:38:30.423 E/AWLogUtils: --- provideContext---
02-27 07:38:30.423 E/AWLogUtils:  ---create LocationInfo:com.a1anwang.chihuo.entity.LocationInfo@c933b5c
02-27 07:38:30.423 E/AWLogUtils: --- provideLocationInfo---:com.a1anwang.chihuo.entity.LocationInfo@c933b5c
02-27 07:38:30.423 E/AWLogUtils:  userInfo:com.a1anwang.chihuo.entity.UserInfo@78785cf
02-27 07:38:30.423 E/AWLogUtils:  userInfo2:com.a1anwang.chihuo.entity.UserInfo@78785cf
02-27 07:38:30.423 E/AWLogUtils:  locationInfo:com.a1anwang.chihuo.entity.LocationInfo@c933b5c
02-27 07:38:30.423 E/AWLogUtils:  locationInfo2:com.a1anwang.chihuo.entity.LocationInfo@c933b5c
全局单例,Component依赖等其他用法请继续看下篇
本博客所有文章如无特别注明均为原创。作者:AlanWang复制或转载请以超链接形式注明转自 AlanWang的博客-专注android和蓝牙BLE技术分享
原文地址《Android Dagger2 笔记1 简单入门使用

相关推荐

发表评论

路人甲 表情
看不清楚?点图切换 Ctrl+Enter快速提交

网友评论(1)

学习了
太仓人才网 6个月前 (2020-03-26) 回复