你好,这个校验其实是引用的依赖提供好的功能,依赖是:
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>xxx</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>xxx</version>
</dependency>
代码里只是对这些依赖的使用,来方便校验。
本质上就是:先定义好几个注解,每个注解都有自己特别的含义,然后给了一个ValidatorFactory类,可以读取这些注解做相关的check。这里涉及到的核心知识是对 annoation的解析。 这种annoation其实在开发中你已经遇到很多了,比如@Service,@Controller这种,java里提供了专门的类来读取专门的注解,然后按照预期的功能去实现