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

7fhtutme  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(469)

我想创建一个自定义注解,并用另一个注解对其进行注解,如下所示:

@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";
    }
}

我知道我可以简单地创建一个实现 ConstraintValidator 但我想知道我是否能实现这个更简单的功能。

erhoui1w

erhoui1w1#

添加此注解

@Constraint(validatedBy = {})

解决了问题。
尽管此注解出现在 Min 以及 Size 注解,但我不知道为什么它没有自动应用于自定义注解,需要显式定义。

相关问题