Spring AOP解决系统日志备忘

配置文件:

复制代码
  
  
<!-- 操作日志切面声明 --> < bean id ="logAspect" class ="com.tq365.service.sys.log.SystemLogAspect" /> < aop:config > < aop:aspect ref ="logAspect" > </ aop:aspect > </ aop:config >
复制代码

Java代码:

复制代码
  
  
/** * 系统操作日志切面 * * @author archie2010 * since 2011-3-17 下午02:44:03 */ @Aspect public class SystemLogAspect { // int与long之Class会自动转为其封装类型之Class private static final String integerClazz = " class java.lang.Integer " ; private static final String longClazz = " class java.lang.Long " ; @Resource private SystemLogService systemLogService; private Logger logger = Logger.getLogger( this .getClass().getName()); @Pointcut( " execution(* com.tq365.service..*.*(..)) " ) public void myAspect() { }; @AfterThrowing(pointcut = " myAspect() " , throwing = " e " ) public void doAfterThrowing(JoinPoint jp, Throwable e) { System.out.println( " 出现异常: " + e.getMessage()); System.out.println(e.getClass().getName()); System.out.println( " 异常所在类: " + jp.getTarget().getClass().getName()); System.out.println( "" + jp.getSignature().getName() + " 方法 throw exception " ); // logger.error("错误! error级别的!!!"+e.getMessage()); logger.error( " Oops=== " + jp.getTarget().getClass().getName() + " 中的 " + jp.getSignature().getName() + " 方法抛出 " + e.getClass().getName() + " 异常 " ); System.out.println( " 参数: " ); ; if (jp.getArgs() != null && jp.getArgs().length > 0 ) { for ( int i = 0 ; i < jp.getArgs().length; i ++ ) { System.out.println(jp.getArgs()[i].toString()); logger.error( " 参数:-- " + jp.getArgs()[i].toString()); } } } @SuppressWarnings( " unchecked " ) @After( " @annotation(com.tq365.sys.annotation.SystemLogAnnotation) " ) public void doAfter(JoinPoint jp) { System.out.println( " ----------后置通知 " ); System.out.println( " 方法所在类: " + jp.getTarget().getClass().getName()); System.out.println( "" + jp.getSignature().getName() + " 方法 " ); String methodName = jp.getSignature().getName(); // 操作日志对象----------------- SystemLog sysLog = new SystemLog(); // 操作参数----------------- String descArgs = " 参数 " ; if (jp.getArgs() != null && jp.getArgs().length > 0 ) { for ( int i = 0 ; i < jp.getArgs().length; i ++ ) { if (jp.getArgs()[i] != null ){ // System.out.println(jp.getArgs()[i].toString()); descArgs += jp.getArgs()[i].toString() + " , " ; } else { descArgs += " null " + " , " ; } } System.out.println( " ------参数 " + descArgs); } sysLog.setOperateArgs(descArgs); String des = null ; // 方法描述 if ( ! (methodName.startsWith( " set " ) || methodName.startsWith( " get " ))) { Class targetClass = jp.getTarget().getClass(); // 方法不定向参数Clazz... Class[] claszs = new Class[jp.getArgs().length]; for ( int i = 0 ; i < jp.getArgs().length; i ++ ) { // System.out.println(jp.getArgs()[i]); if (jp.getArgs()[i] != null ){ System.out.println(jp.getArgs()[i].getClass()); if (jp.getArgs()[i].getClass().toString().equals(integerClazz)) { claszs[i] = int . class ; } else if (jp.getArgs()[i].getClass().toString().equals( longClazz)) { claszs[i] = long . class ; } else { claszs[i] = jp.getArgs()[i].getClass(); } } else if (jp.getArgs()[i] == null ){ claszs[i] = String. class ; } } Method method = null ; try { method = targetClass.getMethod(methodName, claszs); } catch (SecurityException e) { } catch (NoSuchMethodException e) { } // 若方法为空(描述无法获得则des=null) if (method != null ){ System.out.println(method.getAnnotation(SystemLogAnnotation. class ) .description()); des = method.getAnnotation(SystemLogAnnotation. class ).description(); } } // 获得Session HttpSession session = ServletActionContext.getRequest().getSession(); // 取到当前的操作用户 User appUser = (User) session.getAttribute( " USER " ); if (appUser != null ) { System.out.println( " 用户已经存在Session中 " ); // 操作日志对象 sysLog.setUid(appUser.getUserId()); sysLog.setUsername(appUser.getFullName()); } HttpServletRequest request = ServletActionContext.getRequest(); String ip = request.getRemoteAddr(); sysLog.setOperateTime(DateUtil.getCurrentTime()); sysLog.setOperateDes(methodName + " -> " + des); sysLog.setIp(ip); systemLogService.save(sysLog); System.out.println( " ----------保存操作日志 " ); } }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值