简体   繁体   English

如何在 java 中用另一个注解来注解一个注解?

[英]How to annotate an annotation with another annotation in java?

I want to create a custom annotation and annotate it with another annotation like below:我想创建一个自定义注释并使用另一个注释对其进行注释,如下所示:

@Documented
@Target({ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
//@Size(min = min, max = max, message = defaultMessage) //neither this works
//@Min(6) //nor this works
public @interface PasswordString {
    String message() default defaultMessage;

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

    class PasswordStringAnnotationConfig {
        public static final int min = 6;
        public static final int max = 100;
        public static final String defaultMessage = "Password must be between " + min + " and " + max + "characters";
    }
}

I know I can simply create a class that implement ConstraintValidator but I wonder if I can achieve this easier functionality.我知道我可以简单地创建一个实现ConstraintValidator的 class 但我想知道我是否可以实现这个更简单的功能。

adding this annotation添加此注释

@Constraint(validatedBy = {})

solved the problem.解决了这个问题。

Although this annotation is present in the declaration of Min and of Size annotations, but I don't know why it was not applied on the custom annotation automatically and needed to be defined explicitly.虽然这个注解出现在MinSize注解的声明中,但我不知道为什么它没有自动应用到自定义注解上,需要显式定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM