1. 创建一个自定注解,接收一个传值type
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 EchoStatus {
    String type();
}
  1. 创建一个切面类,绑定一些切面方法,比如before,after…
@Aspect
@Component
@Slf4j
public class EchoStatusAspect {

    @Pointcut("@annotation(com.gbs.mgt.annotation.EchoStatus)")
    public void customPointcut() {
    }

    @Before("customPointcut()")
    public void beforeAdvice(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        System.out.println("Before method execution: " + joinPoint.getSignature().getName()+"入参:"+ Arrays.asList(args));
    }

    @After(value = "customPointcut()")
    public void afterAdvice(JoinPoint joinPoint) {

        System.out.println("After method execution: " + joinPoint.getSignature().getName());
    }

    @AfterReturning(value = "customPointcut()", returning = "result")
    public void afterReturningAdvice(JoinPoint joinPoint, Object result) {

        System.out.println("After method execution: " + joinPoint.getSignature().getName()+"结果:"+result);
    }
}
@EchoStatus (type = "无所谓")
public String index(){
	return "hello word";
}

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部