Why does Dagger not allow multiple qualifier annotations per element?

StackOverflow https://stackoverflow.com/questions/23278686

  •  09-07-2023
  •  | 
  •  

سؤال

Say I have two services AService and BService, both of which require an api key.

So in my modules, I can't do something like:

@Provides @Singleton @A @ApiKey String provideAKey() { return "a"; }
@Provides @Singleton @B @ApiKey String provideBKey() { return "b"; }

(Dagger would complain with "Only one qualifier annotation is allowed per element"). Instead what I have to do, is define two separate qualifiers for each combination: @ApiKeyA and @ApiKeyB.

For a service with multiple dependencies, (think network client, request headers, etc.) it gets cumbersome to define these qualifiers for each combination, rather than simply combine different annotations.

Is there a reason why this explicitly disallowed?

هل كانت مفيدة؟

المحلول

It's to simplify Dagger's implementation, and to make it faster.

نصائح أخرى

Please see this issue: JSR330 forbids to have have multiple qualifier annotations.

For more detail, this discussion might be helpful.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top