一、打印請求的日誌,以及異常日誌(入庫、保存)java
//爲項目中的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(); }
二、事物管理rest