Java - get注释始终为null

时间:2016-08-25 09:11:36

标签: java annotations

我是注释的新手,我有使用自定义注释FXMLController注释的类,将这些类发送到工厂以从注释中获取值,但它始终为null〜

注释:

public @interface FXMLController {

    String value() default "";

}

用法:

@FXMLController(value=CommonConstants.SPLASH_SCREEN)
public class SplashScreenController{ ....... )

获得价值:

Annotation annotation = controller.getAnnotation(FXMLController.class);
FXMLController fxmlController = (FXMLController) annotation;

enter image description here

1 个答案:

答案 0 :(得分:8)

我猜您忘记将注释标记为@Retention(RetentionPolicy.RUNTIME)

编辑: 实际上,您的注释应该如下所示:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documentedpublic @interface FXMLController {
    String value() default "";
}