java中可以读取注解的值吗?
这是我的代码:
@Column(columnName="firstname")
private String firstName;
@Column(columnName="lastname")
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
是否可以在另一个类中读取我的注释 @Column(columnName="xyz123")
的值?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
是的,如果您的列注释具有运行时保留,
您可以执行如下
更新:要获取私有字段,请使用
Yes, if your Column annotation has the runtime retention
you can do something like this
UPDATE : To get private fields use
当然是。这是一个示例注释:
和一个示例注释方法:
另一个类中打印 testText 值的示例方法:
对于像您这样的字段注释没有太大不同。
Of course it is. Here is a sample annotation:
And a sample annotated method:
And a sample method in another class that prints the value of the testText:
Not much different for field annotations like yours.
我从来没有这样做过,但它看起来像
字段
是一个AnnotatedElement
所以它有getAnnotation
。 此页面有一个示例(复制如下);如果您知道注释的类并且注释策略在运行时保留注释,则非常简单。当然,如果保留策略不在运行时保留注释,您将无法在运行时查询它。已删除的答案(?)提供了指向注释教程的有用链接 您可能会发现有帮助;我已将链接复制到此处,以便人们可以使用它。
此页面中的示例:
I've never done it, but it looks like Reflection provides this.
Field
is anAnnotatedElement
and so it hasgetAnnotation
. This page has an example (copied below); quite straightforward if you know the class of the annotation and if the annotation policy retains the annotation at runtime. Naturally if the retention policy doesn't keep the annotation at runtime, you won't be able to query it at runtime.An answer that's since been deleted (?) provided a useful link to an annotations tutorial that you may find helpful; I've copied the link here so people can use it.
Example from this page:
详细说明@Cephalopod的答案,如果您想要列表中的所有列名称,您可以使用此oneliner:
Elaborating to the answer of @Cephalopod, if you wanted all column names in a list you could use this oneliner:
虽然到目前为止给出的所有答案都完全有效,但人们还应该记住 Google 反射库 一种更通用、更简单的注释扫描方法,例如
While all the answers given so far are perfectly valid, one should also keep in mind the google reflections library for a more generic and easy approach to annotation scanning, e.g.
通常情况下,您对字段具有私有访问权限,因此您不能在反射中使用getFields。相反,您应该使用 getDeclaredFields
因此,首先,您应该知道您的 Column 注释是否具有运行时保留:
之后您可以执行以下操作:
显然,您想对字段执行某些操作- 使用注释值设置新值:
因此,完整代码可以如下所示:
In common case you have private access for fields, so you CAN'T use getFields in reflection. Instead of this you should use getDeclaredFields
So, firstly, you should be aware if your Column annotation has the runtime retention:
After that you can do something like this:
Obviously, you would like to do something with field - set new value using annotation value:
So, full code can be looked like this:
在我的例子中,您还可以使用泛型类型,考虑到您可以执行以下操作之前所说的所有内容:
记住,这不会 100% 等于 SomeClass.class.get(...)();
但能做到这一点...
You can also use generic types, in my case, taking into account everything said before you can do something like:
Remember, that this will not equival at 100% to SomeClass.class.get(...)();
But can do the trick...
对于少数要求通用方法的人来说,这应该对您有帮助(5 年后:p)。
对于下面的示例,我从具有 RequestMapping 注释的方法中提取 RequestMapping URL 值。
要适应字段,只需将 RequestMapping 的用法更改
为
并交换您要阅读的任何注释。
但请确保注释具有@Retention(RetentionPolicy.RUNTIME)。
For the few people asking for a generic method, this should help you (5 years later :p).
For my below example, I'm pulling the RequestMapping URL value from methods that have the RequestMapping annotation.
To adapt this for fields, just change the
to
And swap usage of RequestMapping for whatever annotation you are looking to read.
But make sure that the annotation has @Retention(RetentionPolicy.RUNTIME).
我使用它的方式之一:
one of the ways I used it :
要读取 java 中注释的值,请尝试执行以下步骤:
注解
目标
输出
结果为“api/v1/demo”
To read the value of annotation in java try to follow the following steps:
Annotation
Target
Output
The result is "api/v1/demo"