Java注解什么时候执行?
我只是想编写一些可以在运行时、调用服务方法之前或之后立即执行的注释。
我不知道它们是在运行时还是编译时执行。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
注释不执行;它们是可以通过各种工具读取的注释或标记。有些是由编译器读取的,例如@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.
注释只是标记。他们不执行也不做任何事情。
您可以指定不同的保留策略:
更多信息请参见: 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:
More here: http://www.java2s.com/Tutorial/Java/0020__Language/SpecifyingaRetentionPolicy.htm
实际上,在定义注解时,必须指定参数@Retention,该参数定义该注解是否在源代码(SOURCE)、类文件(CLASS)或运行时可用(运行时)。
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).这取决于该注释附带的保留策略。
保留策略决定了注释应该在什么时候进行
被丢弃。 Java 通过定义 3 种类型的保留策略
java.lang.annotation.RetentionPolicy
枚举。它有SOURCE
、CLASS
和RUNTIME
。具有保留策略
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 hasSOURCE
,CLASS
andRUNTIME
.Annotation with retention policy
SOURCE
will be retained only withsource code, and discarded during compile time.
Annotation with retention policy
CLASS
will be retained tillcompiling the code, and discarded during runtime.
Annotation with retention policy
RUNTIME
will be available to theJVM through runtime.
The retention policy will be specified by using java built-in
annotation
@Retention
, and we have to pass the retention policytype. The default retention policy type is
CLASS
.注释对您的代码没有影响,它们在这里提供由编译器或其他工具评估的信息。
请参阅此页面。
Annotations don't have effect on your code, they're here to provide information to be evaluated by the compiler or other tools.
See this page.
注释只是源代码中的标记。它们将由 IDE、编译器或注释处理器等工具使用。
您可以使用 @Retention 定义是否应在源、类或运行时中评估注释。
如果您想定义自己拥有的注释,并且只有您知道注释应该做什么,那么您也应该编写一个注释处理器。
(但这并不简单 - 也许)
Annotations are only markers at the source code. They will be used by tools like IDE, compiler or annotation processors.
You can define with @Retention if a annotation should be evaluated in the source, class or runtime.
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)
实际上,它们可以在两种环境中读取,具体取决于您设置的保留策略。
They actually can be read in both environments, depending on the retention policy you set.