java常见的注解之 @Target、@Retention、@Documented、@Component

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.springframework.stereotype;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
String value() default "";
}

上面是一个spring常用的注解@Controller,下面让我们分析一下上面的注解
1.@Target({ElementType.TYPE})

@Target用来表示注解作用范围,超过这个作用范围,编译的时候就会报错。
 java.lang.annotation.ElementType
 Target通过ElementType来指定注解可使用范围的枚举集合,枚举集合如下

package java.lang.annotation;

/**
 * A program element type.  The constants of this enumerated type
 * provide a simple classification of the declared elements in a
 * Java program.
 *
 *

These constants are used with the {@link Target} meta-annotation type
 * to specify where it is legal to use an annotation type.
 *
 * @author  Joshua Bloch
 * @since 1.5
 /
public enum ElementType {
    /
* Class, interface (including annotation type), or enum declaration */
    TYPE,

/** Field declaration (includes enum constants) */
    FIELD,

/** Method declaration */
    METHOD,

/** Parameter declaration */
    PARAMETER,

/** Constructor declaration */
    CONSTRUCTOR,

/** Local variable declaration */
    LOCAL_VARIABLE,

/** Annotation type declaration */
    ANNOTATION_TYPE,

/** Package declaration */
    PACKAGE
}
ElementType的用法

取值 注解使用范围
TYPE 可用于类或者接口上
FIELD 可用于域上
METHOD 可用于方法上
PARAMETER 可用于参数上
CONSTRUCTOR 可用于构造方法上
LOCAL_VARIABLE 可用于局部变量上
ANNOTATION_TYPE 可用于注解类型上(被interface修饰的类型)
PACKAGE 用于记录java文件的package信息
2.@Retention(RetentionPolicy.RUNTIME)

@Retention定义了该Annotation被保留的时间长短

@Retenrion通过RetebtionPolicy表示需要在什么级别保存该注释信息,用于描述注解的生命周期(即:被描述的注解在什么范围内有效)

RetentionPoicy取值有:

package java.lang.annotation;

/**

  • Annotation retention policy. The constants of this enumerated type

  • describe the various policies for retaining annotations. They are used

  • in conjunction with the {@link Retention} meta-annotation type to specify

  • how long annotations are to be retained.

  • @author Joshua Bloch

  • @since 1.5
    /
    public enum RetentionPolicy {
    /
    *

    • Annotations are to be discarded by the compiler.
      */
      SOURCE,

    /**

    • Annotations are to be recorded in the class file by the compiler
    • but need not be retained by the VM at run time. This is the default
    • behavior.
      */
      CLASS,

    /**

    • Annotations are to be recorded in the class file by the compiler and
    • retained by the VM at run time, so they may be read reflectively.
    • @see java.lang.reflect.AnnotatedElement
      */
      RUNTIME
      }
      用法如下:

取值 有效范围
SOURCE 在源文件中有效(即源文件保留)
CLASS 在class文件中有效(即class保留)
RUNTIMR 在运行时有效(即运行时保留)
3.@Documented

Documented注解表明这个注释是由 javadoc记录的,在默认情况下也有类似的记录工具。 如果一个类型声明被注释了文档化,它的注释成为公共API的一部分

package java.lang.annotation;

/**

  • Indicates that annotations with a type are to be documented by javadoc
  • and similar tools by default. This type should be used to annotate the
  • declarations of types whose annotations affect the use of annotated
  • elements by their clients. If a type declaration is annotated with
  • Documented, its annotations become part of the public API
  • of the annotated elements.
  • @author Joshua Bloch
  • @since 1.5
    */
    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.ANNOTATION_TYPE)
    public @interface Documented {
    }
    4.@Component

Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注

package org.springframework.stereotype;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Component {
String value() default "";
}

posted on 2021-06-18 14:53  清风HD  阅读(451)  评论(0)    收藏  举报