- 创建一个自定注解,接收一个传值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();
}
- 创建一个切面类,绑定一些切面方法,比如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
发表评论 取消回复