简体   繁体   English

Java Bean验证:是否可以指定运行时验证?

[英]Java Bean Validation: Posible to specify runtime validations?

I am maintaining a web app that is using Java bean validations (as part of JSR303 I believe). 我正在维护一个使用Java Bean验证的Web应用程序(我相信这是JSR303的一部分)。

Members are mapped with annotations like @Pattern and then we have a LocalValidatorFactoryBean that performs the validation. 成员映射有@Pattern类的@Pattern ,然后我们执行一个LocalValidatorFactoryBean The Validator that this factory bean bootstraps is actually the Hibernate validation instance. 该工厂bean引导的Validator实际上是Hibernate验证实例。

My problem is that my @Pattern regex needs to be loaded at runtime when my application starts up, so I cannot use the annotation. 我的问题是,当我的应用程序启动时,我的@Pattern正则表达式需要在运行时加载,因此我无法使用注释。

Therefore, I'm wondering if there is an alternative way such as XML to plug in such validation? 因此,我想知道是否有其他方法(例如XML)插入这种验证?

If not, I may just have to use a separate Spring validator to do this work 如果没有,我可能只需要使用一个单独的Spring验证器即可完成这项工作

I recommend creating custom validator as described in http://docs.spring.io/spring/docs/3.0.0.RC3/reference/html/ch05s07.html#validation.beanvalidation.spring.constraints . 我建议按照http://docs.spring.io/spring/docs/3.0.0.RC3/reference/html/ch05s07.html#validation.beanvalidation.spring.constraints中的说明创建自定义验证器。 Thus you will need to check the value agains the pattern in the code of constraint validator class. 因此,您将需要再次检查值,然后在约束验证器类的代码中检查模式。

You can configure bean validation using XML for such a thing. 您可以使用XML来配置bean验证。 The syntax is a lot like the JavaBean XML format and maps rather well to the hierarchical structure of the equivalent Java syntax. 语法非常类似于JavaBean XML格式,并且很好地映射到等效Java语法的层次结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM