Hibernate.orgCommunity Documentation
你碰到过下面这些让人抓狂的情况么:
specifying constraint annotations at unsupported data types (e.g. by annotating a String with @Past)
对一个JavaBean的setter方法进行标注(而不是getter)
对一个静态的变量或者方法进行约束条件标注(这样是不支持滴)
这样的话, 你就应该看看Hibernate Validator 的约束处理器了. 它会被插入到编译过程中, 然后如果发现如果哪个约束标注用错了的话, 则汇报编译错误.
You can find the Hibernate Validator Annotation Processor as part of the distribution bundle on Sourceforge or in the JBoss Maven Repository (see 例 1.1 “Configuring the JBoss Maven repository”) under the GAV org.hibernate:hibernate-validator-annotation-processor.
The Hibernate Validator Annotation Processor is based on the "Pluggable Annotation Processing API" as defined by JSR 269 which is part of the Java Platform since Java 6.
As of Hibernate Validator 4.3.2.Final the Hibernate Validator Annotation Processor checks that:
应用了约束标注的属性的类型是否被该约束所支持
only non-static fields or methods are annotated with constraint annotations
only non-primitive fields or methods are annotated with @Valid
only such methods are annotated with constraint annotations which are valid JavaBeans getter methods (optionally, see below)
only such annotation types are annotated with constraint annotations which are constraint annotations themselves
definition of dynamic default group sequence with @GroupSequenceProvider is valid
Hibernate Validator标注处理器的行为可以通过表 9.1 “Hibernate Validator 标注处理器配置项”中列出的处理器配置项加以控制.
表 9.1. Hibernate Validator 标注处理器配置项
配置项 | 功能 |
---|---|
diagnosticKind | 控制编译错误级别. 必须是枚举类型javax.tools.Diagnostic.Kind 中的值(字符串形式), 例如WARNING . 如果是ERROR 的话, 那么如果API检测到约束信息应用的错误的话, 会让编译过程终止, 默认是ERROR . |
methodConstraintsSupported | Controls whether constraints are allowed at methods of any kind. Must be set to true when working with method level constraints as supported by Hibernate Validator. Can be set to false to allow constraints only at JavaBeans getter methods as defined by the Bean Validation API. Defaults to true . |
verbose | Controls whether detailed processing information shall be displayed or not, useful for debugging purposes. Must be either true or false . Defaults to false . |
本小节详细介绍了如何把Hibernate Validator标注处理器与命令行编译(javac, Ant, Maven)以及IDE (Eclipse, IntelliJ IDEA, NetBeans)集成.
When compiling on the command line using javac, specify the JAR hibernate-validator-annotation-processor-4.3.2.Final.jar using the "processorpath" option as shown in the following listing. The processor will be detected automatically by the compiler and invoked during compilation.
例 9.1. 在javac中使用标注处理器
javac src/main/java/org/hibernate/validator/ap/demo/Car.java \ -cp /path/to/validation-api-1.0.0.GA.jar \ -processorpath /path/to/hibernate-validator-annotation-processor-4.3.2.Final.jar
和直接使用javac差不多, 可以在Apache Ant的javac task中添加上面例子中的参数:
例 9.2. 在Ant中使用标注处理器
<javac srcdir="src/main" destdir="build/classes" classpath="/path/to/validation-api-1.0.0.GA.jar"> <compilerarg value="-processorpath" /> <compilerarg value="/path/to/hibernate-validator-annotation-processor-4.3.2.Final.jar"/> </javac>
对于和Apache Maven集成来说我们有很多选择, 通常, 我们可以把Hibenrate Validator标注处理器作为依赖添加到你的项目当中:
例 9.3. 添加HV 标注处理器为依赖
... <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator-annotation-processor</artifactId> <version>4.3.2.Final</version> <scope>compile</scope> </dependency> ...
这样, 这个处理器就能够自动的被编译器所调用. 虽然基本上能工作,但是还是有一些缺点, 在某些情况下, 标注处理器的输出信息可能不能够被显示出来. (请参考MCOMPILER-66).
另外的一个选择是使用Maven Annotation Plugin. 不过在此文档撰写的时候, 这个插件还没有被上传到任何一个广泛被使用的仓库中. 所以, 你需要自己把这个插件自己的仓库添加到你的settings.xml 或 pom.xml中:
例 9.4. 添加Maven Annotation Plugin的仓库
... <pluginRepositories> <pluginRepository> <id>maven-annotation-plugin-repo</id> <url>http://maven-annotation-plugin.googlecode.com/svn/trunk/mavenrepo</url> </pluginRepository> </pluginRepositories> ...
现在, 禁用compiler插件所调用的标准的标注处理过程, 然后再通过定义一个execution来配置annotation plugin的运行, 还需要把Hibernate Validator标注处理器作为该插件的依赖添加进去(这样, 此标注处理器就不会被当成你的项目的依赖而出现在类路径中了):
例 9.5. 配置Maven Annotation Plugin
... <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> <compilerArgument>-proc:none</compilerArgument> </configuration> </plugin> <plugin> <groupId>org.bsc.maven</groupId> <artifactId>maven-processor-plugin</artifactId> <version>1.3.4</version> <executions> <execution> <id>process</id> <goals> <goal>process</goal> </goals> <phase>process-sources</phase> </execution> </executions> <dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator-annotation-processor</artifactId> <version>4.3.2.Final</version> <scope>compile</scope> </dependency> </dependencies> </plugin> ...
请参考以下步骤来在Eclipse中使用标注处理器:
右键点击你的项目, 然后选择"属性"
在"Java Compiler"页面确认"编译级别"设置的是"1.6". 否则的话是无法使用标注处理器的.
到"Java Compiler"下面的"Annotation Processing" 页面, 然后选择"启用标注处理"(译注: 我的电脑是英文版的, 所以真的不知道中文版的eclipse上, 这些翻译成了什么:(
Go to "Java Compiler - Annotation Processing - Factory Path" and add the JAR hibernate-validator-annotation-processor-4.3.2.Final.jar
确认工作空间重新编译
现在你应该能够看到所有的标注错误都在编辑窗口中显示出了错误标记,也都显示在了"问题"视图:
请参考以下步骤来在IntelliJ IDEA (9.0及以上):中使用标注处理器:
选择 "File", 然后 "Settings",
展开"Compiler"节点, 然后点击"Annotation Processors"
Choose "Enable annotation processing" and enter the following as "Processor path": /path/to/hibernate-validator-annotation-processor-4.3.2.Final.jar
添加处理器的全路径名称org.hibernate.validator.ap.ConstraintValidationProcessor
到"Annotation Processors"列表
如果需要的话, 添加你的模块到"Processed Modules"列表
重新编译你的项目, 然后应该能看到关于约束标注的错误信息了:
从6.9这个版本开始, NetBeans也支持标注处理了. 可以通过下面的步骤来启用它:
右键点击你的项目, 然后选择"属性"
Go to "Libraries", tab "Processor", and add the JAR hibernate-validator-annotation-processor-4.3.2.Final.jar
到"Build - Compiling"页面选中"Enable Annotation Processing" 和 "Enable Annotation Processing in Editor", 并且指定标注处理器的全路径名称org.hibernate.validator.ap.ConstraintValidationProcessor
.
所有的约束标注问题应该都会在编辑器里面直接被标记出来了:
以下是截止到2010年五月我们发现(但尚未解决)的问题:
HV-308: Additional validators registered for a constraint using XML are not evaluated by the annotation processor.
有时候, 在eclipse里面自定义的约束条件不能够被正确的检查. 清理这个项目可能会有帮助. 这可能是因为Eclipse中对 JSR 269 API的实现有问题, 但是还需要进一步的研究.
When using the processor within Eclipse, the check of dynamic default group sequence definitions doesn't work. After further investigation, it seems to be an issue with the Eclipse JSR 269 API implementation.
版权 © 2009 - 2011 Red Hat, Inc. & Gunnar Morling