zoukankan      html  css  js  c++  java
  • Java:注解Annotation

    annotation的概念

    In the Java computer programming language, an annotation is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. 

    是Java提供的一种元程序中的元素关联任何信息和着任何元数据(metadata)的途径和方法。Annotation(注解)是一个接口,程序可以通过反射来获取指定程序元素的Annotion对象,然后通过Annotion对象来获取注解里面的元数据

    java中的注解是一种继承自接口java.lang.annotation.Annotation的特殊接口

    自定义annotation

    用关键字@interface可以自定义一个注解,目前这个注解没有任何功能,仅仅只是声明了,(自定义注解需要加上元注解,下面会写到)

    public @interface CustomizeAnnotation {
    
    }

    定义好之后就可以在类、方法、变量上面使用了(自定义注解在加上了元注解后,其使用位置就被限制,下面的例子只是说明注解可以存在且不限于以下位置)

    @CustomizeAnnotation
    public class Check {
    
        @CustomizeAnnotation
        public void checkOut(@CustomizeAnnotation String username) {
    
        }
    
    }

     注解也可以有属性,写法和接口中方法一样

    public @interface CustomizeAnnotation {
    
        String name() default "Tom";
    
        int age();
    
    }

    如果注解有属性,在使用注解时需要给没有默认值的属性赋值(例如name有默认值Tom,不用赋值,age没有默认值,需要赋值)

    public class Check {
    
        @CustomizeAnnotation(age = 10)
        public void checkOut(String username) {
    
        }
    
    }

    这样自定义注解不完整,因为自定义注解需要加上元注解

    meta-annotation元注解

    Java5.0定义了4个标准的meta-annotation类型,它们被用来提供对其它 annotation类型作说明

    • @Target   定义注解的作用目标
    • @Retention   定义注解的保留策略,retention是保留的意思
    • @Documented   表示该注解将被包含在javadoc中
    • @Inherited   表示子类可以继承父类中的该注解

    查看一下@Target注解源码,可以看到这个注解(自己注解了自己)有一个属性value,类型是枚举数组

     

    然后看看这个枚举定义哪些作用目标,每个值的作用注释解释的很详细

     查看一下@Retention注解的源码

    然后看看这个RetentionPolicy这个枚举有哪些值,只有三个值

     annotation应用

    自定义一个注解(如下:需要加上@Target@Retention,另外两个注解@Documented和@Inherited不是必须要加的

    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface CustomizeAnnotation {
        int count() default 300;
    }

    获取注解中的属性

    @CustomizeAnnotation
    public class Main {
    
        public static void main(String[] args) {
            CustomizeAnnotation annotation = Main.class.getAnnotation(CustomizeAnnotation.class);
            System.out.println(annotation.count());
        }
    }

     注解在spring框架中使用很广泛,用以替代xml配置文件,后续在补充在spring中的应用

  • 相关阅读:
    Python3网络学习案例三:编写web server
    struct.pack()和struct.unpack() 详解(转载)
    Python3网络学习案例二:traceroute详解
    Redis 配置
    vue之this.$route.params和this.$route.query的区别
    解决bugs: mybatisPlus 分页不能生效
    解决bug :"status":400,"error":"Bad Request","message":"Required request body is missing:
    vue,ElementUI中Switch 开关,switch 打开时的值为数字,该如何设置
    解决bug:vue项目中点击修改按钮,不能显示要修改的分类名字
    The 'Access-Control-Allow-Origin' header contains multiple values'*, *', but only one is allowed.
  • 原文地址:https://www.cnblogs.com/colin220/p/9265922.html
Copyright © 2011-2022 走看看