Java Annotations

Java常见注解

JDK自带注解

  • @Override:重写
  • @Deprecated:过时
  • @Suppvisewarnings:忽略警告

常见第三方注解 > Spring

  • @Autowired
  • @Service
  • @Respository

常见第三方注解 > Mybatis

  • @InsertProvider
  • @UpdateProvider
  • @Options

注解分类

按照运行机制分

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

按照来源分

  • 来自JDK的注解
  • 来自第三方的注解
  • 自定义的注解

元注解

自定义注解

自定义注解语法要求

  • 使用@interface关键字定义注解。
  • 成员以无参无异常方式声明,可以用default为成员指定一个默认值,成员类型是受限制的,包括基本数据类型,String,Class,Annotation,Enumeration。
  • 如果注解只有一个成员,则成员必须取名为value(),在使用时可以忽略成员名和赋值号(=)。
  • 注解类可以没有成员,没有成员的注解称为标识注解。
  • 元注解 @Target:注解的作用域,包括CONSTRUCTOR(构造方法声明),FIELD (字段声明),LOCAL_VARIABLE(局部变量声明),METHOD(方法声明),PACKAGE(包声明),PARAMETER(参数声明),TYPE(类,接口)。
  • 元注解 @Retention:注解的生命周期,包括SOURCE(只在源码显示,编译时会丢弃),CLASS(编译时会记录到class中,运行时忽略),RUNTIME(运行时存在,可以通过反射读取)。
  • 元注解 @Inherited:允许子类继承。
  • 元注解 @Documented:生成javadoc时会包含注解。
import java.lang.annotation.*;

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Description {

    String desc();
    String author();
    int age() default 18;

}

使用自定义注解

<注解名>(<成员名1> = <成员值1>, <成员名2> = <成员值2>, ...)

    @Description(desc = "hello desc", author = "tom", age = 19)
    public void test() {
        
    }

解析注解

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

  • 定义注解
import java.lang.annotation.*;

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Description2 {

    String value();

}
  • 引用注解
@Description2("I am class Person annotation")
public class Person {

    @Description2("I am class Person : method name annotation")
    public String name() {
        return null;
    }

    @Description2("I am class Person : method age annotation")
    public int age() {
        return 0;
    }

    public void sing() {

    }

}

@Description2("I am class Man annotation")
public class Man extends Person {

    @Override
    @Description2("I am class Man : method name annotation")
    public String name() {
        return super.name();
    }

}
  • 解析注解 & 运行结果
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class Test {

    public static void main(String[] args) throws ClassNotFoundException {

        // 使用类加载器加载类
        Class<?> c = Class.forName("annotation.Man");

        // 找到类上面的注解
        boolean isPresent = c.isAnnotationPresent(Description2.class);

        // 获取注解实例
        if (isPresent) {
            Description2 desc2 = c.getAnnotation(Description2.class);
            System.out.println(desc2.value());
        }

        // 找到方法上的注解
        Method[] methods = c.getMethods();

        // 获取注解实例 1
        for (Method method : methods) {
            boolean isMPresent = method.isAnnotationPresent(Description2.class);
            if (isMPresent) {
                Description2 mDesc2 = method.getAnnotation(Description2.class);
                System.out.println("1:" + mDesc2.value());
            }
        }
        // 获取注解实例 2
        for (Method method : methods) {
            Annotation[] annotations = method.getAnnotations();
            for (Annotation annotation : annotations) {
                if (annotation instanceof Description2) {
                    Description2 desc2 = (Description2) annotation;
                    System.out.println("2:" + desc2.value());
                }
            }
        }
    }

}
I am class Man annotation
1:I am class Man : method name annotation
1:I am class Person : method age annotation
2:I am class Man : method name annotation
2:I am class Person : method age annotation
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,314评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,044评论 25 709
  • ——透过窗帘的,是屋子里桔黄的灯光么?那是暖暖的朦胧的诗意,诗意的恍如皎洁的月光。不知不觉在心中唤起一种似曾相识的...
    数豆者m阅读 1,924评论 1 3
  • 1.数组key和value的条件限制 1.key可以是integer或者string 2.value可以是任意类型...
    白白的沸羊羊阅读 2,720评论 0 0
  • 我的奶奶离开我已经足足四十年了!从她老人家去世以后的这近四十年来,在我的灵魂深处,从来就不曾安顿过。一直以来,我为...
    鬼谷孫子阅读 2,761评论 0 3