背景
在前一篇 Daager2-初认识一 中我们认识了简单的Dagger2的使用和依赖注入的优点,这章继续深入的研究和学习Dagger2
的使用,后续会结合mvp
主流框架给大家一步步讲解如何运用dagger2封装大项目的开发框架!
Component提供依赖
上一节我们讲解了moudel的依赖方法,其实Component也可以提供依赖实现如下:
实现
1.创建一个新的数据对象,提供Component以来演示
public class Info {
private String msg;
}
2.通过传值创建对应的module对象
@Module
public class InfoModule {
private String name;
public InfoModule(String name) {
this.name = name;
}
@Provides
Info provideInfo(){
return new Info(name);
}
}
3.实现依赖的InfoComponent
因为InfoComponent 不需要注入activity使用,所以不用写对应的inject方法,直接提供Info 回调即可
@Component(modules = {InfoModule.class})
public interface InfoComponent {
Info infoMoudule();
}
对比
@Component(modules = UserModule.class)
public interface UserComponent {
void inject(MainActivity mainActivity);
}
4.将InfoComponent 提供依赖给UserComponent
使用dependencies 标识依赖Component 对象
@Component(modules = UserModule.class,dependencies = InfoComponent.class)
public interface UserComponent {
void inject(MainActivity mainActivity);
}
5.activity中实现依赖注入
和之前注入方法一样,不过这里多出了infoComponent方法(自动生成)提供component依赖,成功以后我们可以@Inject得到一个Info 对象
public class MainActivity extends AppCompatActivity {
@Inject
User user;
@Inject
Info info;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
InfoComponent infoComponent= DaggerInfoComponent.builder().infoModule(new InfoModule("AAAAA")).build();
UserComponent component= DaggerUserComponent.builder().userModule(new UserModule()).infoComponent(infoComponent).build();
component.inject(this);
TextView textView=(TextView)findViewById(R.id.tv);
textView.setText("name:"+user.getName()+"\nsex:"+user.getSex()+"\nads:"+user.getAds()+"\ninfo->>>"+info.getMsg());
}
}
效果
提升
到目前为止我们已经学习了两种依赖方式一种是moudel一种是component,基本已经满足了日常的使用,接下来我们提升学习一些更加有用的注解方法
@Scope用法
Scopes可是非常的有用,Dagger2可以通过自定义注解限定注解作用域;其实这里就是生命周期的 意思,我们可以通过@Scopes类定义一个componet或者module的生命周期,比如我们需要让依赖的对象和我们的activity生命周期一样,自动销毁
使用
通过@Scope我们定义了一个@interface PerApp注解,通过这个注解我们可以标示一个对象的生命周期!
@Scope
@Documented
@Retention(RUNTIME)
public @interface PerApp {
}
运用:
@PerApp
@Component(modules = UserModule.class,dependencies = InfoComponent.class)
public interface UserComponent {
void inject(MainActivity mainActivity);
}
@Singleton
@Singleton标示当前方法和对象是单利模式
@PerApp
@Singleton
@Component(modules = UserModule.class,dependencies = InfoComponent.class)
public interface UserComponent {
void inject(MainActivity mainActivity);
}
@Module
public class UserModule {
@Provides
@Singleton
User provideUser(){
return new User("xxxx","SEX","XXX@Gmail.com");
}
}
通过两章基础学习,我们已经掌握了dagger2的基本用法,在此基础上已经可以自由扩展我们的框架了;比如现在要封装一个这样的框架:我们通过dagger2依赖去构建一个component依赖,提供了全局的开发中的公用对象:图片管理器;缓存;服务;http请求…………..
我们可以通过component依赖注入到需要使用的activity中,实现dagger2全局依赖的效果!
大致结构
@Singleton
@Component(modules = {AppModule.class, ClientModule.class, ServiceModule.class, ImageModule.class, CacheModule.class})
public interface AppComponent {
Application Application();
//服务管理器,retrofitApi
ServiceManager serviceManager();
//缓存管理器
CacheManager cacheManager();
//Rxjava错误处理管理类
RxErrorHandler rxErrorHandler();
//用于请求权限,适配6.0的权限管理
RxPermissions rxPermissions();
OkHttpClient okHttpClient();
//图片管理器,用于加载图片的管理类,默认使用glide,使用策略模式,可替换框架
ImageLoader imageLoader();
//gson
Gson gson();
}
大家可以自由想象,自由设计,赶紧将dagger2运用到你的项目中吧!装逼开始了