2019獨角獸企業重金招聘Python工程師標準>>>
###1.日志切面
package com.readygo.zbhealth.common;import java.util.Arrays;
import java.util.List;import org.aspectj.lang.ProceedingJoinPoint;public class LoggingAspect {public Object Around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{Object result = null;String methodName = proceedingJoinPoint.getSignature().getName();try {System.out.println("請求路徑:/"+methodName+",請求參數:");List<Object> list = Arrays.asList(proceedingJoinPoint.getArgs());switch (methodName) {case "getSpecialist":System.out.println("searcheContent:"+list.get(0)+"\n"+"pageNum:"+list.get(1)+"\n"+"lastTime:"+list.get(2)+"\n"+"typeId:"+list.get(3)+"\n");break;case "getQuestionDetail":System.out.println("questionId:"+list.get(0)+"\n"+"userId:"+list.get(1)+"\n");break;case "getSpecialistDetail":System.out.println("specialistId:"+list.get(0)+"\n"+"userId:"+list.get(1)+"\n");break; case "getQuestionList":System.out.println("userId:"+list.get(0)+"\n"+"flag:"+list.get(1)+"\n");break;default:break;}result = proceedingJoinPoint.proceed();} catch (Exception e) {System.out.println("請檢查參數個數");e.printStackTrace();}return result;}}
###2.xml配置文件
<bean id="loggingAspect" class="com.readygo.zbhealth.common.LoggingAspect"></bean><aop:config><aop:pointcut expression="execution(* com.readygo.zbhealth.controller.ThirdPartController.*(..))" id="aopPointcut"/><aop:aspect ref="loggingAspect"><aop:around method="Around" pointcut-ref="aopPointcut"/></aop:aspect></aop:config>
###3.controller文件
@RestController
public class ThirdPartController {@Autowiredprivate ThirdPartService thirdPartService;/*** 獲取專家列表* @param searcheContent* @param pageNum* @param lastTime* @param typeId* @return 專家列表*/@RequestMapping(value = "/getSpecialist", method=RequestMethod.POST)public ResultObject getSpecialist(@RequestParam("searcheContent") String searcheContent,@RequestParam("pageNum") String pageNum,@RequestParam("lastTime") String lastTime,@RequestParam("typeId") String typeId){ResultObject resultObject = new ResultObject();try {resultObject = thirdPartService.getSpecialist(searcheContent, pageNum, lastTime, typeId);} catch (Exception e) {resultObject = Utils.resultCatchInfo(new Object());e.printStackTrace();}return resultObject;}
}
###4.驗證結果
請求路徑:/getSpecialist,請求參數:
searcheContent:搜索內容
pageNum:1
lastTime:20160914112200
typeId:1000000000
###5.功能
該日志切面配置在controller層中的每個方法上,功能是打印出 請求路徑 與 請求參數。