本文介绍了由于@Constraint validatedBy,如何避免层之间的交叉依赖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的项目中,我们在单独的Maven模块中有Service和DAO层。服务模块依赖于DAO模块并与其实体一起使用。问题是我们不能放置自定义jsr 303约束,它使用DAO实体中服务层的一些服务,因为这会创建从DAO层到服务层的后向引用,因为验证器类需要在自定义的validatedBy属性中引用@Constraint。

In our project we have Service and DAO layers in separate Maven modules. Service module depends on DAO module and works with it's entities. The problem is that we can't put custom jsr 303 constraint which uses some services from service layer in DAO entity because that would create the back reference from DAO layer to service layer, because the validator class needs to be referenced in validatedBy attribute of custom @Constraint.

有没有办法(使用标准的jsr 303 api)在运行时指定一些自定义约束的验证器类(或以任何其他方式解决我们的问题)?内置约束具有空的validatedBy属性,但我不知道是否有一些api。

Is there a way (using standard jsr 303 api) to specify validator class of some custom constraint at runtime (or solve our problem in any other way)? The built in constraints have empty validatedBy attribute, but I don't know if there is some api for that.

推荐答案

你可以使用为约束分配验证器,以避免从注释到验证器实现的引用。

You may use an XML based constraint mapping to assign a validator to your constraint to avoid the reference from the annotation to the validator implementation.

也是该领域的一些改进。您可以在上轻松解决问题。

The BV 1.1 EG is also discussing some improvements in that area. Feel free to weigh in on the issue on the mailing list.

这篇关于由于@Constraint validatedBy,如何避免层之间的交叉依赖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 03:22