1、打印请求的日志,以及异常日志(入库、保存)
//为项目中的rest方法定义一个切入点
@Pointcut("execution(* com.curtain.*..*.rest.*.*(..))")
public void rest(){};
@Before("rest()")
public void before(){
System.out.println("开始执行");
}
@After("rest()")
public void after(){
System.out.println("结束执行");
}
//对某方法进行环绕通知
@Around("rest()")
public void around(ProceedingJoinPoint pjp){
MethodSignature signature = (MethodSignature) pjp.getSignature();
Method method = signature.getMethod();
if (method.getDeclaringClass().isInterface()) {//判断是否是接口方法
try {
method = pjp
.getTarget()
.getClass()
.getDeclaredMethod(signature.getName(),
method.getParameterTypes());
} catch (final SecurityException exception) {
} catch (final NoSuchMethodException exception) {
}
}
Object[] args = pjp.getArgs();//获取对应方法的参数
try {
Object result = pjp.proceed();// 执行该方法
String clz = method.getDeclaringClass().getName();//类名
String methodInfo = method.toGenericString();//方法名
String desc = "";
String content = "";
String type = "";
ActionLog action = AnnotationUtils.findAnnotation(method,
ActionLog.class);//方法名上对应的注解
if (action != null) {
desc = action.description();
content = action.content();
type = action.type();
}
List<Object> cells = new ArrayList<Object> ();
cells.add(type);
cells.add(content);
cells.add(desc);
cells.add(clz);
cells.add(methodInfo);
cells.add(StringUtils.asString(Arrays.asList(args)));
cells.add(isSuccess(result));
cells.add(StringUtils.asString(getResult(result)));//对方法结果进行处理
System.out.println(cells);
} catch (Throwable e) {
e.printStackTrace();
}
2、事物管理