Hibernate Validator - 第一个beta版本 - 4.0.0 Beta1 - 现已可用。这个版本基于 JSR 303 规范 1.0.CR2[1]。
这个版本中新增了通过XML配置Hibernate Validator的功能(参见规范[1]的第7节)。有两种类型的配置文件。首先,validation.xml 允许您程序化配置您的Bean Validation框架。以下是一个示例,它具体说明了Bean Validation提供者和消息插值器。它还指定了进一步映射文件的路径
<?xml version="1.0" encoding="UTF-8"?> <validation-config xmlns="https://jboss.com.cn/xml/ns/javax/validation/configuration" xsi:schemaLocation="https://jboss.com.cn/xml/ns/javax/validation/configuration validation-configuration-1.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <default-provider>org.hibernate.validation.engine.HibernateValidatorConfiguration</default-provider> <message-interpolator>org.hibernate.validation.engine.ResourceBundleMessageInterpolator</message-interpolator> <constraint-mapping>META-INF/validation/order-constraints.xml</constraint-mapping> <constraint-mapping>META-INF/validation/user-constraints.xml</constraint-mapping> <property name="org.hibernate.validator.test">foobar</property> </validation-config>
第二种类型的XML配置文件是实际映射文件,允许您声明约束声明。XML与注解声明方法非常相似。在上述示例中使用的 user-constraints.xml 可能看起来像这样
<constraint-mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jboss.com.cn/xml/ns/javax/validation/mapping validation-mapping-1.0.xsd" xmlns="https://jboss.com.cn/xml/ns/javax/validation/mapping"> <default-package>org.hibernate.validation.engine.xml</default-package> <bean class="User" ignore-annotations="false"> <class ignore-annotations="true"> <group-sequence> <value>User</value> <value>Optional</value> </group-sequence> <constraint annotation="org.hibernate.validation.engine.xml.ConsistentUserInformation"> <message>Message defined in xml</message> <groups> <value>javax.validation.groups.Default</value> </groups> <element name="stringParam">foobar</element> </constraint> </class> <field name="lastname"> <constraint annotation="javax.validation.constraints.Pattern"> <message>Last name has to start with with a capital letter.</message> <element name="regexp">^[A-Z][a-z]+</element> </constraint> </field> <field name="creditcard"> <valid/> </field> <getter name="firstname" ignore-annotations="true"> <constraint annotation="javax.validation.constraints.Size"> <message>Size is limited!</message> <groups> <value>org.hibernate.validation.engine.xml.TestGroup</value> <value>javax.validation.groups.Default</value> </groups> <element name="max">30</element> </constraint> </getter> </bean> <constraint-definition annotation="org.hibernate.validation.engine.xml.ConsistentUserInformation"> <validated-by include-existing-validators="false"> <value>org.hibernate.validation.engine.xml.CustomConsistentUserValidator</value> </validated-by> </constraint-definition> </constraint-mappings>
与以下匹配的用户实体
public class User { private String firstname; private String lastname; private CreditCard creditcard; private String phoneNumber; // standard getter and setter follow ... }
这个第一个beta版本现在功能基本完整,除了由于规范工作正在进行中的原因,实施与规范之间的一些小差异。我们希望至少再发布一个beta版本以完全对齐实施和规范。这项工作已经开始,并将产生一个Bean Validation TCK。TCK的基础是建立在 JSR 299 TCK框架 上。谢谢大家;-)当然,我们也会尽快完成我们的参考文档。
欢迎对这次发布提出反馈。
享受吧!