spring --aop(日誌記錄)在工程中實際使用

###1.日誌切面java

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配置文件express

<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文件app

@RestController
public class ThirdPartController {
	
	@Autowired
	private 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層中的每一個方法上,功能是打印出 請求路徑 與 請求參數。code

相關文章
相關標籤/搜索