什么是注解(Annotation)以及自定义注解:

2022-09-15 09:08:53

1.什么是注解:
Annotation是从JDK5.0开始引入的新技术。
2. Annotation的作用:
1):不是程序本身,可以对程序作出解释。(这一点,跟注释没什么区别)
2): 可以被其他程序(比如:编译器等)读取。(注解信息处理流程,是注解和注释的重大区别 。如果没有注解信息处理流程,则注解毫无意义)
3. Annotation在哪里使用?
可以附加在package, class, method, field等上面,相当于给它们添加了额外的辅助信 息,我们可以通过反射机制编程实现对这些元数据的访问。

2.自定义注解:
1.– @interface用来声明一个注解
格式为:
1.public @interface 注解名 {定义体}
2. 其中的每一个方法实际上是声明了一个配置参数。
3. 方法的名称就是参数的名称
4. 返回值类型就是参数的类型(返回值类型只能是基本类型、Class、String、enum)
5. 可以通过default来声明参数的默认值。
6. 如果只有一个参数成员,一般参数名为value
注意: 注解元素必须要有值。我们定义注解元素时,经常使用空字符串、0作为默认值。 也经常使用负数(比如:-1)表示不存在的含义
2.元注解:
元注解的作用就是负责注解其他注解。 Java定义了4个标准的 meta-annotation类型,它们被用来提供对其它 annotation 类型作说明。
这些类型和它们所支持的类在java.lang.annotation包中可以 找到 。
– @Target
– @Retention

– @Documented
– @Inherited
1):@Target的作用:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)例如:@Target(value=ElementType.TYPE)
在这里插入图片描述
2.@Retention的作用:
表示需要在什么级别保存该注释信息,用于描述注解的生命周期
在这里插入图片描述
一般使用RUNTIME

案例:

package com.example.jvm.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(value = {ElementType.METHOD,ElementType.TYPE}) //注解在哪些地方可以使用
@Retention(RetentionPolicy.RUNTIME) //表示可以被反射机制读取
public @interface MyAnnotation {

    String name() default ""; //字符串设置默认值
    int age() default 0;  //int 设置默认值
    int id() default -1; //-1表示不存在


}

*现在定义使用一个完整的注解:
案例:使用注解完成类和表结构的映射
在这里插入图片描述
一般自定义注解需要三步:
1.定义注解
2.在类中使用注解
3.通过反射读取注解

package com.example.jvm.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(value = {ElementType.TYPE}) //注解在哪些地方可以使用
@Retention(RetentionPolicy.RUNTIME) //表示可以被反射机制读取
public @interface MyTable {

    String value();
}
package com.example.jvm.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD) //注解在哪些地方可以使用
@Retention(RetentionPolicy.RUNTIME) //表示可以被反射机制读取
public @interface MyFiled {

   String columnName();
   String type();
   int length();
}

package com.example.jvm.annotation;

@MyTable(“tb_student”)
public class SxtStudent {
@MyFiled(columnName = “idd”,type = “int”,length = 10)
private int id;
@MyFiled(columnName = “sname”,type = “varchor”,length = 10)
private String sname;

@MyFiled(columnName = "age",type = "int",length = 10)

private int age;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getSname() {
    return sname;
}

public void setSname(String sname) {
    this.sname = sname;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

}

package com.example.jvm.annotation;


import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) {

        try {
            Class clazz= Class.forName("com.example.jvm.annotation.SxtStudent");
            //获取类的所有有效注解
           Annotation[] annotations= clazz.getAnnotations();
               for(Annotation annotation:annotations)
               {
                   System.out.println("类上的所有注解:"+annotation);
               }

               //获得类的指定注解
              MyTable myTable= (MyTable) clazz.getAnnotation(MyTable.class);
            System.out.println(myTable.value());

   //获得类的属性注解
          Field field= clazz.getDeclaredField("id");
           MyFiled myFiled= field.getAnnotation(MyFiled.class);
            System.out.println(myFiled.columnName()+""+myFiled.type());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • 作者:没有梦想何必远方。
  • 原文链接:https://blog.csdn.net/qq_41799291/article/details/87627722
    更新时间:2022-09-15 09:08:53