Springboot自定义注解简单应用

作者: adm 分类: java 发布时间: 2023-01-15

从JDK 1.5开始, Java增加了对元数据(MetaData)的支持,也就是 Annotation(注解)。
元注解(meta-annotation)
在JDK 1.5中提供了4个标准的用来对注解类型进行注解的注解类,即元注解,他们分别是:
@Target 描述注解的使用范围
@Retention 描述注解保留的时间范围
@Documented 描述在使用 javadoc 工具为类生成帮助文档时是否要保留其注解信息
@Inherited 使被它修饰的注解具有继承性

自定义注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation {
    String name() default "zhu";
}

测试

public class AnnotationTest {
 
    @MyAnnotation(name = "abc")
    public String hello() {
        return "hello";
    }
 
    public static void main(String[] args) throws NoSuchMethodException {
        Class annotationTestClass = AnnotationTest.class;
 
        Method helloMethod = annotationTestClass.getMethod("hello", new Class[]{});
        Annotation[] annotations = helloMethod.getAnnotations();
 
        for (Annotation annotation : annotations) {
            System.out.println(annotation.annotationType().getName());
        }
        if (helloMethod.isAnnotationPresent(MyAnnotation.class)) {
            MyAnnotation annotation = helloMethod.getAnnotation(MyAnnotation.class);
            System.out.println(annotation.name());
        }
    }
}

运行结果

com...MyAnnotation
abc

使用注解完成对方法切点切入

@Component
@Aspect
public class MyAspect {
 
    @Pointcut("@annotation(com.zhu.annotation.MyAnnotation)")
    private void myAnnotation() {}
 
    @Around("myAnnotation()")
    public void advice(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("around begin...");
        // 获取请求的类名
        String className = joinPoint.getTarget().getClass().getName();
        System.out.print("类名:" + className);
        // 从切面织入点处通过反射机制获取织入点处的方法
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.print(" 方法名:" + method.getName());
 
        // 请求的参数
        Object[] args = joinPoint.getArgs();
        if (args.length != 0) {
            String params = JSONUtils.toJSONString(args);
            System.out.print(" 参数:" + params);
        }
        MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
        if (annotation != null) {
            System.out.println(" 注解属性name:" + annotation.name());
        }
        // 执行原方法
        Object result = joinPoint.proceed();
        System.out.println("around end..." + result);
    }
 
    @Before("myAnnotation()")
    public void before() {
        System.out.println("before...");
    }
 
    @After("myAnnotation()")
    public void after() {
        System.out.println("after...");
    }
}

测试

@RestController
public class TestController {
 
    @MyAnnotation(name = "今天周三了")
    @RequestMapping("/abc")
    public String testAnnotation() {
        System.out.println("自定义注解完成方法切点切入");
        return "明天周四";
    }
}
 

运行效果

around begin...
类名:com...TestController 方法名:testAnnotation 注解属性name:今天周三了
before...
自定义注解完成方法切点切入
after...
around end...明天周四

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!