Objective
At present, more and more architecture design is using annotations, such as spring3.0, struts2 and other frameworks. Let's take a look at the definition of annotations first. The following is a section of code that uses JDK 5 Annotation @Target:
@Target ({Elementtype.method})
@Retention (retentionpolicy.runtime)
@Inherited
@Documented
Public @interface Asynlog {
I. The use of @Target
Java.lang.annotation.Target
Used to set the annotation usage range
Java.lang.annotation.ElementType
Target uses ElementType to specify the enumeration collection that the annotation can be scoped to
second, the use of ElementType
Take value |
Annotation usage Scope |
Method |
Can be used on method |
TYPE |
Can be used on a class or interface |
Annotation_type |
Available on the annotation type (type modified by @interface) |
Constructor |
can be used to construct a method on |
FIELD |
Can be used on domain |
Local_variable |
Can be used on local variables |
PACKAGE |
Package information for recording Java files |
PARAMETER |
Can be used on parameters |
Here is the key note: ElementType. PACKAGE. It is not used in a generic class, but is used in a fixed file Package-info.java. Here you need to emphasize that naming must be "Package-info". Since Package-info.java is not a legitimate class, using Eclipse to create a class prompts you to be illegal, so you need to create the Package-info.java in the form of a file.
For example, define the available scope package:
@Target ({Elementtype.package,elementtype.method})
@Retention (retentionpolicy.runtime)
@Inherited
@ Documented public
@interface Asynlog {
So, create the file: Package-info.java, which reads as follows:
Copy Code code as follows:
@AsynLog
Package org.my.commons.logs.annotation;
Note: Annotations can only be used within the scope of the ElementType set, otherwise the error will be compiled. For example, a range that contains only elementtype.method indicates that the annotation can only be used on the method of the class and that the exception will be compiled beyond the scope of use.