直接上代码
1、maven导入依赖
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
2、自定义一个注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogOperation {
String value()default "";
int type()default 0;
}
3、自定义一个类写切入点代码
@Component
@Aspect
public class LogTrackAspect {
@Pointcut(value = "@annotation(logOperation)")
public void access(LogOperation logOperation){
}
//前置增强
@Before("access(logOperation)")
public void doBefore(JoinPoint joinPoint,LogOperation logOperation) throws Throwable {
System.out.println("-aop 日志记录启动-" + new Date());
System.out.println("获取到的值"+logOperation.value());
}
//环绕增强,是在before前就会触发
@Around("@annotation(logOperation)")
public Object around(ProceedingJoinPoint pjp, LogOperation logOperation) throws Throwable {
System.out.println("-aop 日志环绕阶段-" + new Date());
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// GET 请求其实可以从request里获取出参数
// Map<String,String[]> map=request.getParameterMap();
// System.out.println("获取参数:"+map.get("username")[0])
String url = request.getRequestURL().toString();
String ip = IpUtil.getIpAddr(request);
String logTrackValue = logTrack.value();
Object[] pipArrary = pjp.getArgs();
if (pipArrary.length>1){ //多参,不是Map/JsonObject方式
List<Object> argList = new ArrayList<>();
for (Object arg : pjp.getArgs()) {
// request/response无法使用toJSON
if (arg instanceof HttpServletRequest) {
argList.add("request");
} else if (arg instanceof HttpServletResponse) {
argList.add("response");
} else {
argList.add(JSON.toJSON(arg));
}
}
Signature signature = pjp.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
// 参数名数组
String[] parameterNames = ((MethodSignature) signature).getParameterNames();
System.out.println("参数名数组:"+new ArrayList(Arrays.asList(parameterNames)));
System.out.println("参数是:"+argList.toString());
System.out.println("logTrackValue:"+logTrackValue);
System.out.println("url:"+url);
System.out.println("ip:"+ip);
return pjp.proceed();
}
Object param = pipArrary[0];
System.out.println("logTrackValue:"+logTrackValue);
System.out.println("url:"+url);
System.out.println("ip:"+ip);
System.out.println("param:"+param.toString());
return pjp.proceed();
}
@AfterThrowing(value = "serviceStatististics(operation)", throwing = "e")
public void doAfterThrowing(LogOperation operation, Throwable e) {
//设置异常信息
// long happenTime = System.currentTimeMillis();
// logException.setHappentime(new Date(happenTime));
// logException.setExceptionjson(e.getMessage());
// logException.setExceptionmessage(e.getMessage());
// logExceptionMapper.insert(logException);
}
}
4、将注解加到你想要的切入的方法上
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/test")
@ResponseBody
@LogOperation(value = "测试切面") //当你访问user/test时,就会执行LogTrackAspect 类中的代码
public String test(){
return "test切面";
}
}
过滤的顺序顺序如下:
@Around-joinPoint.proceed(args)前
@Before
@Around-joinPoint.proceed(args)后
@After
@AfterReturning/@AfterThrowing