个人随笔
目录
当前位置: 首页 Java Java注解详解
Java注解详解
2019-03-14 22:58:18

这篇博文详细的讲解一下Java的注解,因为Java的注解真是太重要了基本上现在市面上的很多框架都用注解来简化使用者的使用,比如数据库事物控制,根本就不需要什么开启事务关闭事物,一个注解搞定。

1、注解分类:

a、按运行机制分

  1. 源码注解:注解只在源码中存在,编译成.class文件就不存在了。
  2. 编译时注解:注解在源码和.class中都存在(@Override@Deprecated@Suppvisewarnings
  3. 运行时注解;在运行阶段还起作用,甚至会影响运行逻辑的注解。

b、按来源分,有如下三类

  1. JDK自带的注解:Java目前只内置了三种标准注解:@Override(表明覆写父类的方法)、@Deprecated(表明方法过期,不再推荐使用)、@SuppressWarnings(表明去除什么类型的警告),以及四种元注解:@Target@Retention@Documented@Inherited.
  2. 第三方注解:用的和接触的最多的注解,比如springhibernate
  3. 自定义注解:也可以看做是我们编写的注解,其他的都是他人编写的注解

c、Java的元注解才是最厉害的

自定义注解就靠元注解

2、自定义注解的语法要求

  1. @Target({ElementType.METHOD,ElementType.Type})
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Inherited
  4. @Documented
  5. //使用@interface关键字定义注解
  6. public @interface Description{
  7. //成员以无参无异常方式声明
  8. String desc();
  9. String author();
  10. //可以用default为成员指定一个默认值
  11. int age() default 18;
  12. }

成员类型是受限的,合法的类型包含原始类型及String,Class,Annotation,Enumeration
如果注解只有一个成员,则成员名必须为value(),在使用是可以忽略成员名和赋值号=
注解类可以没有成员,没有成员的注解称为标识注解

3、下面四个皆为元注解

  1. @Target({ElementType.METHOD,ElementType.Type})
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Inherited
  4. @Documented

3-1:@Target

是注解的作用域:标识该注解可以作用于一个类中的哪些属性及方法上,如果作用域类型有多个,用
英文逗号分隔,下面是注解的作用域列表:

  1. CONSTRUCTOR:构造方法声明
  2. FIELD:字段声明
  3. LOCAL_VARIABLE:局部变量声明
  4. METHOD:方法声明
  5. PACKAGE:包声明
  6. PARAMETER:参数声明
  7. TYPE:类,接口

3-2:@Retention 表示该注解的生命周期

SOURCE:只在源码显示,编译时会丢弃
CLASS:编译时会记录到class,运行时忽略
RUNTIME:运行时存在,可以通过反射读取

3-3:Inherited

此注解是标识性的元注解,表示当前注解可以由子注解来继承

3-4:@Documented

表示生成javadoc的时候会包含注解

4、使用自定义注解

使用注解的语法:

  1. @<注解名>(<成员名>=<成员值>,<成员名>=<成员值>,<成员名>=<成员值>,...)
  2. @Description(desc="I am eyeColor",author="forever",age=18)
  3. public String eyeColor(){
  4. return "red";
  5. }

注解的定义看起来很像接口的定义,事实上,与其他任何Java接口一样,注解也将会编译成class文件。
定义注解时,会需要一些元注解(meta-annotation),如@Target@Retention
@Target用来定义你的注解将用于什么地方(例如是一个方法或一个域)。
@Retention用来定义该注解在哪一个级别可用,在源代码(SOURCE)、类文件中(CLASS)或者运行时(RUNTIME)

5、解析注解

通过反射获取类、函数或成员上运行时注解信息,从而实现动态控制程序运行的逻辑。

  1. public static void main(String[] args){
  2. //使用类加载器加载类
  3. try{
  4. Class c = Class.forName("cn.myforever.Test");
  5. //找到类上面的注解
  6. boolean isExist=c.isAnnotationPresent(Description.class);
  7. if(isExist){
  8. //拿到注解实例
  9. Description d =(Description)c.getAnnotation(Description.class);
  10. System.out.println(d.value());
  11. }
  12. }catch(ClassNotFoundException e){
  13. e.printStackTrace();
  14. }
  15. }
  1. 使用forName()方法加载类,并使用isAnnotationPresent(Description.class)判断是否有有注解,若是有的话,用getAnnotation(Description.class)获取注解。
  2. 注解的继承只能作用在类上,方法上的注解不会被继承,Interface中的所有注解不会被继承。若是方法或者字段的话要相应的调用方法的isAnnotationPresent
  3. 按我理解的注解,作用就是用在类,字段,方法。。。上,然后使用这个类获方法时可以根据注解去获取一些信息,比如这个类的作用,以及这个类要使用的配置文件什么的.

6、下面举个例子

a、首先创建一个注解

  1. package cn.forever.myAnnotation;
  2. import java.lang.annotation.Documented;
  3. import java.lang.annotation.ElementType;
  4. import java.lang.annotation.Inherited;
  5. import java.lang.annotation.Retention;
  6. import java.lang.annotation.RetentionPolicy;
  7. import java.lang.annotation.Target;
  8. /**
  9. * 这个注解要用于所有的地方
  10. * @author forever
  11. */
  12. //这里暂时类,方法,和字段
  13. @Target({ElementType.METHOD,ElementType.TYPE,ElementType.FIELD,ElementType.PARAMETER})
  14. //运行期有效
  15. @Retention(RetentionPolicy.RUNTIME)
  16. //子类可以继承(标注型注解)
  17. @Inherited
  18. //可出现在JavaDoc中
  19. @Documented
  20. public @interface Description {
  21. /*
  22. * 成员类型是受限的,合法的类型包含原始类型及String,Class,Annotation,Enumeration
  23. * 如果注解只有一个成员,则成员名必须为value(),在使用是可以忽略成员名和赋值号=
  24. * 注解类可以没有成员,没有成员的注解称为标识注解
  25. */
  26. //注解名字
  27. String name();
  28. //用途
  29. String desc();
  30. }

b、其次创建一个使用注解的model

  1. package cn.forever.model;
  2. import cn.forever.myAnnotation.Description;
  3. //可以告诉这个表的名字,或者这个类需要引用的配置文件路径什么的
  4. @Description(name="TestModel",desc="我是方法")
  5. public class TestModel {//类
  6. @Description(name="name",desc="我是字段name")
  7. private String name;//字段
  8. public String getName() {
  9. return name;
  10. }
  11. @Description(name="test()",desc="我是方法")
  12. public void test(@Description(name="aaa",desc="我是参数aaa")String aaa,@Description(name="bbb",desc="我是参数bbb")String bbb){
  13. System.out.println("方法");
  14. }
  15. }

c、最后创建一个测试类,使用注解

  1. package cn.forever;
  2. import java.lang.annotation.Annotation;
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.Method;
  5. import cn.forever.model.TestModel;
  6. import cn.forever.myAnnotation.Description;
  7. public class Test {
  8. public static void main(String[] args) {
  9. //类
  10. Class clazz = TestModel.class;
  11. Boolean isExist = clazz.isAnnotationPresent(Description.class);
  12. if(isExist){
  13. Description desc = (Description) clazz.getAnnotation(Description.class);
  14. System.out.println(desc.desc());
  15. System.out.println(desc.name());
  16. }
  17. //字段
  18. Field[] fields =clazz.getDeclaredFields();
  19. for (Field field : fields) {
  20. if(field.isAnnotationPresent(Description.class)){
  21. Description desc2 = (Description) field.getAnnotation(Description.class);
  22. System.out.println(desc2.desc());
  23. System.out.println(desc2.name());
  24. }
  25. }
  26. //方法
  27. Method[] methods = clazz.getMethods();
  28. for (Method method : methods) {
  29. if(method.isAnnotationPresent(Description.class)){
  30. Description desc3 = (Description) method.getAnnotation(Description.class);
  31. System.out.println(desc3.desc());
  32. System.out.println(desc3.name());
  33. //参数
  34. Annotation[][] annotations=method.getParameterAnnotations();
  35. for (Annotation[] annotations2 : annotations) {
  36. for (Annotation annotation : annotations2) {
  37. Description desc4=(Description) annotation;
  38. System.out.println(desc4.desc());
  39. System.out.println(desc4.name());
  40. }
  41. }
  42. }
  43. }
  44. }
  45. }

结语

学会注解,为以后写框架打下基础

 220

啊!这个可能是世界上最丑的留言输入框功能~


当然,也是最丑的留言列表

有疑问发邮件到 : suibibk@qq.com 侵权立删
Copyright : 个人随笔   备案号 : 粤ICP备18099399号-2