When implementing validation in Java or Spring, you may use Bean Validation such as @ NotNull
or @NotBlank
.
When I was implementing, I wondered what the implementation class associated with these annotations was, so I looked at the source code, but the validatedBy of @ Constraint
was blank. ..
Take the NotBlank annotation [^ 1] in the javax.validation.constraints package as an example.
NotBlank.java
@Documented
@Constraint(validatedBy = { }) //The class in which the contents of validation are written is not specified by ←
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
@Retention(RUNTIME)
@Repeatable(List.class)
public @interface NotBlank {
String message() default "{javax.validation.constraints.NotBlank.message}";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
/**
* Defines several {@code @NotBlank} constraints on the same element.
*
* @see NotBlank
*/
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
@Retention(RUNTIME)
@Documented
public @interface List {
NotBlank[] value();
}
}
This time, I investigated the class that is associated with the location of the implementation result of this annotation.
When I searched on Google, the same question was asked on stackoverflow at here, and I was able to find out the implementation class. ..
That is here org NotBlankValidator class under .hibernate.validator.internal.constraintvalidators.bv.
From this, we can see that the annotation and its implementation class are not in the same library, but in different libraries.
Also, the class that links these exists in the same library as the implementation class, and [here](https://github.com/hibernate/hibernate-validator/blob/db7ae586508add6491f1443c8ab4e770d7cb2c20/engine/src/main/java/org It turned out that it is linked by the ConstraintHelper class under org.hibernate.validator.internal.metadata.core of /hibernate/validator/internal/metadata/core/ConstraintHelper.java).
This time, I found that the annotation implementation class that exists in Bean Validation's javax.validation.constraints exists under org.hibernate.validator.internal.constraintvalidators.bv.
We also found that these classes are linked by the ConstraintHelper class under org.hibernate.validator.internal.metadata.core.
However, I don't understand why annotations and implementation classes bother to exist in different libraries. It seems that the relationship between Bean Validation and Hibernate is involved, but is it like separating the interface and implementation in consideration of extensibility?
If anyone knows about this point, I would appreciate it if you could tell me.
Until the end Thank you for reading.
Site that triggered the solution
Recommended Posts