DAO 接口中的@Validated 批注是否有帮助?
Will the @Validated annotation in the DAO Interface help at all?
我在DAOs接口中看到很多@Validated
注解不是我写的。例如:
@Validated
public interface CompanyDAO extends BaseDAO<Company> {
public List<Company> list();
我应该期待什么行为:
- 在将所有传递给 DAO 的对象保存到数据库之前验证它们?
- 正在验证 DAO 从数据库中检索的所有对象?
- 他们两个?
- 什么都没有。此注解不应放在 DAO 接口中。
只是一些您可能不知道的提示:
8.8 Spring Validation of 8. Validation, Data Binding, and Type Conversion in the manual 指定
In order to be eligible for Spring-driven method validation, all target classes need to be annotated with Spring’s @Validated
annotation, optionally declaring the validation groups to use. Check out the MethodValidationPostProcessor
javadocs for setup details with Hibernate Validator and Bean Validation 1.1 providers.
MethodValidationPostProcessor
说
Applicable methods have JSR-303 constraint annotations on their parameters and/or on their return value (in the latter case specified at the method level, typically as inline annotation), e.g.:
public @NotNull Object myValidMethod(@NotNull String arg1, @Max(10) int arg2)
Target classes with such annotated methods need to be annotated with Spring's Validated annotation at the type level, for their methods to be searched for inline constraint annotations. Validation groups can be specified through @Validated
as well. By default, JSR-303 will validate against its default group only.
所以,也许是您问题的答案
What behaviour should I expect
是:代码中给出的任何约束。
我在DAOs接口中看到很多@Validated
注解不是我写的。例如:
@Validated
public interface CompanyDAO extends BaseDAO<Company> {
public List<Company> list();
我应该期待什么行为:
- 在将所有传递给 DAO 的对象保存到数据库之前验证它们?
- 正在验证 DAO 从数据库中检索的所有对象?
- 他们两个?
- 什么都没有。此注解不应放在 DAO 接口中。
只是一些您可能不知道的提示:
8.8 Spring Validation of 8. Validation, Data Binding, and Type Conversion in the manual 指定
In order to be eligible for Spring-driven method validation, all target classes need to be annotated with Spring’s
@Validated
annotation, optionally declaring the validation groups to use. Check out theMethodValidationPostProcessor
javadocs for setup details with Hibernate Validator and Bean Validation 1.1 providers.MethodValidationPostProcessor
说Applicable methods have JSR-303 constraint annotations on their parameters and/or on their return value (in the latter case specified at the method level, typically as inline annotation), e.g.:
public @NotNull Object myValidMethod(@NotNull String arg1, @Max(10) int arg2)
Target classes with such annotated methods need to be annotated with Spring's Validated annotation at the type level, for their methods to be searched for inline constraint annotations. Validation groups can be specified through
@Validated
as well. By default, JSR-303 will validate against its default group only.
所以,也许是您问题的答案
What behaviour should I expect
是:代码中给出的任何约束。