直接贴代码了~~
/** * @Author Song * @Date 2020/5/26 9:50 * @Version 1.0 */ @Slf4j @Aspect @Component public class EagleEyeAspect { @Pointcut("@annotation(com.ctgu.song.plantfactory.v2.annotation.EagleEye)") public void eagleEye() { } @Around("eagleEye()") public Object around(ProceedingJoinPoint pjp) throws Throwable { long begin = System.currentTimeMillis(); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); Signature signature = pjp.getSignature(); MethodSignature methodSignature = (MethodSignature) signature; Method method = methodSignature.getMethod(); EagleEye eagleEye = method.getAnnotation(EagleEye.class); String desc = eagleEye.desc(); log.info("============请求开始=========="); log.info("请求链接:{}", request.getRequestURI().toString()); log.info("接口描述:{}", desc); log.info("请求类型:{}", request.getMethod()); log.info("请求方法:{}.{}", signature.getDeclaringTypeName(), signature.getName()); log.info("请求IP:{}", request.getRemoteAddr()); log.info("请求入参:{}", JSON.toJSONString(pjp.getArgs())); Object result = pjp.proceed(); long end = System.currentTimeMillis(); log.info("请求耗时:{}ms", end - begin); log.info("请求返回:{}", JSON.toJSONString(result)); log.info("=============请求结束==========="); return result; } }在@Pointcut里通过@annotation来配置切点,代表我们的AOP切面会切到所有用EagleEye注解修饰的类。
然后使用@Around环绕通知在被注解的方法前后执行一些代码
Object result = pjp.proceed();
这行代码之前就是执行目标方法之前需要执行的代码 ,这行代码之后就是执行目标方法之后需要执行的代码
3. 注解的使用只需要在需要被注解的方法上面使用自己的注解就行了 这里拿我自己项目中的一个Controller中的方法举例
@EagleEye(desc = "分页查询实验") @GetMapping("/experiment") @ApiOperation("分页查询实验") public RsBody<Page<ExperimentVO2>> pageExperiment(ExperimentQueryDTO queryDTO) { log.info("请求分页查询实验的方法pageExperiment,请求参数为{}", queryDTO.toString()); RsBody<Page<ExperimentVO2>> rsBody = new RsBody<>(); IPage<Experiment> page = experimentV2Service.page(new Page<>(queryDTO.getCurrent() - 1, queryDTO.getSize()), new LambdaQueryWrapper<Experiment>() .like(queryDTO.getExperimentId() != null, Experiment::getExperimentId, queryDTO.getExperimentId()) .eq(queryDTO.getExperimentStatus() != null, Experiment::getExperimentStatus, queryDTO.getExperimentStatus()) .between(queryDTO.getStartTime() != null && queryDTO.getEndTime() != null, Experiment::getStartTime, queryDTO.getStartTime(), queryDTO.getEndTime()) .orderBy(true, false, Experiment::getExperimentId)); //组装Vo List<ExperimentVO2> experimentVOList = new ArrayList<>(); for (Experiment experiment : page.getRecords()) { ExperimentVO2 experimentVO = new ExperimentVO2(); experimentVO.setExperimentId(experiment.getExperimentId()); PlantInfo byPlantId = plantService.findByPlantId(experiment.getPlantId()); if (byPlantId != null) { experimentVO.setPlantName(byPlantId.getPlantName()); } else { experimentVO.setPlantName("植物被删除"); } experimentVO.setStartTime(experiment.getStartTime()); experimentVO.setEndTime(experiment.getEndTime()); experimentVO.setExperimentPurpose(experiment.getExperimentPurpose()); experimentVO.setExperimentDescription(experiment.getExperimentDescription()); experimentVO.setExperimentAddress(experiment.getExperimentAddress()); experimentVO.setExperimentPersonName(userService.findById(experiment.getExperimentPersonId()).getUserName()); experimentVO.setCronType(experiment.getCronType()); experimentVO.setExperimentStatus(experiment.getExperimentStatus()); experimentVO.setExperimentResult(experiment.getExperimentResult()); experimentVOList.add(experimentVO); } Page<ExperimentVO2> pageVo = new Page<ExperimentVO2>(); pageVo.setPages(page.getPages()); pageVo.setRecords(experimentVOList); pageVo.setTotal(page.getTotal()); pageVo.setSize(page.getSize()); pageVo.setCurrent(page.getCurrent()); return rsBody.setBody(true).setData(pageVo); } 4.测试情况好的 万事俱备 让我们运行一下程序 并访问这个方法 (过程略过)