运用切面对某个方法的调用次数进行统计:
开发中,有个统计某个接口的前端调用次数,谁想到用调用次数来反映下载次数,我也是醉了,写完后总结如下:
Action类:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Action {
String name();
}
ApiVisitHistory历史统计类
@Component
@Aspect
public class ApiVisitHistory {
@Resource
private DocManageMapper docManageMapper;
@Resource
private RegulationMapper regulationMapper;
private Logger log = LoggerFactory.getLogger(ApiVisitHistory.class);
private ThreadLocal<Long> startTime = new ThreadLocal<>();
@Pointcut("@annotation(com.cmict.oneconstruction.microservice.system.document.aspect.Action))")
public void pointCut(){
}
@Before("pointCut()")
public void doBefore(JoinPoint joinPoint) {
startTime.set(System.currentTimeMillis());
Object[] args = joinPoint.getArgs();
AtomicCounter.getInstance().increase();
}
@AfterReturning(returning = "returnVal", pointcut = "pointCut()")
public void doAfterReturning(JoinPoint joinPoint, Object returnVal) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String type = request.getRequestURI().split("/")[2];
if("regulation".equals(request.getRequestURI().split("/")[2])){
String regulationId = request.getRequestURI().split("/")[3];
String updateFreq =String.valueOf(Integer.parseInt(regulationMapper.selectRegulationById(regulationId).getDownloadFrequency())+ 1);
Regulation regulation = new Regulation();
regulation.setDownloadFrequency(updateFreq);
regulation.setRegulationId(regulationId);
regulationMapper.updateRegulationInfo(regulation);
}else if("doc".equals(request.getRequestURI().split("/")[2])){
String docid = request.getRequestURI().split("/")[3];
String updateFreq = String.valueOf(Integer.parseInt(docManageMapper.selectDocManageById(docid).getDownloadFrequency())+1);
DocManage docManage = new DocManage();
docManage.setDownloadFrequency(updateFreq);
docManage.setDocId(docid);
docManageMapper.updateDocManageInfo(docManage);
}
}
@AfterThrowing(pointcut = "pointCut()")
public void doAfterThrowing(JoinPoint joinPoint) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
}
}
其中的“@annotation(com.cmict.oneconstruction.microservice.system.document.aspect.Action))”是对加了@Action注解的进行AOP
AtomicCounter计数工具类
public class AtomicCounter {
private static final AtomicCounter atomicCounter = new AtomicCounter();
private AtomicCounter() {
}
public static AtomicCounter getInstance() {
return atomicCounter;
}
private static AtomicInteger counter = new AtomicInteger();
public int getValue() {
return counter.get();
}
public int increase() {
return counter.incrementAndGet();
}
public int decrease() {
return counter.decrementAndGet();
}
}
在需要统计的controller方法上加@Action即可:
/**
* 根据档案资料表的主键ID查询详细的档案资料信息
*
* @param id 档案资料表主键
* @return {@link AjaxResult}
*/
@Action(name = "注解式拦截的add操作")
@ApiOperation("根据档案资料表的主键ID查询详细的档案资料信息")
@ApiImplicitParam(name = "id", value = "档案资料表的ID", required = true, paramType = "String")
@GetMapping(value = "/{id}")
public AjaxResult getDocInfo(@PathVariable("id") String id) {
return AjaxResult.success(this.documentService.selectDocManageById(id));
}
@Action(name = “注解式拦截的add操作”)
原创不易,期待三连