spring boot使用自定义注解做AOP的案例代码
作者: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使用自定义注解内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
