• 自定义注解


    一、Java注解简介

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

    Java注解分类

    JDK基本注解
    JDK元注解
    自定义注解

    JDK基本注解

    @Override
    重写
    
    @SuppressWarnings(value = "unchecked")
    压制编辑器警告
    
    • 1
    • 2
    • 3
    • 4
    • 5

    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工具提取成文档.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    自定义注解

    注解分类(根据Annotation是否包含成员变量,可以把Annotation分为两类):
    
    标记Annotation:
    没有成员变量的Annotation; 这种Annotation仅利用自身的存在与否来提供信息
    
    元数据Annotation:
    包含成员变量的Annotation; 它们可以接受(和提供)更多的元数据;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    如何自定义注解?

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

    二、自定义注解

    基本定义规则

    @interface 是修饰注解类

    @Target(决定自定义注解用在哪)

    //ElementType.TYPE 指注解可以用在类上面
    //ElementType.METHOD 指注解可以用在属性上面
    //ElementType.FIELD 指用在方法上面

    测试
    MyAnnptation1

    package com.xlb.ssm.annotation;
    
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    /**
     * @author javaxlb
     * @site www.xlb.com
     * @create  2022-10-30 13:41
     */
    //@interface 是修饰注解类
    //ElementType.TYPE 指注解可以用在类上面
    //ElementType.METHOD  指注解可以用在属性上面
    //ElementType.FIELD  指用在方法上面
    @Retention(RetentionPolicy.SOURCE)
    @Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})
    public @interface MyAnnptation1 {
      
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    StudentController

    package com.xlb.ssm.annotation;
    
    /**
     * @author javaxlb
     * @site www.xlb.com
     * @create  2022-10-30 13:43
     */
    @MyAnnptation1()
    public class StudentController {
    
        @MyAnnptation1
        private String id;
    
        @MyAnnptation1
        private String name;
    
        @MyAnnptation1
        public void text(){
            System.out.println("测试。。。。。。。");
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    @Retention

    在注解类中定义的属性方法,都称为注解类的属性

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

    其中注解的属性为value时是可以省略的
    一般都会给一个default默认值

    StudentController

    package com.xlb.ssm.annotation;
    
    /**
     * @author javaxlb
     * @site www.xlb.com
     * @create  2022-10-30 13:43
     */
    @MyAnnptation1(desc="在类上面标记")
    public class StudentController {
    
        @MyAnnptation1("标记在属性id上面")
        private String id;
    
        @MyAnnptation1("标记在属性id上面")
        private String name;
    
        @MyAnnptation1
        public void text(){
            System.out.println("测试。。。。。。。");
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    MyAnnptation1

    package com.xlb.ssm.annotation;
    
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    /**
     * @author javaxlb
     * @site www.xlb.com
     * @create  2022-10-30 13:41
     */
    //@interface 是修饰注解类
    //ElementType.TYPE 指注解可以用在类上面
    //ElementType.METHOD  指注解可以用在属性上面
    //ElementType.FIELD  指用在方法上面
    @Retention(RetentionPolicy.SOURCE)
    @Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})
    public @interface MyAnnptation1 {
    
        //指的是注解的属性
        public String value() default "value可以修饰类、方法、属性";
        //指的是注解的属性
        public String desc() default "desc可以修饰类、方法、属性";
    
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    案例

    获取类上的注解值

    StudentController

    package com.xlb.ssm.annotation;
    
    /**
     * @author javaxlb
     * @site www.xlb.com
     * @create  2022-10-30 13:43
     */
    @MyAnnptation1(desc="在类上面标记")
    public class StudentController {
    
        @MyAnnptation1("标记在属性id上面")
        private String id;
    
        @MyAnnptation1("标记在属性name上面")
        private String name;
    
        @MyAnnptation1
        public void text(@MyAnnptation2("用来修饰参数id") String id,@MyAnnptation2("用来修饰参数name") String name){
            System.out.println("测试。。。。。。。");
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    Demo1

    package com.xlb.ssm.annotation.demo;
    
    import com.xlb.ssm.annotation.MyAnnptation1;
    import com.xlb.ssm.annotation.StudentController;
    
    /**
     * @author javaxlb
     * @site www.xlb.com
     * @create  2022-10-30 16:35
     *
     * 案例:
     * 1.获取StudentController 类上定义注解中类内容
     * 2.获取StudentController 方法上定义注解中类内容
     * 3.获取StudentController 属性上定义注解中类内容
     * 4.获取StudentController 参数上定义注解中类内容
     *
     */
    public class Demo1 {
        public static void main(String[] args) {
    
            //反射拿到类的注解
            MyAnnptation1 annotation = StudentController.class.getAnnotation(MyAnnptation1.class);
            System.out.println(annotation.desc());
            System.out.println(annotation.value());
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    在这里插入图片描述

    获取属性上的注解值

    Demo1

    package com.xlb.ssm.annotation.demo;
    
    import com.xlb.ssm.annotation.MyAnnptation1;
    import com.xlb.ssm.annotation.MyAnnptation2;
    import com.xlb.ssm.annotation.StudentController;
    
    import java.lang.reflect.Field;
    
    /**
     * @author javaxlb
     * @site www.xlb.com
     * @create  2022-10-30 16:35
     *
     * 案例:
     * 1.获取StudentController 类上定义注解中类内容
     * 2.获取StudentController 方法上定义注解中类内容
     * 3.获取StudentController 属性上定义注解中类内容
     * 4.获取StudentController 参数上定义注解中类内容
     *
     */
    public class Demo1 {
        public static void main(String[] args) throws Exception {
    
            //反射拿到类的注解
            MyAnnptation1 annotation = StudentController.class.getAnnotation(MyAnnptation1.class);
            System.out.println(annotation.desc());
            System.out.println(annotation.value());
    
            //获取属性上的注解值
            Field id = StudentController.class.getDeclaredField("id");
            Field name = StudentController.class.getDeclaredField("name");
    
            System.out.println(id.getAnnotation(MyAnnptation1.class).value());
            System.out.println(name.getAnnotation(MyAnnptation1.class).value());
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    在这里插入图片描述

    获取方法上的注解值

    Demo1

    package com.xlb.ssm.annotation.demo;
    
    import com.xlb.ssm.annotation.MyAnnptation1;
    import com.xlb.ssm.annotation.MyAnnptation2;
    import com.xlb.ssm.annotation.StudentController;
    
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    
    /**
     * @author javaxlb
     * @site www.xlb.com
     * @create  2022-10-30 16:35
     *
     * 案例:
     * 1.获取StudentController 类上定义注解中类内容
     * 2.获取StudentController 方法上定义注解中类内容
     * 3.获取StudentController 属性上定义注解中类内容
     * 4.获取StudentController 参数上定义注解中类内容
     *
     */
    public class Demo1 {
        public static void main(String[] args) throws Exception {
    
            //反射拿到类的注解
            MyAnnptation1 annotation = StudentController.class.getAnnotation(MyAnnptation1.class);
            System.out.println(annotation.desc());
            System.out.println(annotation.value());
    
            //获取属性上的注解值
            Field id = StudentController.class.getDeclaredField("id");
            Field name = StudentController.class.getDeclaredField("name");
    
            System.out.println(id.getAnnotation(MyAnnptation1.class).value());
            System.out.println(name.getAnnotation(MyAnnptation1.class).value());
    
    
    
            //获取方法上的注解值
            Method text = StudentController.class.getDeclaredMethod("text", String.class, String.class);
            System.out.println(text.getAnnotation(MyAnnptation1.class).value());
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    在这里插入图片描述

    获取参数上定义注解中类内容

    Demo1

    package com.xlb.ssm.annotation.demo;
    
    import com.xlb.ssm.annotation.MyAnnptation1;
    import com.xlb.ssm.annotation.MyAnnptation2;
    import com.xlb.ssm.annotation.StudentController;
    
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    import java.lang.reflect.Parameter;
    
    /**
     * @author javaxlb
     * @site www.xlb.com
     * @create  2022-10-30 16:35
     *
     * 案例:
     * 1.获取StudentController 类上定义注解中类内容
     * 2.获取StudentController 方法上定义注解中类内容
     * 3.获取StudentController 属性上定义注解中类内容
     * 4.获取StudentController 参数上定义注解中类内容
     *
     */
    public class Demo1 {
        public static void main(String[] args) throws Exception {
    
            //反射拿到类的注解
            MyAnnptation1 annotation = StudentController.class.getAnnotation(MyAnnptation1.class);
            System.out.println(annotation.desc());
            System.out.println(annotation.value());
    
            //获取属性上的注解值
            Field id = StudentController.class.getDeclaredField("id");
            Field name = StudentController.class.getDeclaredField("name");
    
            System.out.println(id.getAnnotation(MyAnnptation1.class).value());
            System.out.println(name.getAnnotation(MyAnnptation1.class).value());
    
    
    
            //获取方法上的注解值
            Method text = StudentController.class.getDeclaredMethod("text", String.class, String.class);
            System.out.println(text.getAnnotation(MyAnnptation1.class).value());
    
            //获取参数上定义注解中类内容
            for (Parameter p : text.getParameters()) {
                System.out.println(p.getAnnotation(MyAnnptation2.class).value());
            }
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    在这里插入图片描述

    三、自定义注解日志使用

    MyLog

    package com.xlb.ssm.annotation.aop;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyLog {
        String desc();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    DemoController

    package com.xlb.ssm.annotation.aop;
    
    import org.springframework.stereotype.Controller;
    
    /**
     * @author javaxlb
     * @site www.xlb.com
     * @create  2022-10-30 17:08
     */
    
    @Controller
    public class DemoController {
    
        @MyLog(desc = "这是一个测试类的方法")
        public void text(){
            System.out.println("测试方法");
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    ==AnnotationTest ==

    package com.xlb.shiro;
     
    import com.zking.ssm.annotation.aop.DemoController;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     
    /**
     * @author javaxlb
     * @create  2022-10-30 19:29
     */
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations={"classpath:applicationContext.xml"})
    public class AnnotationTest {
        @Autowired
        private DemoController demoController;
     
        @Test
        public void test1(){
            demoController.test();
        }
     
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    MyLogAspect

    package com.xlb.ssm.annotation.aop;
     
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.aspectj.lang.reflect.MethodSignature;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;
     
    @Component
    @Aspect
    public class MyLogAspect {
        private static final Logger logger = LoggerFactory.getLogger(MyLogAspect.class);
     
        /**
         * 只要用到了com.javaxl.p2.annotation.springAop.MyLog这个注解的,就是目标类
         */
        @Pointcut("@annotation(com.zking.ssm.annotation.aop.Mylog)")
        private void MyValid() {
        }
     
        @Before("MyValid()")
        public void before(JoinPoint joinPoint) {
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            logger.debug("[" + signature.getName() + " : start.....]");
            System.out.println("[" + signature.getName() + " : start.....]");
     
            Mylog myLog = signature.getMethod().getAnnotation(Mylog.class);
            logger.debug("【目标对象方法被调用时候产生的日志,记录到日志表中】:"+myLog.desc());
            System.out.println("【目标对象方法被调用时候产生的日志,记录到日志表中】:" + myLog.desc());
     
     
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    在这里插入图片描述

  • 相关阅读:
    R语言时间序列数据提取:使用xts包的first函数提取时间序列中最前面两周的数据(first 2 week)
    OpenCV实现图像的礼帽和黑帽
    多线程锁-synchronized字节码分析
    mysql索引失效的几种情况
    【C++编程能力提升】
    static成员,代码块,内部类
    学习java的第三十天。。。(网络编程)
    Vue(3.3.4)+three.js(0.161.0)实现3D可视化地图
    EAP-TLS实验之Ubuntu20.04环境搭建配置(FreeRADIUS3.0)(一)
    C++搜索二叉树
  • 原文地址:https://blog.csdn.net/qq_63531917/article/details/127598567