【问题标题】:How to Simplify Annotation如何简化注释
【发布时间】:2012-10-23 11:10:38
【问题描述】:

我正在使用来自 Hibernate 验证器的 @NotEmpty 注释。但是我需要提供自定义消息。因此,我正在尝试以下方法以消除重复代码

@org.hibernate.validator.constraints.NotEmpty
public @interface NotEmpty {
    String message() default "{notempty}";
}

上面是正确的方法吗?我可以知道这个注释覆盖的术语是什么吗?

【问题讨论】:

  • 如果您必须提供默认消息更改键 org.hibernate.validator.constraints.NotEmpty.message 的值,则无需另外添加注释。这也是默认消息,您可以在声明注释时指定消息。
  • 我已经在我的两个 ValidationMessages.properties 文件中添加了密钥,它就像一个魅力。只是为了澄清一下,有没有办法让我的上述注释有效?谢谢

标签: java annotations hibernate-annotations


【解决方案1】:

如果您想提供默认消息,请更改 ValidationMessages.properties 内的键 org.hibernate.validator.constraints.NotEmpty.message 的值,则无需另外添加注释。这也是默认消息,您可以在声明注释时指定消息

您可以在 Annotation Processing 上阅读有关注释的更多信息

【讨论】:

    猜你喜欢
    • 2019-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    • 1970-01-01
    • 2010-11-04
    • 2014-11-28
    • 2016-10-31
    相关资源
    最近更新 更多