JSF:JSR 303 Bean验证 - 为什么在getter而不是setter? [英] JSF : JSR 303 Bean Validation - Why on getter and not setter?

查看:118
本文介绍了JSF:JSR 303 Bean验证 - 为什么在getter而不是setter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么JSR 303(bean验证)是针对getter方法而不是setter的?把它放在setter方法下是不合逻辑的,因为那是进入字段的入口点,应该在之前检查验证?

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 point into a field and validation should be checked prior to that?

推荐答案

注释getter并不意味着在调用getter时执行验证。它仅用于标识约束应适用的属性。

Annotating getters doesn't mean that validation is performed when a getter is invoked. It is just used to identify the property to which a constraint shall apply.

将约束放在(通常是公共的)getter而不是(通常是私有)字段上的最大优点是约束是这种类型的公共API的一部分。它们甚至会被添加到生成的JavaDoc中。类型的用户知道哪种约束适用于它而不考虑其内部实现。

The big advantage of putting constraints on (usually public) getters instead on (typically private) fields is that the constraints are part of the type's public API that way. They will even be added to the generated JavaDoc. A user of a type knows that way which constraints apply to it without looking into its internal implementation.

注释getter的另一个优点是可以在基础上的方法中设置约束类或接口,也适用于任何子类型/实现。

Another advantage of annotating getters is that constraints can be put at methods on base classes or interfaces and also apply for any sub-types/implementations.

这篇关于JSF:JSR 303 Bean验证 - 为什么在getter而不是setter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆