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 r
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)
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.
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
.
They actually can be read in both environments, depending on the retention policy you set.
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
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 {}