- 先说说注解是什么
- 注解(也被称为元数据)为我们在代码中添加信息提供了一种形式化的方法,使我们可以在稍后某个时刻非常方便地使用这些数据。
- 注解是一种标记
- class、interface一样,是一种标记,例如class 代表是这是一个类,interface代表的是这是一个接口,注解@代表的是这是一个注解。
- 自定义注解需要三步
- 定义一个注解(空注解,没有任何的功能)
- 使用aop (常用方式)方式,分析注解,为注解添加功能
- 使用注解
- 自定义注解的操作流程
- 定义一个注解
package com.jamon.main.common.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 元注解(meta-annotation)一共有四种 * 常用的元注解有以下两种: * @Target 用在什么地方: 指明了修饰的这个注解的使用范围,即被描述的注解可以用在哪里。 * ElementType.TYPE:接口、类、枚举、注解 * ElementType.FIELD:字段、枚举的常量 * ElementType.METHOD:方法 * ElementType.PARAMETER:方法参数 * ElementType.CONSTRUCTOR:构造函数 * ElementType.LOCAL_VARIABLE:局部变量 * ElementType.ANNOTATION_TYPE:注解 * ElementType.PACKAGE:包 * * @Retention 注解的生命周期: 指明修饰的注解的生存周期,即会保留到哪个阶段。 * RetentionPolicy.SOURCE:这种类型的Annotations只在源代码级别保留,编译时就会被忽略 * RetentionPolicy.CLASS —— 这种类型的Annotations编译时被保留,在class文件中存在,但JVM将会忽略 * RetentionPolicy.RUNTIME —— 这种类型的Annotations将被JVM保留,所以他们能在运行时被JVM或其他使用反射机制的代码所读取和使用. * * @Documented注解 功能:指明修饰的注解,可以被例如javadoc此类的工具文档化,只负责标记,没有成员取值。 * * @Inherited 允许子类继承父类中的注解。 * 自己实现一个注解 * 1、创建一个注解 * 2、利用aop实现注解功能 */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { String name() default ""; String value() default ""; } - 使用aop 方式,分析注解,为注解添加功能
package com.jamon.main.common.annotation; import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUnit; import cn.hutool.core.date.DateUtil; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import java.lang.reflect.Method; import java.util.Arrays; /** * 注解是使用是在 请求的时候,因为此时使用的是aop的方式实现的注解功能 */ @Aspect @Component public class MyAnnotationAspect { @Pointcut("@annotation(com.jamon.main.common.annotation.MyAnnotation)") public void MyPointCut() { } /** * 1、使用此方式可以直接获取注解中属性的值, * 2、也可以使用方法获取注解的值 * 3、如果要使用分布式锁,则可以使用Redis, RedissonClient方法中的API * * @param joinPoint * @param myAnnotation * @return * @throws Throwable */ @Around("MyPointCut()&&@annotation(myAnnotation)") public Object around(ProceedingJoinPoint joinPoint, MyAnnotation myAnnotation) throws Throwable { DateTime dateStart = DateUtil.date(); // 方法返回结果 Object result = joinPoint.proceed(); DateTime dateEnd = DateUtil.date(); long between = DateUtil.between(dateEnd, dateStart, DateUnit.MINUTE); MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); //获取自定义注解 System.out.println(myAnnotation); myAnnotation = method.getAnnotation(MyAnnotation.class); String name = myAnnotation.name(); String value = myAnnotation.value(); //获取调用的类名方法名 String className = joinPoint.getTarget().getClass().getName(); String methodName = signature.getName(); //方法入参 Object[] args = joinPoint.getArgs(); System.out.println("注解名字: "+ name + ", 注解上的值: " + value); System.out.println("执行时间: " + between + " 分钟 "); System.out.println("执行的类和方法: " + className + "." + methodName + "()"); System.out.println("方法入参: " + Arrays.toString(args)); return result; } } - 使用注解
package com.jamon.main.controller; import com.jamon.main.common.annotation.MyAnnotation; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @Author: issuser * @Date: 2021/2/1 */ @RestController @RequestMapping("hello") public class HelloController { /** * 使用自定义注解,通过aop实现注解功能的实现, 获取注解参数,自定义注解,分析自定义注解 * @return */ @MyAnnotation(value = "18", name = "jamon") @GetMapping("/") public String getIndex(){ int i = 0; return "hello:::" + ++i; } }
- 定义一个注解
springboot 自定义注解
最新推荐文章于 2025-11-09 23:24:15 发布
本文介绍了注解的概念,将其定义为一种元数据,用于在代码中添加信息。讲解了注解类似class和interface,是一种标记。接着阐述了自定义注解的三个步骤:定义注解、使用AOP为注解添加功能以及如何使用自定义注解。
1万+

被折叠的 条评论
为什么被折叠?



