使用AOP是为了降低我们的开发量,且直观,我只需要知道这个AOP是做什么的就够了。
拿一个较为常用例子讲解:
一.Weight(权值)
这个AOP实现的是为接口提供自动增加权值,只要有对该接口的调用就会实现一次权值增加。
我们要了解它的实现只需要了解3个步骤:
①:interface - 接口
package com.seatrend.sign.aop;
import *;
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Weight {
}
这里需要了解的是它的生命周期:Retention
②:aspect - 切面
package com.seatrend.sign.aspect;
import *;
@Aspect
@Component
public class Weight {
//增加权值的sql
@Autowired
private Mapper Mapper;
//接口的路径
@Pointcut("execution(@com.seatrend.sign.aop.Weight * *(..))")
private void method() {
}
//表示在接口实现后再调用这里的method
@After("method()")
public void increaseWeight() throws Exception {
//获取我们接口参数,对该xh对应的数据权值进行运算
HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
String xh = request.getParameter("xh");
Mapper.addOne(xh);
}
}
这里我们还需要去了解的是:After、Before、Around
③:request - 使用方式
@ApiImpliciParam(name = "xh", required = "true", dataType = "string")
@Weight
public void xxx(@RequestParam(value = "xh") String xh) {
...
}
直接放在接口上即可