Springboot自定义注解简单应用

从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<AnnotationTest> 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...明天周四

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiha_zhu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值