1.定义一个注解
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface TestOut {
String value() default "";
}
2.编写切面文件
@Aspect
@Component
@SuppressWarnings({"unused"})
public class TestOutAspect {
public static final Logger logger= LoggerFactory.getLogger(TestOutAspect.class);
public static final String Token="token";
@Pointcut("@annotation(com.pa.xj.qa.matrix.biz.processor.service.nouse.TestOut)")
public void annotationPointcut() {
}
@Before("annotationPointcut()")
public void beforePointcut(JoinPoint joinPoint) {
logger.info("我的注解 before");
// 此处进入到方法前 可以实现一些业务逻辑
}
@Around("annotationPointcut()")
public Object arount(ProceedingJoinPoint joinPoint) throws Throwable{
logger.info("我的注解 around");
return joinPoint.proceed();
}
@AfterReturning("annotationPointcut()")
public void doAfterReturning(JoinPoint joinPoint) {
logger.info("我的注解 after");
}
}
3.使用注解
@GetMapping(value = "/hs")
@TestOut
public GenericResponse<String> hstest() {
GenericResponse<String> resp = new GenericResponse<>();
resp.setStatus(ResponseConstants.STATUS_SUCCESS);
resp.setMessage("OK");
return resp;
}
4.效果
image.png
网友评论