• SpringBoot自定义注解


    Java注解简介

    Java注解是附加在代码中的一些元信息,用于一些工具在编译、运行时进行解析和使用,起到说明、配置的功能。 注解相关类都包含在java.lang.annotation包中。

    Java注解分类:JDK基本注解、JDK元注解、自定义注解

    JDK基本注解:

    @Override
    重写

    @SuppressWarnings(value = "unchecked")
    压制编辑器警告

    JDK元注解  

    @Retention:定义注解的保留策略
    @Retention(RetentionPolicy.SOURCE)             //注解仅存在于源码中,在class字节码文件中不包含
    @Retention(RetentionPolicy.CLASS)              //默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,
    @Retention(RetentionPolicy.RUNTIME)            //注解会在class字节码文件中存在,在运行时可以通过反射获取到

    @Target:指定被修饰的Annotation可以放置的位置(被修饰的目标)
    @Target(ElementType.TYPE)                      //接口、类
    @Target(ElementType.FIELD)                     //属性
    @Target(ElementType.METHOD)                    //方法
    @Target(ElementType.PARAMETER)                 //方法参数
    @Target(ElementType.CONSTRUCTOR)               //构造函数
    @Target(ElementType.LOCAL_VARIABLE)            //局部变量
    @Target(ElementType.ANNOTATION_TYPE)           //注解
    @Target(ElementType.PACKAGE)                   //包
    注:可以指定多个位置,例如:
    @Target({ElementType.METHOD, ElementType.TYPE}),也就是此注解可以在方法和类上面使用

    @Inherited:指定被修饰的Annotation将具有继承性

    @Documented:指定被修饰的该Annotation可以被javadoc工具提取成文档.

    自定义注解

    注解分类(根据Annotation是否包含成员变量,可以把Annotation分为两类):

    标记Annotation:
    没有成员变量的Annotation; 这种Annotation仅利用自身的存在与否来提供信息

    元数据Annotation:
    包含成员变量的Annotation; 它们可以接受(和提供)更多的元数据;

    二、自定义注解应用 

    如何自定义注解?

    使用@interface关键字, 其定义过程与定义接口非常类似, 需要注意的是:
       Annotation的成员变量在Annotation定义中是以无参的方法形式来声明的, 其方法名和返回值类型定义了该成员变量的名字和类型,
       而且我们还可以使用default关键字为这个成员变量设定默认值;

    Myannotation1 :

    1. package com.zjy.ssm.annotation;
    2. import java.lang.annotation.*;
    3. /**
    4. * @author 小猪
    5. * @create 2022-10-27 11:41
    6. * 注解可以用在类、接口、属性、方法上
    7. * 注解运行期也保留
    8. * 不可继承
    9. */
    10. @Target({ElementType.TYPE, ElementType.FIELD,ElementType.METHOD})
    11. @Retention(RetentionPolicy.RUNTIME)
    12. public @interface Myannotation1 {
    13. // 指的是注解中的属性
    14. public String desc() default "desc可以修饰类、方法、属性";
    15. public String value() default "value可以修饰类、方法、属性";
    16. }

    StudentContoller:

    1. package com.zjy.ssm.annotation;
    2. /**
    3. * @author xiaozhu
    4. * @create 2022-10-27 14:25
    5. */
    6. @Myannotation1(desc = "标记在类上面")
    7. public class StudentController {
    8. @Myannotation1("标记在属性id上面")
    9. private String id;
    10. @Myannotation1("标记在属性name上面")
    11. private String name;
    12. @Myannotation1
    13. public void test1(@Myannotation2("用来修饰id参数") String id,@Myannotation2("用来修饰name参数") String name){
    14. System.out.println("测试...");
    15. }
    16. }

    Myannotation2 :

    1. package com.zjy.ssm.annotation;
    2. import java.lang.annotation.ElementType;
    3. import java.lang.annotation.Retention;
    4. import java.lang.annotation.RetentionPolicy;
    5. import java.lang.annotation.Target;
    6. /**
    7. * @author xiaozhu
    8. * @create 2022-10-27 11:41
    9. * 注解可以用在类、接口、属性、方法上
    10. * 注解运行期也保留
    11. * 不可继承
    12. */
    13. @Target({ElementType.PARAMETER})
    14. @Retention(RetentionPolicy.RUNTIME)
    15. public @interface Myannotation2 {
    16. // 指的是注解中的属性
    17. public String desc() default "desc可以修饰类、方法、属性";
    18. public String value() default "value可以修饰类、方法、属性";
    19. }

     

     

    Demo1 :

    如何获取注解上的类、如何获取注解上的方法、如何获取注解上的属性、如何获取注解上的参数

    1. package com.zjy.ssm.annotation.demo;
    2. import com.zjy.ssm.annotation.Myannotation1;
    3. import com.zjy.ssm.annotation.Myannotation2;
    4. import com.zjy.ssm.annotation.StudentController;
    5. import java.lang.reflect.Field;
    6. import java.lang.reflect.Method;
    7. import java.lang.reflect.Parameter;
    8. /**
    9. * @author 小猪
    10. * @create 2022-10-27 15:14
    11. *
    12. * 目标:
    13. * 1、获取studentController 类上自定义注解中的内容
    14. * 2、获取studentController 方法上自定义注解中的内容
    15. * 3、获取studentController 属性上自定义注解中的内容
    16. * 4、获取studentController 参数上自定义注解中的内容
    17. *
    18. * * *..*Service.*pager(...)
    19. *
    20. */
    21. public class Demo1 {
    22. public static void main(String[] args) throws Exception {
    23. Myannotation1 annotation = StudentController.class.getAnnotation(Myannotation1.class);
    24. System.out.println(annotation.value());
    25. System.out.println(annotation.desc());
    26. // 获取属性上的
    27. Field id = StudentController.class.getDeclaredField("id");
    28. Field name = StudentController.class.getDeclaredField("name");
    29. // Myannotation2 annotation2 = id.getAnnotation(Myannotation2.class);
    30. System.out.println(id.getAnnotation(Myannotation1.class).value());
    31. System.out.println(name.getAnnotation(Myannotation1.class).value());
    32. //获取方法上的
    33. Method m1 = StudentController.class.getDeclaredMethod("test1", String.class, String.class);
    34. System.out.println(m1.getAnnotation(Myannotation1.class).value());
    35. // 获取参数上的标识
    36. for (Parameter p:m1.getParameters()){
    37. System.out.println(p.getAnnotation(Myannotation2.class).value());
    38. }
    39. // Field[] declaredFields = StudentController.class.getDeclaredFields();
    40. // for (Field f : declaredFields){
    41. // Annotation annotation1=f.getAnnotation(Myannotation1.class);
    42. // if (annotation !=null){
    43. //
    44. // }
    45. // }
    46. }
    47. }

     

  • 相关阅读:
    D2--FPGA SPI接口通信2022-08-03
    【实战】jenkins api 接口
    ubuntu2004 有线与另一个Ubuntu系统通信
    PMP每日一练 | 考试不迷路-11.07(包含敏捷+多选)
    空间曲面@常见曲面方程
    每日一学vue2:自定义绑定事件解除方式($off)及销毁vc
    redis报错 Error getaddrinfo ENOTFOUND
    腾讯JAVA后端秋招面试总结
    服务网格新篇章:Eureka与分布式服务网格的协同共舞
    2022-08-06 第四小组 修身课 学习笔记(every day)
  • 原文地址:https://blog.csdn.net/m0_67477525/article/details/127576623