Spring Boot @Aspect简单使用
@Aspect @Component public class TimeAspect { /** * 切入Controller */ @Pointcut("execution(public * com.yf.game.app.controller.GameRecordController.submitGameRecord(..))") public void execService() { } @Around("execService()") public Object doAround(ProceedingJoinPoint pjp) throws Throwable { boolean canJoin = false; Date nowTime = new Date(); canJoin = activeTimeLimits.canJoinActive(nowTime); if(!canJoin) { throw new TimeLimitException(parseResult(nowTime)); } Object result = pjp.proceed(); return result; } /** * 组装结果 * @param targetTime * @return */ private ApiRest<Map<String,Object>> parseResult(Date targetTime) { ApiRest<Map<String,Object>> apiRest = new ApiRest<Map<String,Object>>(); Map<String, Object> map = new HashMap<String, Object>(); if(activeTimeLimits != null) { map.put("targetTime", targetTime); map.put("dayStatus", activeTimeLimits.isDayStatus()); map.put("dayLimits", activeTimeLimits.getDayLimits()); } apiRest.setCode(3); apiRest.setData(map); apiRest.setMsg("当前时间不可参与考试!!!"); return apiRest; } }