# 11.5. 验证

只要 JSR-303 实现（例如 Hibernate 验证器）位于类路径上，Bean Validation 1.1 支持的方法验证功能就会自动启用。这使得 bean 方法可以通过`jakarta.validation`对其参数和/或返回值的约束进行注释。具有`@Validated`类注释方法的目标类需要在类型级别使用注释进行注释，以便在其方法中搜索内联约束注释。

例如，以下服务触发第一个参数的验证，确保其大小在 8 到 10 之间：

```
@Service
@Validated
public class MyBean {
​
    public Archive findByCodeAndAuthor(@Size(min = 8, max = 10) String code, Author author) {
        return ...
    }
​
}
```

在解析约束消息`{parameters}`时使用应用程序的`MessageSource`消息。这允许您将[应用程序的`messages.properties`文件](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#features.internationalization)用于 Bean 验证消息。一旦参数被解析，消息插值就使用 Bean Validation 的默认插值器完成。

要自定义`Configuration`用于构建`ValidatorFactory`，请定义一个`ValidationConfigurationCustomizer`bean。当定义多个定制器 bean 时，将根据它们的`@Order`注释或实现`Ordered`按顺序调用它们。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.shiker.tech/spring-boot-can-kao-wen-dang/11.-io/11.5.-yan-zheng.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
