Dagger2 Scope && Qualifier
博客专区 > lightUp 的博客 > 博客详情
Dagger2 Scope && Qualifier
lightUp 发表于1年前
Dagger2 Scope && Qualifier
  • 发表于 1年前
  • 阅读 19
  • 收藏 0
  • 点赞 0
  • 评论 0


@Scope annotation

    public [@interface](https://my.oschina.net/u/996807) ActivityScope {

Another part of JSR-330 standard. In Dagger 2 @Scope is used to define custom scopes annotations. In short they make dependencies something very similar to singletons. Annotated dependencies are single-instances but related to component lifecycle (not the whole application).

Now worth-mentioning is that all custom scopes does the same thing (from code perspective) - they keep single instances of objects. But also they are used in graph validation process which is helpful with catching graph structural problems as soon as possible.


@Qualifier annotation helps us to create “tags” for dependencies which have the same interface. Imagine that you need to provide two RestAdapter objects - one for Github API, another for facebook API. Qualifier will help you to identify the proper one:

Naming dependencies

@RestAdapter("github")  //Qualifier
RestAdapter provideRestAdapter() {
	return new RestAdapter.Builder()


@RestAdapter("facebook")  //Qualifier
RestAdapter provideRestAdapter() {
	return new RestAdapter.Builder()

and this the qualifier:

public @interface RestAdapter {

	/** The name. */
	String value() default "";

then you can inject the tagged objects:

RestAdapter githubRestAdapter;

RestAdapter facebookRestAdapter;

reference: Dependency injection with Dagger 2 - Custom scopes

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 10
博文 132
码字总数 287936
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
* 支付类型