为什么要使用java注解呢?

发布于 2024-10-04 04:30:56 字数 678 浏览 2 评论 0原文

我想问为什么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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

江南烟雨〆相思醉 2024-10-11 04:30:56

与标记接口相比,注释有一些优点:

  • 它们可以参数化
  • 它们更加细粒度 - 您不仅可以将它们附加到类,还可以将它们附加到其他类元素(字段、方法、方法参数等)

注释也被认为侵入性较小,但这一点是品味问题并且值得商榷。

另请参阅:

When compared to marker interfaces, annotations have some advantages:

  • they can be parameterized
  • they are more fine grained - you can attach them not only to classes but also to other class elements (fields, methods, method arguments, etc)

Annotations are also supposedly less intrusive, but this point is matter of taste and debatable.

See also:

╰つ倒转 2024-10-11 04:30:56

与强制客户端实现接口或扩展类相比,使用注释的侵入性要小得多。

The use of annotations is a lot less invasive than forcing the client to implement a interface or extend a class.

我不在是我 2024-10-11 04:30:56

对我来说有明显的解决方案,

您所描述的称为“标记接口”,这是对接口概念的滥用。我怀疑您认为它显而易见的唯一原因是因为 Serialized - 它的存在只是因为当时没有注释。

强制实体实施一些
无方法接口而不是使用
@注释。但这并不流行
框架设计者中,什么是
这个解决方案的缺点?

它的优点是什么?注释有一个巨大的优势,那就是它们可以有参数,而且粒度更细。标记接口仅在类级别工作。

There is obvious solution for me,

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.

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?

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.

迷迭香的记忆 2024-10-11 04:30:56

引用java教程:

注释提供有关
程序不属于
程序本身。他们没有直接的
对代码运行的影响
他们注释。

注释有多种用途,
其中:

  • 编译器的信息 - 注释可供编译器使用
    编译器检测错误或抑制错误
    警告。
  • 编译时和部署时处理 - 软件工具可以
    将注释信息处理为
    生成代码、XML 文件等
    向前。
  • 运行时处理——一些注释可用于
    在运行时检查。

正如您所看到的,注释是一种在 java 中指定有关类型(包括接口)元数据的方法,它们绝不是它们的替代品。

Citing the java tutorial:

Annotations provide data about a
program that is not part of the
program itself. They have no direct
effect on the operation of the code
they annotate.

Annotations have a number of uses,
among them:

  • Information for the compiler — Annotations can be used by the
    compiler to detect errors or suppress
    warnings.
  • Compiler-time and deployment-time processing — Software tools can
    process annotation information to
    generate code, XML files, and so
    forth.
  • Runtime processing — Some annotations are available to be
    examined at runtime.

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.

马蹄踏│碎落叶 2024-10-11 04:30:56

当您想要向类、方法或实例变量添加一些附加信息时,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文