1. 之前一直有用 Spring Boot Web 集成的 Bean Validation , 也写了自定义的Bean Validator (一般公司也仅用到这块) 对于JSR标准定义了哪些有些模糊
  2. Service层使用 Bean Validation
  3. 关于Bean Validation的单元测试
  4. Bean Validation 源码Debug


Bean Definition


  1. 所有属性都必须是private
  2. 所有外部访问必须通过setter、getter
  3. 不能有extend
  4. 不能有implments
  5. 不能包含预定注解,如@Entity public class Hello



JavaBean 是一种JAVA语言写成的可重用组件。JavaBean符合一定规范编写的Java类,不是一种技术,而是一种规范。它的方法命名,构造及行为必须符合特定的约定:

  1. 所有属性为private。
  2. 这个类必须具有一个公共的(public)无参构造函数
  3. private属性必须提供public的getter和setter来给外部访问,并且方法的命名也必须遵循一定的命名规范。 。
  4. 这个类应是可序列化的,要实现serializable接口。

Spring Bean


Validation Defionition

JSR -> JavaSpecification Requests的缩写,意思是“Java 规范提案”

Specification Version 最低java版本 Link Core
JSR303 Bean Validation 1.0 java6 JCP Hibernate 对标准进行实现
JSR349 Bean Validation 1.1 java6 JCP 增加校验方法和返回值...
JSR380 Bean Validation 2.0 java8 JCP add more function



How to Use

spring boot 指南 controller service unit bean Validation


Bean Validation 1.0 中的提案一段话

Validating data is a common task that is copied in many different layers of an application, from the presentation tier to the persistentce layer. Many times the exact same validations will have to be implemented in each separate validation framework, proving time consuming and error-prone. To prevent having to re-implement these validations at each layer, many developers will bundle validations directly into their classes, cluttering them with copied validation code that is, in fact, meta-data about the class itself.

This JSR will define a meta-data model and API for JavaBean validation. The default meta-data source will be annotations, with the abilty to override and extend the meta-data through the use of XML validation descriptors. It is expected that the common cases will be easily accomplished using the annotations, while more complex validations or context-aware validation configuration will be available in the XML validation descriptors.

The validation API developed by this JSR will not be specific to any one tier or programming model. It will specifically not be tied to either the web tier or the persistence tier, and will be available for both server-side application programming, as well as rich client Swing application developers. This API is seen as a general extension to the JavaBeans object model, and as such is expected to be used as a core component in other specifications, such as JSF, JPA, and Bean Binding.