java中注释作用类型_Java注解的 含义 作用 分类

@[toc]

一.注解的定义

注解是一种为程序元素设置元数据的方法

元数据是添加到程序元素如方法, 字段, 类 和包上的额外信息.

注解是一种分散式的元数据设置方式, 而xml是一种集中式设置方式, 与代码无绑定关系.

注解不能直接干扰程序的运行

注解的作用是把数据存储起来, 在某一个时刻去调用.

0edcd5c8eff8db8775892bd3fd540825.png 注解是继承自Annotation

二. 注解的作用

作为特定的标记, 告知编译器一些信息. 例如方法的@Override 注解, 用于编译器去检验方法的重写是否符合规范.

编译时动态处理, 例如动态生成代码, 例如Lombok提供的一些注解@Data, 来动态的生成getter setter toString 等等方法

运行时动态处理, 作为额外的信息载体,例如@Controller层的请求映射的路径

一个注解最重要的是解析这个注解的代码, 否则这个注解就没有连注释都不如.

三. 注解的分类

标准注解: Override(方法重写) Deprecated(过时的) SuppressWarings(忽略某些警告)

元注解: @Target @Retention @Inherited @Documented 这些注解的作用是用于定义注解的注解

自定义注解

3.1 元注解

元注解是用于修饰注解的注解, 通常用在注解的定义上. @Target : 注解的作用目标 @Retention : 注解的生命周期 @Inherited : 是否允许子类继承该注解 @Documented: 注解是否应当被包含在JavaDoc上

3.1.1@Target的作用

@Target 用于描述所修饰的注解使用范围.

packages types (包含了类 接口 枚举 Annotation类型)

类型成员( 方法 构造方法 成员变量 枚举值)

方法参数和本地变量 (循环变量 catch参数) 如下是@Target 注解源码

6cf0bdd221cb2f256108d2ae8b0dd1d4.png 查看ElementType的源码, 可以看到类型

public enum ElementType {

/** 用于类 接口 枚举

Class, interface (including annotation type), or enum declaration */

TYPE,

/** 作用于属性上 例如Spring中的 Autowired

Field declaration (includes enum constants) */

FIELD,

/** Method declaration */

METHOD,

/** 用于方法的参数上 例如Spring的RequestParam

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,

/**

* Module declaration.

*

* @since 9

*/

MODULE

}

3.1.2@Retention的作用

标注注解被保留的时间长短

用于定义注解的生命周期

Retention 的源码

@Documented

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.ANNOTATION_TYPE)

public @interface Retention {

/**

* Returns the retention policy.

* @return the retention policy

*/

RetentionPolicy value();

}

RetentionPolicy 的源码, 定义了几种类型

public enum RetentionPolicy {

/**

注解能在源文件(java文件)中保留, 编译后在class文件中被去除.

例如Override 也是用的@Retention(RetentionPolicy.SOURCE)进行修饰.

* Annotations are to be discarded by the compiler.

*/

SOURCE,

/**

注解能在源文件(java文件)中保留, 编译后在class文件中也会保留.

* 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,

/**

能够在运行时获取该注解的相关信息.

例如Spring的Autowired注解, 通过反射将用到的bean实例注入进来.

* 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

}

3.1.3 其他注解

@Documented: 注解是否应当被包含在JavaDoc上 . @Inherited : 是否允许子类继承该注解. 注解是否具有可继承性. 例如某个注解修饰了一个父类, 并且这个注解是用@Inherited修饰的, 那么该父类的子类就会自动的被该注解修饰.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值