1.定义某个方法的注解
@Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface MethodsListener {}
MethodsListener这个名字根据对应所需要实现的功能来命名即可
2.实现这个注解
@Component @Slf4j @Aspect @EnableAspectJAutoProxy public class Listener { //这个地方填需要做那种类型的通知 @AfterReturning(value = "pointCut()", returning = "task") public void handle(){ } @Pointcut("@annotation(xxx.xxx.xxx.xxx.xxx.xxx.MethodsListener)") public void pointCut() { } }
3.在某个需要这个注解的方法上面加上这个注解即可@MethodsListener
补充说明:
AspectJ 支持 5 种类型的通知注解:
@Before: 前置通知, 在方法执行之前执行
@After: 后置通知, 在方法执行之后执行 。
@AfterRunning: 返回通知, 在方法返回结果之后执行
@AfterThrowing: 异常通知, 在方法抛出异常之后
@Around: 环绕通知, 围绕着方法执行
标签:MethodsListener,自定义,通知,xxx,注解,方法,public From: https://www.cnblogs.com/ForeverLoveByMe/p/16822139.html