java since注解_java注解

java注解在我们使用框架的时候经常会遇到,那么java注解又是如何运行,我们应该怎样编写我们的注解,提高我们的编码逼格,这个就是我们今天要讨论的内容。

①定义注解类

/*** 定义注解类*/

public @interfaceMyAnnotation {}

②注解的作用目标:

*类

*方法

*构造器

*参数

*局部变量

*包

③注解的属性定义格式:类型 属性名();

/*** 定义注解类*/

public @interfaceMyAnnotation {int value() default 100;//注解属性特权,当属性只要填写一个,并且属性名为value时,可以不写属性名,直接写值,例如@MyAnnotation(xxx)

intage();

String name()default "zhaoLiu";//定义属性,顺便定义默认值,可以忽略写该属性,default在后面添加默认值

}

④注解的属性类型

*8种基本类型

*String

*Enum

*Class

*注解类型

*以上类型的一位数字类型

例子,上代码

public @interfaceMyAnnotation {int a();//8种基本类型中的int类型

String b();//String类型

MyEnum c();//枚举类型

Class d();//Class类型

MyAnno e();//注解类型

int[] f();//数组类型

}enumMyEnum{

A,B,C

}

@interfaceMyAnno{doublea();doubleb();

}

这里我们写了一个注解类,而这个注解类是如何在类里面声明以及使用呢

@MyAnnotation(a=1,b="lisi",c=MyEnum.A,d=List.class,e=@MyAnno(a=0.1,b=0.2),f=3)public class Test1 {}

其中f是一个数组,当数组元素的个数为1时,可以省略大括号。

⑤注解的作用目标限定:比如让一个注解,它的作用目标只能在在类上,不能在方法上,这叫作用目标的限定

*在定义注解时,给注解添加注解,这个注解是@Target,其实@Target也是注解,我们来看下Target.class代码是怎样的

@Documented

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.ANNOTATION_TYPE)public @interfaceTarget {/*** Returns an array of the kinds of elements an annotation type

* can be applied to.

*@returnan array of the kinds of elements an annotation type

* can be applied to*/ElementType[] value();

}

这里要求@Target(...)的的属性类型是ElementType数组,然后我们在看一下ElementType.class

public enumElementType {/**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

*

*@since1.8*/TYPE_PARAMETER,/*** Use of a type

*

*@since1.8*/TYPE_USE

}

里面的英文注释也写的很清楚,如果只作用于类、接口(包括注解类)或者枚举上,可以写@Target(ElementType.TYPE)。如果只作用于成员变量上,可以写@Target(ElementType.FIELD)。如果同时作用在方法和构造器上,可以写@Target({ElementType.METHOD,ElementType.CONSTRUCTOR})。这里我没有写"value=",因为这是注释属性特权,当注释的属性只有一个并且属性名为value,可以忽略不写

⑥保留策略:在定义注解时,给注解添加注解,这个注解是@Retention,我们来看Retention.class代码

@Documented

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.ANNOTATION_TYPE)public @interfaceRetention {/*** Returns the retention policy.

*@returnthe retention policy*/RetentionPolicy value();

}

它的属性类型是RetentionPolicy,接着我们继续看RetentionPolicy.class代码

public enumRetentionPolicy {/*** 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.

*

*@seejava.lang.reflect.AnnotatedElement*/RUNTIME

}

是个枚举类型,下面是它们每个值所代表的意思

* 源代码文件(SOURCE):注解只在源代码中存在,当编译时就被忽略了

* 字节码文件(CLASS):注解在源代码中存在,然后编译时会把注解信息放到了class文件,但JVM在加载类时,会忽略注解!

* JVM中(RUNTIME):注解在源代码、字节码文件中存在,并且在JVM加载类时,会把注解加载到JVM内存中(它是唯一可反射注解!)

⑦关于读取注解(反射),我们在下一个章节就能看到,关于不清楚反射应该怎么使用,可以看一下我前几章的"java反射的用法"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值