使用JSR-303进行方法参数验证 [英] Method Parameter Validation with JSR-303

查看:490
本文介绍了使用JSR-303进行方法参数验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSR-303是否也用于方法参数验证?

Is JSR-303 also intended for method parameter validation?

如果是这样,网络上是否有任何示例?我面临的最大挑战是如何在每种方法中获取验证器.在Spring 3中,这是否意味着我实际上必须在每个类中注入LocalValidatorFactoryBean?

If so, is there any example on the web? The biggest challenge I'm facing is how to get a validator within each method. With Spring 3, doesn't it mean that I'd have to inject virtually every class with a LocalValidatorFactoryBean?

谢谢!

推荐答案

这听起来像是AOP(AspectJ)的用例.为用javax.validation.constraints.*注释的方法编写切入点,将验证器注入到方面(可能使用Spring),并使用@Before@Around建议在方法执行之前执行验证.

This sounds like a use case for AOP (AspectJ). Write a pointcut for methods that are annotated with javax.validation.constraints.*, inject a validator into the aspect (probably using Spring) and use a @Before or @Around advice to perform the validation before method execution.

阅读 AspectJ在行动中以供参考.

这篇关于使用JSR-303进行方法参数验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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