月光代碼園

記錄、分享、交流

导航

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;
    }
}

  

posted on 2021-12-22 09:46  bangdikka  阅读(378)  评论(0编辑  收藏  举报