使用 Class 文字作为自定义 Java 注释的成员

Using Class Literal as a member for custom Java annotation

我是 Java 注释概念的新手。我想为我的 Spring 启动应用程序编写如下 Java 注释:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
      
  DataType type() default null;

  Class<? extends DataProcessor> dataProcessor() <--I WOULD LIKE TO ADD ONE CLASS LITERAL IMPLEMENTING DATAPROCESSOR INTERFACE DEFINED BELOW


      default NullDataProcessor.class;

}

接口数据处理器定义如下:

public interface DataProcessor {
  String process(DataType type, Map<String, Object> input);
}

我想将上面的注释用于方法,如下所示:

@MyAnnotation(dataProcessor=MyDataProcessorImpl.class)

所以这里我有三个问题:

  1. 我将如何添加 class 文字作为 Java 注释的成员?
  2. 如何定义接口的多个实现?
  3. 我将如何定义默认实现,即 NullDataProcessor?

有人可以帮忙吗?谢谢。

编辑

  1. ,我得到了关于如何在方面的帮助下从方法参数中提取值的想法。但我无法理解如何调用函数:process() 方法参数。
  1. 您已经这样做了:

    @MyAnnotation(dataProcessor=MyDataProcessorImpl.class)
    
  2. 将注释元素设为数组:

    @Retention(RUNTIME)
    @Target(METHOD)
    public @interface MyAnnotaiton {
        Class<? extends DataProcessor>[] dataProcessors();
    }
    

    或者使注释可重复:

    @Retention(RUNTIME)
    @Target(METHOD)
    @Repeatable(MyAnnotations.class)
    public @interface MyAnnotation {
       Class<? extends DataProcessor> dataProcessor();
    }
    
    @Retention(RUNTIME)
    @Target(METHOD)
    public @interface MyAnnotations {
        MyAnnotation[] value();
    }
    
  3. 给注解元素添加默认值:

    @Retention(RUNTIME)
    @Target(METHOD)
    public @interface MyAnnotation {
        Class<? extends DataProcessor> dataProcessor() default NullDataProcessor.class;
    }
    

    如果您使用数组,最好将默认值保留为空数组,并让处理注释的代码处理该特殊情况。