13.07.2015 Views

JSR 303 的参考实现使用指南 - JBoss

JSR 303 的参考实现使用指南 - JBoss

JSR 303 的参考实现使用指南 - JBoss

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

类 级 别 约 束public void setManufacturer(String manufacturer) {}this.manufacturer = manufacturer;@AssertTruepublic boolean isRegistered() {return isRegistered;}}public void setRegistered(boolean isRegistered) {}this.isRegistered = isRegistered;When using property level constraints property access strategy is used to access thevalue to be validated. This means the bean validation provider accesses the state viathe property accessor method. One advantage of annotating properties instead of fieldsis that the constraints become part of the constrained type's API that way and users areaware of the existing constraints without having to examine the type's implementation.提 示It is recommended to stick either to field or property annotations withinone class. It is not recommended to annotate a field and the accompanyinggetter method as this would cause the field to be validated twice.2.1.3. 类 级 别 约 束最 后 , 一 个 约 束 也 能 够 被 放 在 类 级 别 上 . 当 一 个 约 束 被 标 注 在 一 个 类 上 的 时 候 , 这 个 类 的 实 例 对 象被 传 递 给 ConstraintValidator. 当 需 要 同 时 校 验 多 个 属 性 来 验 证 一 个 对 象 或 者 一 个 属 性 在 验 证 的 时候 需 要 另 外 的 属 性 的 信 息 的 时 候 , 类 级 别 的 约 束 会 很 有 用 . 在 例 2.3 “ 类 级 别 约 束 ” 中 , 我 们给 类 Car 添 加 了 一 个 passengers 的 属 性 . 并 且 我 们 还 标 注 了 一 个 PassengerCount 约 束 在 类 级 别 上 . 稍后 会 看 到 我 们 是 如 何 创 建 这 个 自 定 义 的 约 束 的 ( 第 3 章 创 建 自 己 的 约 束 规 则 ). 现 在 , 我 们 可 以 知道 ,PassengerCount 会 保 证 这 个 车 里 乘 客 的 数 量 不 会 超 过 它 的 座 位 数 .例 2.3. 类 级 别 约 束package com.mycompany;import javax.validation.constraints.Min;import javax.validation.constraints.NotNull;import javax.validation.constraints.Size;@PassengerCountpublic class Car {@NotNullprivate String manufacturer;9

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!