是否可以在Dagger 2中传播注释?

时间:2017-10-26 10:10:03

标签: dagger-2

我有这些课程:

class Storage {
  @Qualifier
  @Retention(RetentionPolicy.SOURCE)
  @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
  public @interface Songs {}

  @Qualifier
  @Retention(RetentionPolicy.SOURCE)
  @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
  public @interface Movies {}

  @Inject
  public Storage(
    @Songs Blobstore songsStore,
    @Movies Blobstore moviesStore) {
// ...
}

class Blobstore {
  @Inject
  public Blobstore(File path) {
// ...

仅提供路径似乎是合理的:

@Provides
@Songs
public static File songPath() { return new File("/sdcard/songs"); }
@Provides
@Movies
public static File moviePath() { return new File("/sdcard/movies"); }

但如果我这样做,我会收到错误消息:File is bound multiple times。有办法解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

您的@Qualifier注释need to be runtime-retained to comply with JSR-330(强调我的):

  

限定符注释:

     
      
  • 使用@Qualifier, @Retention(RUNTIME)进行注释,通常是@Documented。
  •   
  • [...]
  •   

虽然Dagger代码生成发生在编译时,但它也需要符合JSR-330,这意味着Dagger不会受到不合规的限定符注释的影响。

相关问题