1 首先定义一下 创建自己的注解 package com.study.controller; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface Myannotion { String key(); }
定义aop 拦截
package com.study.controller; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; @Component @Aspect public class MyannotionAop { @Around("@annotation(myannotion)") public Object doAround(ProceedingJoinPoint proceedingJoinPoint, Myannotion myannotion) { Object object = null; try { // 被注解方法的返回结果 object = proceedingJoinPoint.proceed(); System.out.println(object.toString()); } catch (Throwable throwable) { throwable.printStackTrace(); } // 后 return object; } }
这样 被 方法注解 myannotion的 都会被自己定义的aop 拦截