为什么要使用java注解呢?
我想问为什么java注释被如此多地使用...我知道它们替换了例如jpa中的xml配置,但是为什么要使用这种配置呢? 考虑这段代码:
@Entity
class Ent{
// some fields
}
//... somewhere in the other file far far away
class NonEnt{
// whatever here
}
Now, when I try to put this in persistence context, with EntityManager
's persist method, I get runtime error(better would be to get compile error) with trying to persist NonEnt
instance. There is obvious solution for me, force the entities to implement some no-method interface instead of using @Annotations. But this isn't popular among framework designer, what is the drawback of this solution?Thanks for answering...
i want to ask why are the java annotations used so much... I know that they replaced xml configuration in for example jpa, but why is this kind configuration used at all?
Consider this piece of code:
@Entity
class Ent{
// some fields
}
//... somewhere in the other file far far away
class NonEnt{
// whatever here
}
Now, when I try to put this in persistence context, with EntityManager
's persist method, I get runtime error(better would be to get compile error) with trying to persist NonEnt
instance. There is obvious solution for me, force the entities to implement some no-method interface instead of using @Annotations. But this isn't popular among framework designer, what is the drawback of this solution?
Thanks for answering...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
与标记接口相比,注释有一些优点:
注释也被认为侵入性较小,但这一点是品味问题并且值得商榷。
另请参阅:
When compared to marker interfaces, annotations have some advantages:
Annotations are also supposedly less intrusive, but this point is matter of taste and debatable.
See also:
与强制客户端实现接口或扩展类相比,使用注释的侵入性要小得多。
The use of annotations is a lot less invasive than forcing the client to implement a interface or extend a class.
您所描述的称为“标记接口”,这是对接口概念的滥用。我怀疑您认为它显而易见的唯一原因是因为
Serialized
- 它的存在只是因为当时没有注释。它的优点是什么?注释有一个巨大的优势,那就是它们可以有参数,而且粒度更细。标记接口仅在类级别工作。
What you describe is called a "marker interface" and it's an abuse of the interface concept. I suspect the only reason why you consider it obvious is because of
Serializable
- which only exists because there were no annotations at that time.What are its advantages? Annotations have the huge advantage that they can have parameters, and they are much more fine-grained. Marker interfaces only work at the class level.
引用java教程:
正如您所看到的,注释是一种在 java 中指定有关类型(包括接口)元数据的方法,它们绝不是它们的替代品。
Citing the java tutorial:
As you can see, annotations are a way of specifying meta-data about your types in java, including interfaces, they are in no way a replacement for them.
当您想要向类、方法或实例变量添加一些附加信息时,Java 注释非常有用。有很多库大量使用这些注释。这些注释使代码保持简单和可读,并具有在运行时更改代码的能力。
例如,如果您使用了 lombok 库,它会在编译时创建 setter、getter 和构造函数时间并节省您的代码行和时间。
当编译器执行代码时,lomok 会搜索所有标有 @Setter 或 @Getter 注释的字段,并在类中为该字段添加 setter 和 getter。
另一个例子是 Junit 测试运行程序。 junit 如何区分测试类中的普通辅助方法和测试。为了区分两者,它使用 @Test 注释。
本教程介绍了如何使用 java 注释来创建自己的测试运行器。
Java annotation are really helpful when you want to add some additional information to your class, method or instance variable. There are a lot of libraries which use these annotations heavily. These annotations keep the code simple and readable with the power of making changes to the code at runtime.
For example if you have used lombok library, which creates setter, getter and constructor at compile time and saves you lines of code and time.
When compiler executes the code, lomok searches for all the fields marked with @Setter or @Getter annotation and add setter and getter for that field in the class.
One other example is Junit test runner. How junit differentiates between normal helper method in test class and a test. To differentiate between the two it uses @Test annotation.
This tutorial explains how you can use java annotations to create you own test runner.