@Target
用于描述注解的使用范围
说明该注解可以放置在什么地方,如接口,class类上等使用方式: @Target({ElementType.TYPE}) public @interface FeignClient { }
ElementType的值
即规定注解的声明,即限制改注释能使用的范围
- TYPE——接口(包括Annotation,即@interface)、类、枚举声明
- FIELD——字段(包括枚举常量)声明,@CFNotNull
- METHOD——方法声明,如@Autowired
- PARAMETER——参数声明,如@Param
- CONSTRUCTOR—— 构造函数什么,如@ConstructorProperties
- LOCAL_VARIABLE——局部变量声明,如@CFNotNull
- ANNOTATION_TYPE——注释类型声明,如@Target
- PACKAGE——包声明
- TYPE_PARAMETER——类型参数声明
- TYPE_USE——类型的使用
原码:
public enum ElementType {
/** Class, interface (including annotation type), or enum declaration */
TYPE,
/** Field declaration (includes enum constants) */
FIELD,
/** Method declaration */
METHOD,
/** Formal parameter declaration */
PARAMETER,
/** Constructor declaration */
CONSTRUCTOR,
/** Local variable declaration */
LOCAL_VARIABLE,
/** Annotation type declaration */
ANNOTATION_TYPE,
/** Package declaration */
PACKAGE,
/**
* Type parameter declaration
*
* @since 1.8
*/
TYPE_PARAMETER,
/**
* Use of a type
*
* @since 1.8
*/
TYPE_USE
}
注意:当注解未指定Target值时,或未被Target注释时,则说明该注解可以用于任何元素之上。
Target值具有多个值的表达形式
形式1:
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
形式2:
@Target(value = {ElementType.ANNOTATION_TYPE,ElementType.CONSTRUCTOR})