spring-boot-aop–面向切面编程
- spring-boot-starter-aop 支持AOP
- aspectjtools 使用aspectj实现
一、快速开始
1、添加依赖
<!-- aop依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.8.0</version>
</dependency>
2、编写AOP切面处理类
@Aspect
@Component
public class MyAspect {
//=============切点====================//
//方式一:使用注解的方式 方式二:使用EL表示的是包形式
@Pointcut("@annotation(com.springboot.annotation.Log)")
//@Pointcut("execution(* com.wwj.springboot.service.impl.*.*(..))")
public void pointcut() { }
//======================通知=================//
//环绕通知
@Around("pointcut()")
public void around(ProceedingJoinPoint point) {
// do AOP methods
}
//前置通知
@Before("pointcut()")
public void doBefore(JoinPoint joinPoint) {
LOGGER.info("doAfter():{}", joinPoint.toString());
}
//后置通知
@After("pointcut()")
public void doAfter(JoinPoint joinPoint) {
LOGGER.info("doAfter():{}", joinPoint.toString());
}
//后置返回通知
@AfterReturning("pointcut()")
public void doAfterReturning(JoinPoint joinPoint) {
LOGGER.info("耗时 :{}", ((System.currentTimeMillis() - threadLocal.get())) + "ms");
}
}
以上可以看出,实现AOP的核心在于切面类
3、总结的一些好用的工具类
操作上可以通过工具类获取某些参数结果
public class AspectUtils {
public static final String UNKONW = "unkonw";
/**
* 获取当前请求对象
* @return
*/
public static ServletRequestAttributes getAttributes(){
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
.getRequestAttributes();
return attributes;
}
/**
* 获取请求示例
* @return
*/
public static HttpServletRequest getRequest(){
ServletRequestAttributes attributes = getAttributes();
HttpServletRequest request = attributes.getRequest();
return request;
}
/**
* 获取
* @param joinPoint
* @return
*/
public static Method getMethod(ProceedingJoinPoint joinPoint){
//获取连接点的方法签名对象
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
//获取方法实例
Method method = methodSignature.getMethod();
Class<? extends MethodSignature> aClass = methodSignature.getClass();
return method;
}
/**
* 通过注解获取注解内部参数内容
*/
public static OperationLog getAnnotationData(ProceedingJoinPoint joinPoint){
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
OperationLog annotation = method.getAnnotation(OperationLog.class);
return annotation;
}
/**
* 获取请求参数
*/
public static void getParameterJson(ProceedingJoinPoint joinPoint){
}
/**
* 获取IP地址
* @return
*/
public static String getIPAdress(HttpServletRequest request){
String ip = request.getHeader("x-forword-for");
if(ip == null || ip.length() ==0 || UNKONW.equalsIgnoreCase(ip)){
ip = request.getHeader("Proxy-Clent-IP");
}
if(ip == null || ip.length() ==0 || UNKONW.equalsIgnoreCase(ip)){
ip = request.getHeader("WL-Clent-IP");
}
if(ip == null || ip.length() ==0 || UNKONW.equalsIgnoreCase(ip)){
ip = request.getRemoteAddr();
}
return ip;
}
/**
* 获取请求参数
* @param proceedingJoinPoint
* @return
*/
public static String getRequestParams(ProceedingJoinPoint proceedingJoinPoint) {
List<Object> argsList = Arrays.stream(proceedingJoinPoint.getArgs())
.filter(arg -> (!(arg instanceof HttpServletRequest) && !(arg instanceof HttpServletResponse)))
.collect(Collectors.toList());
if (CollUtil.isEmpty(argsList)) {
return "";
}
return JSON.toJSONString(argsList.get(0));
}
}