spring boot使用自定义注解做AOP

  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";
}

相关推荐

  1. spring boot使用定义注解AOP

    2024-06-15 17:12:01       9 阅读
  2. springboot aop 定义注解形式

    2024-06-15 17:12:01       34 阅读
  3. SpringBoot使用定义注解AOP实现API接口日志记录

    2024-06-15 17:12:01       42 阅读
  4. 基于AOP实现定义注解

    2024-06-15 17:12:01       14 阅读
  5. 定义注解+AOP实现日志记录

    2024-06-15 17:12:01       13 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-15 17:12:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-15 17:12:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-15 17:12:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-15 17:12:01       18 阅读

热门阅读

  1. 10个Python编程技巧,助你提高开发效率

    2024-06-15 17:12:01       7 阅读
  2. 富格林:正视欺诈阻挠交易被骗

    2024-06-15 17:12:01       5 阅读
  3. 低代码开发:智能财务系统开发应用

    2024-06-15 17:12:01       10 阅读
  4. Web 品质样式表

    2024-06-15 17:12:01       6 阅读
  5. 论徐州高防IP的作用有哪些?

    2024-06-15 17:12:01       8 阅读
  6. postgresql 创建函数

    2024-06-15 17:12:01       8 阅读
  7. 观察者模式

    2024-06-15 17:12:01       8 阅读