【学习笔记】注解

注解

 

注解概念

mybatis、springboot 等框架的底层实现机制就是注解和反射

 

  • 注解(Annotation) 是JDK5.0开始引入的新技术

  • Annotation 的作用:

    • 不是程序本身,可以对程序做出解释 (和注释相同)

    • 可以被其他程序(比如:编译器)读取,通过反射读取

  • Annotation的格式:

    • 注解是以 “@注释名” 在代码中存在,还可以添加一些参数值,例如 @SuppressWarning(value= "unchecked")

  • Annotation在哪里使用?

    • 可以附加在package ,class ,method,field上面,相当于给它们添加了额外的辅助信息,我们可以通过反射机制编程实现对这些元数据的访问

 

内置注解

 

  • @Override:定义在 java.lang.Override 中,此注解只适用于修饰方法,表示一个方法声明打算重写父类中另一个方法

  • @Deprecated : 定义在 java.lang.Deprecated 中,此注解可以用来修饰方法、属性、类,表示不鼓励程序员使用这样的元素,通常是因为它很危险,或存在更好的选择

  • @SuppressWarnings: 定义在 java.lang.SuppressWarnings中,用来抑制编译时的警告信息

    • 与前两个不同,SuppressWarnings需要添加一个参数才能正确使用,这些参数都是已经定义好的

    • @SuppressWarning("all")

    • @SuppressWarning("unchecked")

    • @SuppressWarning(value={"unchecked","deprecation"})

 

package com.annotation;
//镇压警告
@SuppressWarnings("all")
public class Demo01 {
    //重写注解
    @Override
    public String toString() {
        return super.toString();
    }
    //废弃的注解
    @Deprecated
    public static void test(){
        System.out.println("Deprecated");
    }
​
    public static void main(String[] args) {
        test();
    }
}

 

元注解

  • 元注解的作用就是负责注解其他注解,Java 定义了四个标准的 meta-annotation 类型,它们被用来提供对其他 annotation 类型作说明

  • 这些类型和它们所支持的类在java.lang.annotation包中可以找到(@Target,@Retention,@Decumented,@Inherited)

    • @Target:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)

    • @Retention:表示需要在什么级别保存在注释信息,用于描述注解的生命周期(SOURCE(源码)<CLASS(class文件)<RUNTIME(运行))

    • @Document:说明该注解将被包含在javaDoc中

    • @Inherited:说明子类可以继承父类中的该注解

 

@Target 源码:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
    /**
     * Returns an array of the kinds of elements an annotation type
     * can be applied to.
     * @return an array of the kinds of elements an annotation type
     * can be applied to
     */
    ElementType[] value();
}

参数名为value ,类型是ElementType[],ElementType是枚举类型

public enum ElementType {
    /** 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
     *
     * @since 1.8
     */
    TYPE_PARAMETER,
​
    /**
     * Use of a type
     *
     * @since 1.8
     */
    TYPE_USE
}

令Target的参数等于ElementType中的什么,它的作用域就是什么

public class Demo02 {
    @MyAnnotation
    public void test(){
        
    }
}
​
//定义一个注解
@Target(value={ElementType.METHOD,ElementType.TYPE})
@interface MyAnnotation{
​
}

比如我们自定义的注解,可以使用在方法上和类上

 

@Retention源码

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
    /**
     * Returns the retention policy.
     * @return the retention policy
     */
    RetentionPolicy value();
}
public enum RetentionPolicy {
    /**
     * 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.
     *
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME
}

SOURCE : 表示在源码级别

CLASS:编译后

RUNTIME:运行时

我们自定义的注解都用RUNTIME

//@Retention 表示注解在什么地方有效
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation{
​
}

 

另外两个元注解

//@Documented 表示是否将注解生成在JavaDoc中
@Documented
//@Inherited 表示子类可以继承父类的注解
@Inherited
@interface MyAnnotation{
​
}

 

自定义注解

  • 使用@interface 自定义注解时,自动继承了java.lang.annotation.Annotation 接口

  • 分析:

    • @interface 用来声明一个注解,格式:public @ interface 注解名{定义内容}

    • 其中的每一个方法实际上就是声明了一个配置参数

    • 方法的名称就是参数的名称

    • 返回值类型就是参数的类型(返回值只能是基本类型,Class,String,enum)

    • 可以通过default来声明参数的默认值

    • 如果只有一个参数成员,一般参数名为 value

    • 注解元素必须要有值,我们定义注解元素时,经常使用空字符串,0作为默认值

package com.annotation;
​
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
​
public class Demo03 {
    @MyAnnotation2(name="w",age=18)
    public void test(){
        
    }
    @MyAnnotation3("hhh")
    public void test3(){
​
    }
}
​
@Target(value = {ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2{
    //注解的参数:参数类型+ 参数名 ();
    String name();
    int age();
    int id() default -1;
}
@Target(value = {ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation3{
    //注解的参数:参数类型+ 参数名 ();
    String value();
}
posted @ 2022-08-04 10:08  GrowthRoad  阅读(18)  评论(0编辑  收藏  举报