Java注解什么时候执行?

发布于 2025-01-07 09:13:09 字数 69 浏览 2 评论 0原文

我只是想编写一些可以在运行时、调用服务方法之前或之后立即执行的注释。

我不知道它们是在运行时还是编译时执行。

I am just looking to write some annotation which can execute at runtime, before or immediately after a service method is invoked.

I don't know if they are executed at runtime or compile time.

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

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

发布评论

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

评论(7

谁把谁当真 2025-01-14 09:13:09

注释不执行;它们是可以通过各种工具读取的注释或标记。有些是由编译器读取的,例如@Override;其他的则嵌入到类文件中,并在运行时由 Hibernate 等工具读取。但他们自己什么也不做。

您可能会想到断言 相反,它可用于验证前置条件和后置条件。

Annotations don't execute; they're notes or markers that are read by various tools. Some are read by your compiler, like @Override; others are embedded in the class files and read by tools like Hibernate at runtime. But they don't do anything themselves.

You might be thinking of assertions instead, which can be used to validate pre and post conditions.

伪装你 2025-01-14 09:13:09

注释只是标记。他们不执行也不做任何事情。

您可以指定不同的保留策略:

  • SOURCE:注释仅保留在源文件中,并在编译期间被丢弃。
  • CLASS:编译时保存在.class文件中的注释,在运行时不可用。
  • RUNTIME:注释存储在 .class 文件中并在运行时可用。

更多信息请参见: http://www.java2s.com/Tutorial/Java/0020__Language/指定RetentionPolicy.htm

Annotations are just markers. They don't execute and do anything.

You can specify different retention policies:

  • SOURCE: annotation retained only in the source file and is discarded during compilation.
  • CLASS: annotation stored in the .class file during compilation, not available in the run time.
  • RUNTIME: annotation stored in the .class file and available in the run time.

More here: http://www.java2s.com/Tutorial/Java/0020__Language/SpecifyingaRetentionPolicy.htm

枫林﹌晚霞¤ 2025-01-14 09:13:09

实际上,在定义注解时,必须指定参数@Retention,该参数定义该注解是否在源代码(SOURCE)、类文件(CLASS)或运行时可用(运行时)。

@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {}

Actually, when you define an annotation, you must specify the parameter @Retention, which defines whether the annotation is available in the source code (SOURCE), in the class files (CLASS), or at run time (RUNTIME).

@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {}
半步萧音过轻尘 2025-01-14 09:13:09

这取决于该注释附带的保留策略。

保留策略决定了注释应该在什么时候进行
被丢弃。 Java 通过定义 3 种类型的保留策略
java.lang.annotation.RetentionPolicy 枚举。它有 SOURCECLASSRUNTIME

  • 具有保留策略 SOURCE 的注释将仅保留
    源代码,并在编译时丢弃。

  • 具有保留策略CLASS的注释将被保留直到
    编译代码,并在运行时丢弃。

  • 具有保留策略RUNTIME的注释将可供
    JVM 通过运行时。

保留策略将使用 java 内置指定
注解@Retention,我们必须传递保留策略
类型。默认保留策略类型是CLASS

It depends on retention policy attached with that annotation.

A retention policy determines at what point annotation should be
discarded. Java defined 3 types of retention policies through
java.lang.annotation.RetentionPolicy enumeration. It has SOURCE, CLASS and RUNTIME.

  • Annotation with retention policy SOURCE will be retained only with
    source code, and discarded during compile time.

  • Annotation with retention policy CLASS will be retained till
    compiling the code, and discarded during runtime.

  • Annotation with retention policy RUNTIME will be available to the
    JVM through runtime.

The retention policy will be specified by using java built-in
annotation @Retention, and we have to pass the retention policy
type. The default retention policy type is CLASS.

情域 2025-01-14 09:13:09

注释对您的代码没有影响,它们在这里提供由编译器或其他工具评估的信息。

注释有多种用途,其中:

编译器的信息 - 编译器可以使用注释来检测错误或抑制警告。

编译时和部署时处理 - 软件工具可以处理注释
用于生成代码、XML 文件等的信息。

运行时处理——一些注释可以在运行时检查。

请参阅此页面

Annotations don't have effect on your code, they're here to provide information to be evaluated by the compiler or other tools.

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.

See this page.

你在看孤独的风景 2025-01-14 09:13:09
  1. 注释只是源代码中的标记。它们将由 IDE、编译器或注释处理器等工具使用。

  2. 您可以使用 @Retention 定义是否应在源、类或运行时中评估注释。

  3. 如果您想定义自己拥有的注释,并且只有您知道注释应该做什么,那么您也应该编写一个注释处理器。
    (但这并不简单 - 也许)

  1. Annotations are only markers at the source code. They will be used by tools like IDE, compiler or annotation processors.

  2. You can define with @Retention if a annotation should be evaluated in the source, class or runtime.

  3. If you would like define your owned annotations and only you has the knowledge what the annotation should be do, so you should write an annotation processor too.
    (but this is not simple - maybe)

归属感 2025-01-14 09:13:09

实际上,它们可以在两种环境中读取,具体取决于您设置的保留策略。

They actually can be read in both environments, depending on the retention policy you set.

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