JSR 303 Bean Validation - Why on getter and not setter?

前端 未结 4 1671
广开言路
广开言路 2020-12-29 20:25

I don\'t understand why JSR 303 (bean validation) is for the getter methods and not setter? Isn\'t it more logical to put it under setter method since that is the entry poin

4条回答
  •  没有蜡笔的小新
    2020-12-29 20:58

    Bean Validation is called that way for a reason. It is applied to an initialized bean. So, first off, you initialize it with everything you have, then you pass it(or it is passed explicitly) to the Bean Validation implementation, which will rely on the validation annotations when accessing the fields. In case of Spring MVC validation handling starts at:

    result = execVal.validateParameters(
                    invocation.getThis(), methodToValidate, invocation.getArguments(), groups);
    

    inside MethodValidationInterceptor. From here on, it's passed to validation implementation, in most cases Hibernate. invocation.getArguments() will contain all the method arguments already initialized with the given values, regardless of validation annotations.

提交回复
热议问题