今天带来一个简单的Spring中的环绕AOP切面的具体使用以及实现
直接废话不多说上代码!也相当于给自己做个笔记。
#Spring中的切面依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
//Java部分
//开启切面注解
@Aspect
//lombok日志注解
@Slf4j
//注入容器,统一管理
@Component
public class WebException extends BaseApiService {
//切入点 所有的方法 >> execution 为切面表达式 感兴趣的小伙伴可以百度
@Pointcut("execution(* yq..*(..))")
private void webPointcut() {
}
//环绕增强
@Around("webPointcut()")
public Object around(ProceedingJoinPoint proceedingJoinPoint) {
Object proceed = null;
String className = proceedingJoinPoint.getSignature().getDeclaringTypeName();
String methodName = proceedingJoinPoint.getSignature().getName();
System.out.println();
System.out.println("============== " + className + " 类中的方法:" + methodName + " 开始执行 ==============");
MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
//参数名称
String[] parameterNames = methodSignature.getParameterNames();
//参数值
Object[] args = proceedingJoinPoint.getArgs();
for (int i = 0; i < args.length; i++) {
System.out.println("第"+i+"个参数:"+" key:"+parameterNames[i]+" value:"+args[i]);
}
try {
proceed = proceedingJoinPoint.proceed();
System.out.println("执行结果为:" + proceed);
} catch (Throwable e) {
e.printStackTrace();
System.out.println("执行方法发生了错误:"+e.getMessage());
System.out.println("============== " + className + " 类中的方法:" + methodName + " 执行结束 ==============");
return setResultError("执行方法发生了错误:"+e.getMessage());
}
System.out.println("============== " + className + " 类中的方法:" + methodName + " 执行结束 ==============");
return proceed;
}
}
**