技术标签: Inject Dagger2 Component
public class ApplicationBean {
private String name = null;
public ApplicationBean() {
name = "AppBean";
}
public String getAppBeanName() {
return name;
}
}
@Module
public class ApplicationModule {
//作为单例模式注入app
@Singleton
@Provides
ApplicationBean privoderAppBean() {
return new ApplicationBean();
}
}
@Singleton
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
void inject(DaggerApplication application);
//说明将BeanForApplication开放给其他Component使用
ApplicationBean providerAppBean();
}
在这里请注意两点:
public class DaggerApplication extends Application {
private ApplicationComponent mAppComponent;
@Inject
ApplicationBean mAppBean1;
@Inject
ApplicationBean mAppBean2;
@Override
public void onCreate() {
super.onCreate();
if (mAppComponent == null) {
mAppComponent = DaggerApplicationComponent.create();
}
mAppComponent.inject(this);
Log.d("Dagger", "Application mAppBean1:" + mAppBean1);
Log.d("Dagger", "Application mAppBean2:" + mAppBean2);
}
public ApplicationComponent getAppComponent() {
return mAppComponent;
}
}
在这里我们注入了两次ApplicationBean对象,并在注入完成后打印出它们的地址用于观察是否实现了单例的功能。
public class ActivityBean {
private String name = null;
public ActivityBean() {
}
public String getAppBeanName() {
return name;
}
}
@Module
public class ActivityModule {
@Provides
ActivityBean providerActivityBean() {
return new ActivityBean();
}
}
@ForActivity
@Component(dependencies = ApplicationComponent.class, modules = ActivityModule.class)
public interface ActivityComponent {
void inject(MainActivity activity);
void inject(MainActivity.OtherClass otherClass);
}
这个Component的写法有三处与之前的写法不同的地方:
@Scope
@Retention(RUNTIME)
public @interface ForActivity {
}
为什么要添加这个修饰呢?因为当前Component所继承的ApplicationComponent中包含Singleton的注释,
所以ApplicationComponent的子类Component的作用范围不能高于ApplicationComponent的作用范围,因此需要对ActivityComponent也添加Scope的限定。
public class MainActivity extends AppCompatActivity {
@Inject
ApplicationBean applicationBean1;
@Inject
ApplicationBean applicationBean2;
@Inject
ActivityBean activityBean;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DaggerApplication application = (DaggerApplication) getApplication();
ApplicationComponent applicationComponent = application.getAppComponent();
ActivityComponent activityComponent = DaggerActivityComponent.builder().applicationComponent(applicationComponent).build();
activityComponent.inject(this);
Log.d("Dagger", "Activity activityBean:" + activityBean);
Log.d("Dagger", "Activity applicationBean1:" + applicationBean1);
Log.d("Dagger", "Activity applicationBean2:" + applicationBean2);
OtherClass otherClass = new OtherClass();
}
class OtherClass {
@Inject
ApplicationBean applicationBean1;
@Inject
ApplicationBean applicationBean2;
@Inject
ActivityBean activityBean;
public OtherClass() {
DaggerApplication application = (DaggerApplication) getApplication();
ApplicationComponent applicationComponent = application.getAppComponent();
ActivityComponent activityComponent = DaggerActivityComponent.builder().applicationComponent(applicationComponent).build();
activityComponent.inject(this);
Log.d("Dagger", "OtherClass activityBean:" + this.activityBean);
Log.d("Dagger", "OtherClass applicationBean1:" + this.applicationBean1);
Log.d("Dagger", "OtherClass applicationBean2:" + this.applicationBean2);
}
}
}
@ForActivity
@Subcomponent(modules = ActivityModule.class)
public interface ActivityComponent {
void inject(MainActivity activity);
void inject(MainActivity.OtherClass otherClass);
}
它与之前的方式的区别有两点:
@Singleton
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
//注入DaggerApplication
void inject(DaggerApplication application);
//说明将BeanForApplication开放给其他Component使用
ApplicationBean providerAppBean();
ActivityComponent activityComponent();
}
这里的改造只是多了一句声明:ActivityComponent activityComponent()
DaggerApplication application = (DaggerApplication) getApplication();
ApplicationComponent applicationComponent = application.getAppComponent();
applicationComponent.activityComponent().inject(this);
然后就完成了所有改造,运行结果如下:
DaggerApplication application = (DaggerApplication) getApplication();
//获取ApplicationComponent对象
ApplicationComponent applicationComponent = application.getAppComponent();
//用ActivityComponent对象进行注入
ActivityComponent activityComponent = DaggerActivityComponent.builder().applicationComponent(applicationComponent).build();
activityComponent.inject(this);
Subcomponent方式:
DaggerApplication application = (DaggerApplication) getApplication();
ApplicationComponent applicationComponent = application.getAppComponent();
//用ApplicationComponent对象进行注入
applicationComponent.activityComponent().inject(this);
结果发现,dependencies方式中,我们最终调用的是ActivityComponent对象中的inject()方法,而Subcomponent方式中,我们最终调用的是ApplicationComponent的inject()方法。
//ApplicationComponent
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
......
}
//ActivityComponent
@Component(dependencies = ApplicationComponent.class, modules = ActivityModule.class)
public interface ActivityComponent {
......
}
Subcomponent方式:
//ApplicationComponent
@Component(modules = ApplicationModule.class)
public interface ApplicationComponent {
......
ActivityComponent activityComponent();
}
//ActivityComponent
@Subcomponent(modules = ActivityModule.class)
public interface ActivityComponent {
......
}
对比中我们发现,
dependencies中Component强调的是在子类Component依赖于某个Component(子类为主角),而Subcomponent中强调的则是在父类Component中提供某个子类的Component(父类为主角)。
至此,Dagger2系列介绍就全部结束,下课。
我们再次列出本系列所有的总结:
文章浏览阅读10w+次,点赞2.2k次,收藏1.4w次。1、Spring是什么?Spring是一个轻量级的IoC和AOP容器框架。是为Java应用程序提供基础性服务的一套框架,目的是用于简化企业应用程序的开发,它使得开发者只需要关心业务需求。常见的配置方式有三种:基于XML的配置、基于注解的配置、基于Java的配置。主要由以下几个模块组成:Spring Core:核心类库,提供IOC服务;Spring Context:提..._spring面试题
文章浏览阅读1.6w次,点赞3次,收藏21次。在使用Maven多模块结构工程时,配置版本是一个比较头疼的事。继承版本,依赖版本,自身版本,都需要单独定义,很是麻烦。版本号变更使用mvn versions:set,有时候也可能导致版本号不一致、不便于统一管理:mvn versions:set但其实Maven已经提供了这种CI版本的管理方式,下面来介绍具体用法。从Maven 3.5.0-beta-1版本开始,就可以使用${revision},${sha1}和${changelist}作为占位符来替换pom文件了。注意:Id..._reports that usage of properties in modules parent definition is prohibited
文章浏览阅读313次。结合上几个章节,我开始对《电子商城实录------项目目录的结构搭建及其说明2》中方法优化Framework.class.php代码加入static:<?php//核心启动类class Framework{public static function run(){echo "hello,wrold!";}//初始化方法private static function ..._网上购物系统项目目录结构
文章浏览阅读516次。HTML5期末大作业 精彩在线影视网站设计——在线影视(1页) HTML+CSS+JavaScript 学生DW网页设计作业成品 web课程设计网页规划与设计 计算机毕设网页设计源码常见网页设计作业题材有 个人、 美食、 公司、 学校、 旅游、 电商、 宠物、 电器、 茶叶、 家居、 酒店、 舞蹈、 动漫、 明星、 服装、 体育、 化妆品、 物流、 环保、 书籍、 婚纱、 军事、 游戏、 节日、 戒烟、 电影、 摄影、 文化、 家乡、 鲜花、 礼品、 汽车、 其他 等网页设计题目, A+水平作业_dw影视网站源码
文章浏览阅读3.7k次,点赞7次,收藏40次。目录1、问题描述开发环境我的程序配置2、问题造成的后果3、解决方案说明:4、实例,针对ORB_SLAM3问题5、其他参考解决方案6、另一个例程7、其他:修改系统默认链接的cv_bridge版本号,以及查看当前系统链接的cv_bridge版本号与位置与本篇问题相关的一个问题,可参考我之前的一篇博客:cv_bridge与python版本问题导致编译错误error: return-statement with no value, in function retur._sh_cv_bridge版本
文章浏览阅读891次,点赞3次,收藏5次。文章目录vscode常用插件中文插件网页运行标签自动更正Emmet常用语法快速生成html结构快速生成css样式快速格式化代码参考所谓“工欲善其事,必先利其器”,想要快速高效的完成开发,必须要借助一些开发工具,毕竟“搬砖”我们也是专业的!这里推荐一个不仅用于web前端开发也可以用于其他编程语言的“神器”–vscode。vscode优点:跨平台丰富插件支持多种编程语言支持Emmet语法,高效书写代码vscode常用插件安装不表,下面介绍几个web前端开发常用插件中文插件Chinese _打div自动生成标签是什么插件
文章浏览阅读561次。有关于使用matlab进行基础的文件读入输出,字符串处理_如何做项目笔记图片
文章浏览阅读1.2w次,点赞3次,收藏7次。数据类型long long是C++11中重新定义的,标准规定它最小是64bit在这之前为了提供超过32bit的整数,各个开发环境(编译器)分别定义了各自的64bit整数类型。这会导致代码不兼容现在,C++11直接定义了long long类型我猜许多人应该使用过这个类型,当然在C++11之前,这种尝试会被编译器无情拒绝,自C++11之后就不会在发生这样地情况了。因此我认为:在C++11新特性中,long long一定是最容易被接受的一个。多数程序员看到它时甚至不会意识到这是一个新特性。相应的,C++1_longlong c
文章浏览阅读1.4k次。习惯使用的 Markdown 的伙伴们应该知道,当需要加粗字体时,会首先输入。,也就是先键入**,后面紧接着输入需要加粗的文字,最后键入**。但是在 Notion 中,这个就不太行了。同样,行内公式、行内代码高亮、斜体等都是这个规则。,然后在里面填内容。_notion怎么写markdown
文章浏览阅读4.5k次,点赞2次,收藏3次。flask使用form表单报错:“KeyError: ‘A secret key is required to use CSRF.’”报错详情:KeyError: 'A secret key is required to use CSRF.'Traceback (most recent call last)FFile &quot;F:\Projects\flask_env\lib\site-pac..._keyerror: 'form
文章浏览阅读884次。绿色底色很烦,看不清文件夹的名字在.bashrc里加一行,LS_COLORS=$LS_COLORS:'ow=1;32:'这样即可取消有些文件夹的绿色底色。其中ow的意思是OTHER_WRITABLE1的意思是粗体,32的意思是绿色前景参考:编码 颜色/动作 0 重新设置属性到缺省设置 1 设置粗体 2 设置一半亮度(模拟彩色显示器的颜色) 4 设置下划线(模拟彩色显示器的颜色) 5 设置闪烁 7 设置反_ubuntu中如何不显示绿色
文章浏览阅读7.5k次,点赞3次,收藏7次。一、安装与配置使用pip安装包:$ pip install django-celery-beat将django_celery_beat模块添加到INSTALLED_APPSDjango项目中settings.py:#jdango时区配置# 官方用来修复CELERY_ENABLE_UTC=False and USE_TZ = False 时时间比较错误的问题;# 详情见:https://github.com/celery/django-celery-beat/pull/216/file_django-celery-beat