SpringBoot自定义注解,实现AOP切面

此处就不解释AOP到底能做什么的了。废话不多说,直接上代码,看效果

定义一个注解

@Target(ElementType.METHOD) //使用位置(类,方法)
@Retention(RetentionPolicy.RUNTIME) //加载到jvm里运行
@Documented
public @interface MyAnnotation {

    String name() default "";//属性,默认值""
    int age() default 23;//属性,默认值23

}

实现这个注解分析类

import io.renren.common.annotation.MyAnnotation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
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.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect     //定义一个切面
@Component
public class MyAnnotationAspect {

    //定义切点,路径为注解的路径
    @Pointcut("@annotation(io.renren.common.annotation.MyAnnotation)")
    public void myPointCut(){
    }
    @Before("myPointCut()")
    public void MyBeforCut(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        String methodName = method.getName();//获取执行方法名
        MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);
        if(null !=  myAnnotation){
            //获取注解参数值
            System.out.println("===========获取注解参数值==========");
            System.out.println("age= "+myAnnotation.age());
            System.out.println("name= "+myAnnotation.name());
            System.out.println("===========获取注解参数值==========");
        }
        //获取方法传递的参数
        Object[] args = joinPoint.getArgs();
        if(null != args){
            for(Object o : args){
                System.out.println("方法传递的参数: "+ o.toString());
            }
        }
        System.out.println("==============MyBeforCut方法,执行接口【"+methodName+"】之前触发=============");
    }

    @AfterReturning("myPointCut()")
    public void MyAfterCut(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        String methodName = method.getName();//获取执行方法名
        System.out.println("==============MyAfterCut方法,执行完接口【"+methodName+"】之后触发=============");
    }
}

再写一个接口


import io.renren.common.annotation.MyAnnotation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/anno")
public class MyAnnotationTest {



    @GetMapping("test")
    @MyAnnotation(name = "iFound")
    public void test(String s1,String s2){
        System.out.println("=============调用了接口方法============");
    }
}

通过postman或者在浏览器中调试接口

http://localhost:端口号/项目名/anno/test?s1=1&s2=333

===========获取注解参数值==========
age= 23
name= iFound
===========获取注解参数值==========
方法传递的参数: 1
方法传递的参数: 333
==============MyBeforCut方法,执行接口【test】之前触发=============
=============调用了接口方法============
==============MyAfterCut方法,执行完接口【test】之后触发=============

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值