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

相关文章
相关标签/搜索