Qualifier即Named
当module的@Provides提供相同变量的不同属性时:用于区分把哪一个初始化
Module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public class {
@Blue
@Provides
public Cloth getBluecloth(){
Cloth mCloth=new Cloth();
mCloth.setColor("蓝");
return mCloth;
}
@Named("Red")
@Provides
public Cloth getRedcloth(){
Cloth mCloth=new Cloth();
mCloth.setColor("红");
return mCloth;
}
}
|
Component
1
2
3
4
|
@Component(modules=ClothModule.class)
public interface ClothCompetent {
void inject(MainActivity mainActivity);
}
|
Activity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public class MainActivity extends AppCompatActivity {
TextView mTextView;
@Named("Red")
@Inject
Cloth mClothRed;
@Blue
@Inject
Cloth mClothBlue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView= (TextView) findViewById(R.id.tv1);
ClothCompetent mClothCompetent=DaggerClothCompetent.builder().clothModule(new ClothModule()).build();
mClothCompetent.inject(this);
mTextView.setText(mClothRed+"n"+mClothBlue);
}
}
|
Qualifier
自定义的Qualifier与官方的Named原理一样
1
2
3
4
|
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface Blue {
}
|
官方的Named
1
2
3
4
5
6
7
8
|
@Qualifier
@Documented
@Retention(RUNTIME)
public @interface Named {
String value() default "";
}
|
Scope即Singleton
以Component为依据,在指定范围内的单例
Module
- 在@Provides为外界提供Jacket时,参数中用到了Cloth,必须在Module中@Provides为Jacket提供Cloth
- 自定义的Qualifier和Named也可以在参数中使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public class JacketModule {
@Provides
public Jacket getJacket(@Named("Red") Cloth cloth){
return new Jacket(cloth);
}
@Singleton
@Named("Red")
@Provides
public Cloth getRedCloth(){
Cloth mCloth=new Cloth();
mCloth.setColor("红");
return mCloth;
}
@Blue
@Provides
public Cloth getBluecloth(){
Cloth mCloth=new Cloth();
mCloth.setColor("蓝");
return mCloth;
}
}
|
Component
在用到的Cloth和Component上同时添加@Singleton,此时Cloth为单例
1
2
3
4
5
|
@Singleton
@Component(modules = JacketModule.class)
public interface JacketComponent {
void inject(MainActivity mainActivity);
}
|
Activity
此时的Cloth和JacketRed.getCloth()为同一个
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public class MainActivity extends AppCompatActivity {
TextView mTextView;
@Named("Red")
@Inject
Cloth mClothRed;
@Inject
Jacket mJacketRed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView= (TextView) findViewById(R.id.tv1);
JacketComponent mJacketComponent = DaggerJacketComponent.builder().jacketModule(new JacketModule()).build();
mJacketComponent.inject(this);
mTextView.setText((mJacketRed.getCloth() == mClothRed) + "");
}
}
|
Scope
自定义的Scope与官方的Singleton原理一样,替换Singleton为JacketSingleton仍返回true
自定义的JacketSingleton:在JacketSingleton作用域内单例
1
2
3
4
|
@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface JacketSingleton {
}
|
官方的Singleton
1
2
3
4
|
@Scope
@Retention(RUNTIME)
public @interface Singleton {}
|
dependencies
实例:分别创建两个Activity,跳转后的实例为同一个(工具类多此用法在app层单例)
分别创建两个Module
JacketModule与JacketModule2
创建Component
方法一:dependencies
1
2
3
4
5
|
@JacketSingleton
@Component(modules = JacketModule.class,dependencies = BaseComponent.class)
public interface JacketComponent {
void inject(MainActivity mainActivity);
}
|
方法二:Subcomponent
1
2
3
4
5
|
@JacketSingleton
@Subcomponent(modules = JacketModule2.class)
public interface JacketComponent2 {
void inject(MainActivity2 mainActivity);
}
|
创建BaseModule
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class BaseModule {
@Singleton
@Provides
public Jacket getJacket(@Named("Red") Cloth cloth){
return new Jacket(cloth);
}
@Singleton
@Provides
@Named("Red")
public Cloth getRedCloth(){
Cloth mCloth=new Cloth();
mCloth.setColor("红");
return mCloth;
}
}
|
创建BaseComponent
1
2
3
4
5
6
|
@Singleton
@Component(modules = BaseModule.class)
public interface BaseComponent {
Jacket getJacket();//dependencies依赖声明的方式
JacketComponent2 getJacketComponent2(JacketModule2 jacketModule2);//@Subcomponent使用的声明方式,声明一个返回值为子组件的方法,子组件需要什么Module,就在方法参数中添加什么
}
|
在app中初始化BaseComponent
1
2
3
4
5
6
7
8
9
10
11
12
|
public class App extends Application {
private BaseComponent baseComponent;
@Override
public void onCreate() {
super.onCreate();
baseComponent = DaggerBaseComponent.builder().baseModule(new BaseModule()).build();
}
public BaseComponent getBaseComponent() {
return baseComponent;
}
}
|
在两个Activity中使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public class MainActivity extends AppCompatActivity {
TextView mTextView;
@Named("Red")
@Inject
Cloth mClothRed;
@Inject
Jacket mJacketRed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView= (TextView) findViewById(R.id.tv1);
JacketComponent mJacketComponent = DaggerJacketComponent.builder().baseComponent(((App) getApplication()).getBaseComponent()).jacketModule(new JacketModule()).build();
mJacketComponent.inject(this);
mTextView.setText(mJacketRed.hashCode()+"");
}
public void go(View view) {
Intent intent = new Intent(this,MainActivity2.class);
intent.putExtra("xx",mJacketRed.hashCode()+"");
startActivity(intent);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public class MainActivity2 extends AppCompatActivity {
TextView mTextView;
@Named("Red")
@Inject
Cloth mClothRed;
@Inject
Jacket mJacketRed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
mTextView= (TextView) findViewById(R.id.tv2);
App application = (App) getApplication();
application.getBaseComponent().getJacketComponent2(new JacketModule2()).inject(this);
String mString = (String) getIntent().getExtras().get("xx");
mTextView.setText(mString+"n"+mJacketRed.hashCode()+"");
}
}
|
结论
分别在两个Activity中初始化的Jacket哈希值相同,为同一个变量,app层单例。
Lazy与Provider
Lazy用于延迟加载,所谓的懒加载就是当你需要用到该依赖对象时,Dagger2才帮你去获取一个;Provide用于强制重新加载,也就是每一要用到依赖对象时,Dagger2都会帮你依赖注入一次
1
2
3
4
|
@Inject //Lazy声明方式
Lazy<Cloth> redCloth;
@Inject //Provider声明方式
Provider<Shoe> shoe;
|
- Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier、@scope
以下内容摘自部分网友的,并加上了自己的理解 @Service用于标注业务层组件(我们通常定义的service层就用这个) @Controller用于标注控制层组件(如struts中的action.Sp ...
- 【从零开始撸一个App】Dagger2
Dagger2是一个IOC框架,一般用于Android平台,第一次接触的朋友,一定会被搞得晕头转向.它延续了Java平台Spring框架代码碎片化,注解满天飞的传统.尝试将各处代码片段串联起来,理清思 ...
- 深入理解spring中的各种注解
Spring中的注解大概可以分为两大类: 1)spring的bean容器相关的注解,或者说bean工厂相关的注解: 2)springmvc相关的注解. spring的bean容器相关的注解,先后有:@ ...
- 深入理解spring中的各种注解(转)
Spring中的注解大概可以分为两大类: 1)spring的bean容器相关的注解,或者说bean工厂相关的注解: 2)springmvc相关的注解. spring的bean容器相关的注解,先后有:@ ...
- JSR330: DI
JSR330 DI JSR 330 ,提供了一种可重用的.可维护.可测试的方式来获取Java对象.也称为Dependency Injection . DI应该都不陌生,因为它就是Spring core ...
- Spring注解简介
提供了基于注解(Annotation-based)的配置,我们可以通过注解的方式来完成注入依赖. 1. 使用注解方式配置 我们需要修改spring配置文件的头信息,修改部分红色标注,如下: <c ...
- Spring实战拆书--SpringBean
代码源码地址:https://github.com/wujiachengSH/springBeanDemo 概述:本章将讲解Spring对于Bean的管理方案. 目录: 准备工作 自动装配 处理装配歧 ...
- @Component 元注解
@Component 元注解 这是一个元注解,意思是它可以用于标注其他注解,被它标注的注解和它起到相同或者类似的作用.Spring用它定义了其他具有特定意义的注解如@Controller @Servi ...
- 解锁Dagger2使用姿势(二) 之带你理解@Scope
关于Dagger2使用的基础如果你还不了解,可以参考我的上一篇文章解锁Dagger2使用姿势(一),这有助于你理解本篇文章. OK,我们在上篇文章介绍另外Dagger2使用过程中四个基本的注解,分别是 ...
随机推荐
- Java实战——简介
由于下学期要学习JavaEE所以打算将JavaSE的知识再重新学习一遍,打好基础的同时也希望自己有新的收获和更深刻的理解. 这次复习主要是参考廖雪峰老师的java教程,每学习完一章对其中一些要点进行总 ...
- 吴裕雄--天生自然 PYTHON3开发学习:日期和时间
import time; # 引入time模块 ticks = time.time() print ("当前时间戳为:", ticks) import time localtime ...
- 使用DataSnap Server环境搭建注意的问题。
1.Data exploer 的MYSQL文件(Libmysql.dll)放到系统的system32目录即可
- ubuntu19.10——snap错误has install-snap change in progress
使用软件商店安装时遇到问题 snap has install-snap change in progress 原因是之前的安装错误终止,使得现在的安装无法进行,解决方案: 终端输入: snap cha ...
- P2486 [SDOI2011]染色 区间合并+树链剖分(加深对线段树的理解)
#include<bits/stdc++.h> using namespace std; ; struct node{ int l,r,cnt,lazy; node(,,,):l(l1), ...
- 比率(ratio)|帕雷托图|雷达图|轮廓图|条形图|茎叶图|直方图|线图|折线图|间隔数据|比例数据|标准分数|标准差系数|离散系数|平均差|异众比率|四分位差|切比雪夫|右偏分布|
比率是什么? 比率(ratio) :不同类别数值的比值 在中文里,比率这个词被用来代表两个数量的比值,这包括了两个相似却在用法上有所区分的概念:一个是比的值:另一是变化率,是一个数量相对于另一数量的变 ...
- 对xgboost中dump_model生成的booster进行解析
xgboost原生包中有一个dump_model方法,这个方法能帮助我们看到基分类器的决策树如何选择特征进行分裂节点的,使用的基分类器有两个特点: 二叉树: 特征可以重复选择,来切分当前节点所含的数据 ...
- 给本地web项目配置域名
给本地的web项目配置一个域名 通常访问本地问项目时,使用localhost:port/projectname或者127.0.0.1:port/projectname来实现.我们可以通过配置tomca ...
- iOS简单音乐实现、React-Native完整项目、仿闲鱼京东列表分页、语音识别、网络加载过度动画等源码
iOS精选源码 iOS快速入手语音识别.听写.评测.播报 网络加载数据的过渡动画(仿简书网页) iOS 封装跑马灯和轮播效果 crash防护组件,适用常见常用的数组,字典等crash保护 iOS:高仿 ...
- Xshell中使用xftp怎么选择默认编辑器,如nodepad
工具-选项-高级-编辑器路径