Controller层aop

时间:2023-03-09 18:15:21
Controller层aop

利用@Around通知修改Controller的返回值

自定义一个注解@OperationBtn

Controller层aop

在切入点Controller上加上自定义注解

Controller层aop

接下来就是重点了,AspectJ写切面类,对该Controller 1 @Component
 @Aspect
public class OperationBtnAspect { @Autowired
private AppTableOperationServiceI appTableOperationService; @Around(value = "@annotation(anno)", argNames = "anno")
public AjaxJson aroundMthod(ProceedingJoinPoint point, OperationBtn anno) throws Throwable{
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
HttpServletRequest request = sra.getRequest(); String url = request.getRequestURL().toString(); // http://localhost:8080/5U/appDataController.do (URL:返回的是完整的url,包括Http协议,端口号,servlet名字和映射路径,但它不包含请求参数。)
String method = request.getMethod(); // POST
String uri = request.getRequestURI(); // /5U/appDataController.do
String queryString = request.getQueryString(); // addOrUpdateData&operId=402881e8645f580501645f61202c0044&optId=402881e8645f580501645f61203d004c(返回url路径后面的查询字符串)
queryString = queryString.substring(queryString.indexOf("operId"));
queryString = queryString.substring(,queryString.indexOf("&"));
AppTableOperationEntity operationEntity = appTableOperationService.getEntity(AppTableOperationEntity.class, queryString);
AjaxJson json = (AjaxJson) point.proceed(); //执行Controller中方法,得到返回值json
if(json.isSuccess() && operationEntity !=null && operationEntity.getCallbackSucMsg() !=null){
json.setMsg(operationEntity.getCallbackSucMsg());
}else if(!json.isSuccess() && operationEntity !=null && operationEntity.getCallbackSucMsg() !=null){
json.setMsg(operationEntity.getCallbackFailMsg());
}
return json;
}
}