spring-aop日志配置

application.xml配置
<aop:aspectj-autoproxy />
	<bean id="logAspect" class="com.ciming.action.log.LogAspect" />


LogAspect日志

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

import com.ciming.bo.LogVo;
import com.ciming.service.ILogService;

/**
 * 
 * @Description: 拦截器处理日志信息
 * @author liyong
 * @date 2014年11月26日 上午11:09:19
 * @version V1.0
 *
 */

@Aspect
public class LogAspect {

	@Resource
	private ILogService logService;

	/**
	 * 添加业务逻辑方法切入点
	 */
	@Pointcut("execution(* com.ciming.service.*.insert*(..))")
	public void insertServiceCall() {
	}

	/**
	 * 修改业务逻辑方法切入点
	 */
	@Pointcut("execution(* com.ciming.service.*.update*(..))")
	public void updateServiceCall() {
	}

	/**
	 * 删除影片业务逻辑方法切入点
	 */
	@Pointcut("execution(* com.ciming.service.*.delete*(..))")
	public void deleteServiceCall() {
	}

	/**
	 * 管理员添加操作日志(后置通知)
	 * 
	 * @param joinPoint
	 * @param rtv
	 * @throws Throwable
	 */
	@AfterReturning(value = "insertServiceCall()", argNames = "rtv", returning = "rtv")
	public void insertServiceCallCalls(JoinPoint joinPoint, Object rtv)
			throws Throwable {
		// 判断参数
		if (joinPoint.getArgs() == null) {
			return;
		}
		// 获取方法名
		// String methodName = joinPoint.getSignature().getName();
		// 获取日志信息
		LogVo log = getOperatorLog(joinPoint.getArgs());
		if (log == null) {
			return;
		}
		// 执行结果
		int ret = (Integer) rtv;
		if (ret >= 1) {
			log.setOperResult("添加成功");
		} else {
			log.setOperResult("添加失败");
		}
		// 准备添加的数据
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId", log.getUserId());
		map.put("menuName", log.getMenuName());
		map.put("operation", log.getOperation());
		map.put("content", log.getContent());
		map.put("operResult", log.getOperResult());
		map.put("logIp", log.getLogIp());
		map.put("logMac", log.getLogMac());
		map.put("comName", log.getComName());
		map.put("result", null);
		// 添加日志
		logService.insertLog(map);
	}

	/**
	 * 管理员修改操作日志(后置通知)
	 * 
	 * @param joinPoint
	 * @param rtv
	 * @throws Throwable
	 */
	@AfterReturning(value = "updateServiceCall()", argNames = "rtv", returning = "rtv")
	public void updateServiceCallCalls(JoinPoint joinPoint, Object rtv)
			throws Throwable {
		// 判断参数
		if (joinPoint.getArgs() == null) {
			return;
		}
		// 获取方法名
		// String methodName = joinPoint.getSignature().getName();
		// 获取日志信息
		LogVo log = getOperatorLog(joinPoint.getArgs());
		if (log == null) {
			return;
		}
		// 执行结果
		int ret = (Integer) rtv;
		if (ret >= 1) {
			log.setOperResult("成功");
		} else {
			log.setOperResult("失败");
		}
		// 准备添加的数据
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId", log.getUserId());
		map.put("menuName", log.getMenuName());
		map.put("operation", log.getOperation());
		map.put("content", log.getContent());
		map.put("operResult", log.getOperResult());
		map.put("logIp", log.getLogIp());
		map.put("logMac", log.getLogMac());
		map.put("comName", log.getComName());
		map.put("result", null);
		// 添加日志
		logService.insertLog(map);
	}

	/**
	 * 管理员删除影片操作(环绕通知),使用环绕通知的目的是 在影片被删除前可以先查询出影片信息用于日志记录
	 * 
	 * @param joinPoint
	 * @param rtv
	 * @throws Throwable
	 */
	@AfterReturning(value = "deleteServiceCall()", argNames = "rtv", returning = "rtv")
	public void deleteServiceCall(JoinPoint joinPoint, Object rtv)
			throws Throwable {

		// 判断参数
		if (joinPoint.getArgs() == null) {
			return;
		}
		// 获取方法名
		// String methodName = joinPoint.getSignature().getName();
		// 获取日志信息
		LogVo log = getOperatorLog(joinPoint.getArgs());
		if (log == null) {
			return;
		}
		// 执行结果
		int ret = (Integer) rtv;
		if (ret >= 1) {
			log.setOperResult("删除成功");
		} else {
			log.setOperResult("删除失败");
		}
		// 准备添加的数据
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId", log.getUserId());
		map.put("menuName", log.getMenuName());
		map.put("operation", log.getOperation());
		map.put("content", log.getContent());
		map.put("operResult", log.getOperResult());
		map.put("logIp", log.getLogIp());
		map.put("logMac", log.getLogMac());
		map.put("comName", log.getComName());
		map.put("result", null);
		// 添加日志
		logService.insertLog(map);
	}

	/**
	 * 
	 * @Description: 获取参数的日志信息
	 * @author liyong
	 * @date 2014年11月26日 上午11:02:38
	 * @version V1.0
	 *
	 */
	protected LogVo getOperatorLog(Object[] args) {
		LogVo log = null;
		// 类名
		String className = null;
		if (args == null) {
			return null;
		}
		for (Object info : args) {
			// 获取对象类型
			className = info.getClass().getName();
			className = className.substring(className.lastIndexOf(".") + 1);
			// 当方法中包含有日志的Bean表示当前要做拦截,否则不用拦截
			if (className.equals("LogVo")) {
				log = (LogVo) info;

			}
		}
		return log;
	}

	/**
	 * 
	 * @Description: 拼操作内容
	 * @author liyong
	 * @param args方法参数
	 * @param mName方法名称
	 * @date 2014年11月26日 上午11:03:15
	 * @version V1.0
	 *
	 */
	public String adminOptionContent(Object[] args, String mName)
			throws Exception {
		if (args == null) {
			return null;
		}
		StringBuffer rs = new StringBuffer();
		rs.append(mName);
		String className = null;
		int index = 1;
		// 遍历参数对象
		for (Object info : args) {
			// 获取对象类型
			className = info.getClass().getName();
			className = className.substring(className.lastIndexOf(".") + 1);
			rs.append("[参数" + index + ",类型:" + className + ",值:");
			// 获取对象的所有方法
			Method[] methods = info.getClass().getDeclaredMethods();
			// 遍历方法,判断get方法
			for (Method method : methods) {
				String methodName = method.getName();
				// 判断是不是get方法
				if (methodName.indexOf("get") == -1) {// 不是get方法
					continue;// 不处理
				}
				Object rsValue = null;
				try {
					// 调用get方法,获取返回值
					rsValue = method.invoke(info);
					if (rsValue == null) {// 没有返回值
						continue;
					}
				} catch (Exception e) {
					continue;
				}
				// 将值加入内容中
				rs.append("(" + methodName + " : " + rsValue + ")");
			}
			rs.append("]");
			index++;
		}
		return rs.toString();
	}

}




猜你喜欢

转载自you366.iteye.com/blog/2163186