dagger2系列之依赖方式dependencies、包含方式(从属方式)SubComponent
本篇是实战文章,从代码的角度分析这两种方式。本文参考自下列文章:
http://www.jianshu.com/p/1d42d2e6f4a5
http://www.jianshu.com/p/94d47da32656
一般在项目中我们需要全局使用app或者其他,这个时候就需要有一个全局的Component作为桥梁,提供给使用方使用。一个Component可以依赖一个或多个Component,并拿到被依赖Component 暴露 出来的实例。
<一>依赖关系
全局提供App实例类、上下文Context
@Module
public class AppModule5 { private App5 app; public AppModule5(App5 app) {
this.app = app;
} @Provides
public App5 provideApp() {
return app;
} @Provides
Context provideApplicationContext() {
return app.getApplicationContext();
}
}
提供 全局提供App实例类、上下文Context桥梁,该Component会被其他Component依赖
@Component(modules = {AppModule5.class})
public interface AppComponent5 {
void inject(App5 app);
//因为AppComponent会被dependencies,所以此处Module中提供的内容,我们需要在此处声明一下
App5 getApp();
Context getApplicationContext();
}
由于AppComponent5会被其他Component依赖,所以此处需要显示地声明AppModule5中提供的东西,此处的方法名任意!!!
App被全局提供
public class App5 extends Application {
private AppComponent5 mAppComponent5;
@Override
public void onCreate() {
super.onCreate();
mAppComponent5 = DaggerAppComponent5.builder().appModule5(new AppModule5(this)).build();
mAppComponent5.inject(this);
}
public AppComponent5 getAppComponent5(){
return mAppComponent5;
}
}
若Module的构造器需要参数,我们就需要在这里appModule5(new AppModule5()),若Module的构造器中不需要参数,此处可以省略!!!需要通过这种方式将参数传递进去。
提供被依赖类:
@Module
public class ActivityModule5 { @Provides
public DependencyPresenter getDependencyPresenter(App5 app){
return new DependencyPresenter(app);
}
}
桥梁:
@Component(dependencies = AppComponent5.class,modules = ActivityModule5.class)
public interface ActivityComponent {
void inject(TargetActivity5 activity5);
}
使用了Component的dependencies属性,ActivityModule5提供被依赖类,AppComponent5提供被依赖类的参数(全局提供)。
被依赖类:此处没用@inject修饰构造器
public class DependencyPresenter {
public DependencyPresenter(App5 app){
Log.d("Dagger.class","DependencyPresenter-----构造器------(app == null)?????:"+(app == null));
}
public void printMethod(){
Log.d("Dagger.class","DependencyPresenter-----printMethod()-----");
}
}
目标类
public class TargetActivity5 extends AppCompatActivity {
@Inject
DependencyPresenter mDependencyPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
App5 app5 = (App5) this.getApplication();
DaggerActivityComponent.builder().appComponent5(app5.getAppComponent5()).build().inject(this);
mDependencyPresenter.printMethod();
}
}
由于ActivityComponent依赖AppComponent,所以需要在此处添加声明appComponent(app5.getAppComponent)。
由此得到结论:需要在Component中显示的声明提供 被依赖类 的方法(方法名任意)
1 同一个Component的两个Module之间,当ModuleA需要ModuleB提供参数,在Component中需要提供获取参数的方法
2 ComponentA和它所依赖的ComponentB之间,若ComponentA需要ComponentB提供参数,在ComponentB中需要显示声明方法
<二>包含方式(从属方式)@SubComponent
当我们的Component需要依赖全局的Component提供的实例或者参数的时候,我们上面的实现方式是选择Component的dependencies属性,此时需要在被依赖的Component中有显示的声明。这里介绍另一种方法,包含方式。相比于依赖方式,包含方式不需要在父Component中显示的提供方法,就可以拿到想要的东西。下面看一下代码:
使用@Subcomponent注解标注ActivityModule6,代表它是子Component
@Subcomponent(modules = ActivityModule6.class)
public interface ActivityComponent6 {
void inject(TargetActivity6 activity5);
}
父Component:
@Component(modules = {AppModule6.class})
public interface AppComponent6 {
void inject(App6 app);
ActivityComponent6 getSubComponent();
}
需要在父Component中声明自己的子Component,方法名任意。如果ActivityComponent6中的Module的构造器需要参数,则在此处传递进去,譬如:
ActivityComponent6 getSubComponent(new ActivityModule6("参数"));
目标类:使用方式上也有区别
public class TargetActivity6 extends AppCompatActivity {
@Inject
DependencyPresenter6 mDependencyPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
App6 app6 = (App6) this.getApplication();
app6.getAppComponent6().getSubComponent().inject(this);
mDependencyPresenter.printMethod();
}
}
其他类均没有变化。依赖方式、包含方式介绍到此为止~更多的内容请参看上面的链接!!!
dagger2系列之依赖方式dependencies、包含方式(从属方式)SubComponent的更多相关文章
- Spring系列之依赖注入的方式
一.依赖注入方式 对于spring配置一个bean时,如果需要给该bean提供一些初始化参数,则需要通过依赖注入方式,所谓的依赖注入就是通过spring将bean所需要的一些参数传递到bean实例对象 ...
- Spring系列之aAOP AOP是什么?+xml方式实现aop+注解方式实现aop
Spring系列之aop aop是什么?+xml方式实现aop+注解方式实现aop 什么是AOP? AOP为Aspect Oriented Programming 的缩写,意识为面向切面的编程,是通过 ...
- 【Xamarin 挖墙脚系列:IOS 开发界面的3种方式】
原文:[Xamarin 挖墙脚系列:IOS 开发界面的3种方式] xcode6进行三种基本的界面布局的方法,分别是手写UI,xib和storyboard.手写UI是最早进行UI界面布局的方法,优点是灵 ...
- 依赖注入之unity(winform方式)
依赖注入之unity(winform方式) 要讲unity就必须先了解DI和IOC及DIP,如下链接提供DI和IOC的基础:https://www.cnblogs.com/zlp520/p/12015 ...
- 万恶技术系列笔记-jupyter工作路径和源文件打开方式
万恶技术系列笔记-jupyter工作路径和源文件打开方式 脚本文件,ipynb的正确打开姿势: ipynb不能直接打开,需要复制到工作路径.例如 10_monkeys_model_1.ipynb ...
- Java Array数组 遍历 四种方式(包含 Lambda 表达式遍历)
示例代码如下: package com.miracle.luna.lambda; import java.util.Arrays; /** * @Author Miracle Luna * @Date ...
- Java List集合 遍历 四种方式(包含 Lambda 表达式遍历)
示例代码如下: package com.miracle.luna.lambda; import java.util.ArrayList; import java.util.List; /** * @A ...
- Java Map集合 遍历 五种方式(包含 Lambda 表达式遍历)
示例代码如下: package com.miracle.luna.lambda; import java.util.HashMap; import java.util.Iterator; import ...
- Gradle学习系列之七——依赖管理
在本系列的上篇文章中,我们讲到了如何使用java Plugin,在本篇文章中,我们将讲到Gradle的依赖管理. 请通过以下方式下载本系列文章的Github示例代码: git clone https: ...
随机推荐
- wordpress多站点配置
wordpress作为全球第一的个人博客搭建平台一直在国内外有着较高的人气,从3.0版本开始就已经支持多站点的搭建.该功能可以让子站点运行主站点的程序,不需要再每个站点分别存放网站程序.最近更新的4. ...
- CSS 特殊属性介绍之 pointer-events
首先看一下 MDN 上关于 pointer-events 的介绍: CSS属性 pointer-events 允许作者控制特定的图形元素在何时成为鼠标事件的 target.当未指定该属性时,SVG 内 ...
- [C#] C# 知识回顾 - 你真的懂异常(Exception)吗?
你真的懂异常(Exception)吗? 目录 异常介绍 异常的特点 怎样使用异常 处理异常的 try-catch-finally 捕获异常的 Catch 块 释放资源的 Finally 块 一.异常介 ...
- windows环境redis主从安装部署
准备工作 下载windows环境redis,我下载的是2.4.5,解压,拷贝一主(master)两从(slaveof).主机端口使用6379,两从的端口分别为6380和6381, 我本地索性用6379 ...
- const let,console.log('a',a)跟console.log('a'+a)的区别
const 创建一个只读的常量 let块级作用域 const let重复赋值都会报错 console.log('a',a) a console.log('a'+a) a2 逗号的值会有空格:用加号的值 ...
- Dynamics CRM 2015-Data Encryption激活报错
在CRM的日常开发中,Data Encryption经常是不得不开启的一个功能.但是有时,我们可能遇到一种情况,Organization导入之后,查看Data Encryption是已激活的状态,但是 ...
- swift 中关于open ,public ,fileprivate,private ,internal,修饰的说明
关于 swift 中的open ,public ,fileprivate,private, internal的区别 以下按照修饰关键字的访问约束范围 从约束的限定范围大到小的排序进行说明 open,p ...
- 搭建TFS 2015 Build Agent环境(一)
Download the build agent Downloading the build agent is really simple. Navigate to your TFS control ...
- 我将系统从Windows迁移至Linux下的点点滴滴
一.写在最前 由于本人的技术水平有限,难免会出现错误.本文对任何一个人有帮助都是我莫大的荣幸,任何一个大神对我的点拨,我都会感激不尽. 二.技术选型 在2013年8月低的时候,公司中了XXX市场监督局 ...
- CSharpGL(34)以从零编写一个KleinBottle渲染器为例学习如何使用CSharpGL
CSharpGL(34)以从零编写一个KleinBottle渲染器为例学习如何使用CSharpGL +BIT祝威+悄悄在此留下版了个权的信息说: 开始 本文用step by step的方式,讲述如何使 ...