关于注解的说明

发布于 2024-08-07 00:35:05 字数 485 浏览 4 评论 0原文

谁能用简单的英语解释一下下面两段的意思? (取自 http://www.ibm.com/developerworks/java /library/j-cwt08025.html

“注释更加灵活 就你如何使用它们而言, 是否注释的选项 信息将包含在课堂中 编译器输出并制作的文件 应用程序在运行时可用 时间”

不确定这些意味着什么。注释可以配置为有选择地更改字节码吗?

虽然注释非常适合 与特定相关的元数据 组件,它们不太适合 具有跨组件的元数据 应用程序。

恕我直言,大多数 Web 应用程序都是跨组件的。作者在这里想表达什么?

Can anyone please explain what the following two paragraphs mean, in simple english? (Taken from http://www.ibm.com/developerworks/java/library/j-cwt08025.html)

"Annotations are more flexible in
terms of how you use them, with
options for whether the annotation
information is to be included in class
files output by the compiler and made
available to the application at run
time"

Not sure what these means. Can Annotations be configured to optionally change the bytecode?

While annotations are ideal for
metadata that relates to a particular
component, they are not well suited to
metadata with cross-component
application.

IMHO most web applications would be cross-component ones. What is the author trying to say here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

零時差 2024-08-14 00:35:05

注释在术语上更加灵活
您如何使用它们,以及以下选项
注释信息是否为
包含在类文件输出中
由编译器提供并提供给
运行时的应用程序

我认为,这是指 Java5 注释可以被编译器删除,而有些注释可以保留在字节码中 这是由放置在注释类型上的 @Retention 注释控制的,例如,

@Documented
@Retention(value=RUNTIME)
public @interface Deprecated

这表明 @Deprecated 注释将出现在字节码中,并且也是可见的反思。 java.lang.annotation.RetentionPolicy 定义不同的选项。

Annotations are more flexible in terms
of how you use them, with options for
whether the annotation information is
to be included in class files output
by the compiler and made available to
the application at run time

This, I think, refers to the fact that Java5 annotations can be dropped by the compiler, whereas some can be retained in the bytecode. This is controlled by the @Retention annotation that is placed on your annotation type, e.g

@Documented
@Retention(value=RUNTIME)
public @interface Deprecated

This indicates that the @Deprecated annotation will be present in the bytecode, and will also be visible to reflection. java.lang.annotation.RetentionPolicy defines the different options.

心如狂蝶 2024-08-14 00:35:05

“注释在使用方式方面更加灵活,可以选择注释信息是否包含在编译器输出的类文件中并在运行时可供应用程序使用”

比 XDoclet 更灵活,因为:

  • 它 可以从源代码中使用(如 XDoclet)
  • 可以在运行时使用,当您只有字节码而没有源代码时(与 XDoclet 不同)

虽然注释非常适合与特定组件相关的元数据,但它们不太适合跨组件应用程序的元数据。

与外部 Xml 相比,注释(如 XDoclet)有一个有趣的功能:
注释存在于代码中,因此它们很自然地应用于定义它们的代码。您不必指定(使用一些复杂的语法)它们适用于哪一段代码。示例:

  • 如果在方法上定义注释,则它自然适用于该方法
  • 如果在字段上定义注释,则它自然适用于该字段
  • 如果在类上定义注释,则它自然适用于该类
  • 如果注释是在包上定义的,它自然适用于该包

如果您希望在外部 Xml 文件中具有相同的内容,则必须使用复杂的语法来标识您引用的代码段。这使得它们非常容易应用。

此外,在代码重构(如重命名)的情况下,注释仍然可以正常工作,而必须更改外部 xml 以指向新的类或方法名称。


我不相信在 Web 应用程序中,大多数东西都是跨组件的。

  • 如果您定义了实体的持久性(到数据库),例如应该持久保存此类的表是什么,那么它不是所有实体的全局变量,它只影响当前实体。
  • 对于许多其他例子也是如此......

"Annotations are more flexible in terms of how you use them, with options for whether the annotation information is to be included in class files output by the compiler and made available to the application at run time"

It is more flexible than XDoclet because:

  • it can be used from the source code (like XDoclet)
  • it can be used at runtime, when you only have the byte-code and not the source code (unlike XDoclet)

While annotations are ideal for metadata that relates to a particular component, they are not well suited to metadata with cross-component application.

Annotations (like XDoclet) have one interesting feature, as opposed to an external Xml for example :
Annotations live in the code, so it is natural for them to be applied to the code they are defined on. You don't have to specify (using some complex syntax) to what piece of code they apply. Examples:

  • if an annotation is defined on a method, it applies naturally to that method
  • if an annotation is defined on a field, it applies naturally to that field
  • if an annotation is defined on a class, it applies naturally to that class
  • if an annotation is defined on a package, it applies naturally to that package

If you want to have the same in an external Xml file, you have to use a complex syntax to identify the piece of code you refer to. So that makes them very easy to apply.

Also, in case of a code refactoring (like renaming), annotations continue to work just fine, while an external xml would have to be changed to point to the new class or method name.


I don't believe that in a web application, most things are cross-component.

  • If you defined Persistance (to a database) of an Entity, like what is the table where this class should be persisted, it is not something global to all Entities, it only affects the current Entity.
  • same for many other examples...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文