spring boot使用自定义注解做AOP的案例代码
更新时间:2024年06月12日 09:29:28 作者:Byte Beat
这篇文章主要介绍了spring boot使用自定义注解做AOP的案例代码,代码简单易懂,通过创建一个自定注解,接收一个传值type,感兴趣的朋友一起看看吧
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();
}2、创建一个切面类,绑定一些切面方法,比如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";
}到此这篇关于spring boot使用自定义注解做AOP的文章就介绍到这了,更多相关spring boot使用自定义注解内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Java 实用注解篇之@Qualifier 深度解析及实战案例
在Spring框架中,@Qualifier是一个常见的注解,主要用于解决依赖注入(DI)时的歧义性,本文给大家介绍Java 实用注解篇之@Qualifier 深度解析及实战案例,感兴趣的朋友一起看看吧2025-06-06
SpringBoot集成PostgreSQL并设置最大连接数
本文主要介绍了SpringBoot集成PostgreSQL并设置最大连接数,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2023-11-11
Spring-Cloud-Function-Spel 漏洞环境搭建
这篇文章主要介绍了Spring-Cloud-Function-Spel 漏洞复现及搭建方法,搭建方法也很简单,首先需要安装maven jdk,具体安装过程跟随小编一起看看吧2022-03-03
No ‘Access-Control-Allow-Origin‘ header is&nb
这篇文章主要介绍了No ‘Access-Control-Allow-Origin‘ header is present跨域及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2023-02-02


最新评论