Explain the usage of JDK 5 Annotation annotation @target _java

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.