1.知识点
通知(Advice)
通知描述了切面要完成的工作以及何时执行。比如我们的日志切面需要记录每个接口调用时长,就需要在接口调用前后分别记录当前时间,再取差值。
前置通知(Before):在目标方法调用前调用通知功能;
后置通知(After):在目标方法调用之后调用通知功能,不关心方法的返回结果;
返回通知(AfterReturning):在目标方法成功执行之后调用通知功能;
异常通知(AfterThrowing):在目标方法抛出异常后调用通知功能;
环绕通知(Around):通知包裹了目标方法,在目标方法调用之前和之后执行自定义的行为。
连接点(JoinPoint)
通知功能被应用的时机。比如接口方法被调用的时候就是日志切面的连接点。
切点(Pointcut)
切点定义了通知功能被应用的范围。比如日志切面的应用范围就是所有接口,即所有controller层的接口方法。
切面(Aspect)
切面是通知和切点的结合,定义了何时、何地应用通知功能。
引入(Introduction)
在无需修改现有类的情况下,向现有的类添加新方法或属性。
织入(Weaving)
把切面应用到目标对象并创建新的代理对象的过程。
2.书写方式
execution(public * com.macro.mall.tiny.controller..(…))
切入点指示符用来指示切入点表达式目的,在 Spring AOP 中目前只有执行方法这一个连接点,Spring AOP 支持的 AspectJ 切入点指示符,切入点表达式可以使用 &&、||、!来组合切入点表达式,还可以使用类型匹配的通配符来进行匹配
实际操作 指定多个包可以前置通知:
@Pointcut("execution(public * com.ewaytek.edf.web.controllers.a||b..*(..))")
只对加了自定义注解的方法切入
@Pointcut("@annotation(LogSave)")
3.代码
可自定义注解来配置
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface testTarget {
String testName() default “”;
String testContent() default “”;
int page() default 0;
}
代码实践
添加AOP切面实现接口日志记录
添加日志信息封装类WebLog
/**
* Controller层的日志封装类
* Created by macro on 2018/4/26.
*/
public class WebLog {
/**
* 操作描述
*/
private String description;
/**
* 操作用户
*/
private String username;
/**
* 操作时间
*/
private Long startTime;
/**
* 消耗时间
*/
private Integer spendTime;
/**
* 根路径
*/
private String basePath;
/**
* URI
*/
private String uri;
/**
* URL
*/
private String url;
/**
* 请求类型
*/
private String method;
/**
* IP地址
*/
private String ip;
/**
* 请求参数
*/
private Object parameter;
/**
* 请求返回的结果
*/
private Object result;
/**
* 请求参数
*/
private Object pageSize;
/**
* 请求参数
*/
private Object pageNum;
public Object getPageSize() {
return pageSize;
}
public void setPageSize(Object pageSize) {
this.pageSize = pageSize;
}
public Object getPageNum() {
return pageNum;
}
public void setPageNum(Object pageNum) {
this.pageNum = pageNum;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Long getStartTime() {
return startTime;
}
public void setStartTime(Long startTime) {
this.startTime = startTime;
}
public Integer getSpendTime() {
return spendTime;
}
public void setSpendTime(Integer spendTime) {
this.spendTime = spendTime;
}
public String getBasePath() {
return basePath;
}
public void setBasePath(String basePath) {
this.basePath = basePath;
}
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public Object getParameter() {
return parameter;
}
public void setParameter(Object parameter) {
this.parameter = parameter;
}
public Object getResult() {
return result;
}
public void setResult(Object result) {
this.result = result;
}
}
添加切面类WebLogAspect
定义了一个日志切面,在环绕通知中获取日志需要的信息,并应用到controller层中所有的public方法中去。
/**
* 统一日志处理切面
* Created by macro on 2018/4/26.
*/
@Aspect
@Component
@Order(1)
public class WebLogAspect {
private static final Logger LOGGER = LoggerFactory.getLogger(WebLogAspect.class);
@Pointcut("execution(public * com.macro.mall.tiny.controller.*.*(..))")
@Pointcut("@annotation(LogSave)")//通过注解
public void webLog() {
}
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
System.err.println("开始");
}
@AfterReturning(value = "webLog()", returning = "ret")
public void doAfterReturning(Object ret) throws Throwable {
}
@Around("webLog()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
long startTime = System.currentTimeMillis();
//获取当前请求对象
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
//记录请求信息
WebLog webLog = new WebLog();
Object result = joinPoint.proceed();
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
List<Map<String, Object>> listMap=(List)getParameter(method, joinPoint.getArgs());
for (Map<String, Object> map : listMap) {
System.out.println(map);
//使用map.keySet()遍历map,map.keySet()获取map中key的值
for (String s : map.keySet()) {
//map.get(key)获取value
System.out.println(s + "------>" + map.get(s) + " ");
if(s.equals("pageNum")){
webLog.setPageNum( map.get(s));
}
if(s.equals("pageSize")){
webLog.setPageSize( map.get(s));
}
}
}
if (method.isAnnotationPresent(testTarget.class)) {
testTarget testTargetDemo = method.getAnnotation(testTarget.class);
webLog.setDescription(testTargetDemo.testContent());
webLog.setMethod(testTargetDemo.testName());
}
String urlStr = request.getRequestURL().toString();
webLog.setBasePath(StrUtil.removeSuffix(urlStr, URLUtil.url(urlStr).getPath()));
webLog.setIp(request.getRemoteUser());
webLog.setParameter(getParameter(method, joinPoint.getArgs()));
webLog.setResult(result);
webLog.setStartTime(startTime);
webLog.setUri(request.getRequestURI());
webLog.setUrl(request.getRequestURL().toString());
LOGGER.info("{}", JSONUtil.parse(webLog));
return result;
}
/**
* 根据方法和传入的参数获取请求参数
*/
private Object getParameter(Method method, Object[] args) {
List<Object> argList = new ArrayList<>();
Parameter[] parameters = method.getParameters();
for (int i = 0; i < parameters.length; i++) {
//将RequestBody注解修饰的参数作为请求参数
RequestBody requestBody = parameters[i].getAnnotation(RequestBody.class);
if (requestBody != null) {
argList.add(args[i]);
}
//将RequestParam注解修饰的参数作为请求参数
RequestParam requestParam = parameters[i].getAnnotation(RequestParam.class);
if (requestParam != null) {
Map<String, Object> map = new HashMap<>();
String key = parameters[i].getName();
if (!StringUtils.isEmpty(requestParam.value())) {
key = requestParam.value();
}
map.put(key, args[i]);
argList.add(map);
}
}
if (argList.size() == 0) {
return null;
} else if (argList.size() == 1) {
return argList.get(0);
} else {
return argList;
}
}
}
进行接口测试
@testTarget(testName="测试",testContent="测试2")//给自定义注解赋值
@RequestMapping(value = "/list", method = RequestMethod.POST)
@ResponseBody
public CommonResult<CommonPage<PmsBrand>> listBrand(@RequestParam(value = "pageNum")
@ApiParam("页码") Integer pageNum,
@RequestParam(value = "pageSize")
@ApiParam("每页数量") Integer pageSize) {
List<PmsBrand> brandList = brandService.listBrand(pageNum, pageSize);
return CommonResult.success(CommonPage.restPage(brandList));
}
项目源码地址
https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-aop