读书人

Spring AOP记要系统日志

发布时间: 2013-03-17 13:48:31 作者: rapoo

Spring AOP记录系统日志

配置文件:

Xml代码
  1. <!-- 操作日志切面声明 -->
  2. <bean id="logAspect" class="com.tq365.service.sys.log.SystemLogAspect"/>
  3. <aop:config>
  4. <aop:aspect ref="logAspect">
  5. </aop:aspect>
  6. </aop:config>

实现代码:

Java代码
  1. /**
  2. * 系统操作日志切面
  3. *
  4. * @author archie2010
  5. * since 2011-3-17 下午02:44:03
  6. */
  7. @Aspect
  8. public class SystemLogAspect {
  9. // int与long之Class会自动转为其封装类型之Class
  10. private static final String integerClazz = "class java.lang.Integer";
  11. private static final String longClazz = "class java.lang.Long";
  12. @Resource
  13. private SystemLogService systemLogService;
  14. private Logger logger = Logger.getLogger(this.getClass().getName());
  15. @Pointcut("execution(* com.tq365.service..*.*(..))")
  16. public void myAspect() {
  17. };
  18. @AfterThrowing(pointcut = "myAspect()", throwing = "e")
  19. public void doAfterThrowing(JoinPoint jp, Throwable e) {
  20. System.out.println("出现异常:" + e.getMessage());
  21. System.out.println(e.getClass().getName());
  22. System.out.println("异常所在类:" + jp.getTarget().getClass().getName());
  23. System.out.println("" + jp.getSignature().getName()
  24. + "方法 throw exception");
  25. // logger.error("错误! error级别的!!!"+e.getMessage());
  26. logger.error("Oops===" + jp.getTarget().getClass().getName() + "中的"
  27. + jp.getSignature().getName() + "方法抛出" + e.getClass().getName()
  28. + "异常");
  29. System.out.println("参数:");
  30. ;
  31. if (jp.getArgs() != null && jp.getArgs().length > 0) {
  32. for (int i = 0; i < jp.getArgs().length; i++) {
  33. System.out.println(jp.getArgs()[i].toString());
  34. logger.error("参数:--" + jp.getArgs()[i].toString());
  35. }
  36. }
  37. }
  38. @SuppressWarnings("unchecked")
  39. @After("@annotation(com.tq365.sys.annotation.SystemLogAnnotation)")
  40. public void doAfter(JoinPoint jp) {
  41. System.out.println("----------后置通知");
  42. System.out.println("方法所在类:" + jp.getTarget().getClass().getName());
  43. System.out.println("" + jp.getSignature().getName() + "方法");
  44. String methodName = jp.getSignature().getName();
  45. // 操作日志对象-----------------
  46. SystemLog sysLog = new SystemLog();
  47. // 操作参数-----------------
  48. String descArgs = "参数";
  49. if (jp.getArgs() != null && jp.getArgs().length > 0) {
  50. for (int i = 0; i < jp.getArgs().length; i++) {
  51. if(jp.getArgs()[i]!=null){
  52. //System.out.println(jp.getArgs()[i].toString());
  53. descArgs += jp.getArgs()[i].toString()+",";
  54. }else{
  55. descArgs +="null"+",";
  56. }
  57. }
  58. System.out.println("------参数" + descArgs);
  59. }
  60. sysLog.setOperateArgs(descArgs);
  61. String des = null;//方法描述
  62. if (!(methodName.startsWith("set") || methodName.startsWith("get"))) {
  63. Class targetClass = jp.getTarget().getClass();
  64. //方法不定向参数Clazz...
  65. Class[] claszs = new Class[jp.getArgs().length];
  66. for (int i = 0; i < jp.getArgs().length; i++) {
  67. //System.out.println(jp.getArgs()[i]);
  68. if(jp.getArgs()[i]!=null){
  69. System.out.println(jp.getArgs()[i].getClass());
  70. if (jp.getArgs()[i].getClass().toString().equals(integerClazz)) {
  71. claszs[i] = int.class;
  72. } else if (jp.getArgs()[i].getClass().toString().equals(
  73. longClazz)) {
  74. claszs[i] = long.class;
  75. }else{
  76. claszs[i] =jp.getArgs()[i].getClass();
  77. }
  78. }else if(jp.getArgs()[i]==null){
  79. claszs[i] = String.class;
  80. }
  81. }
  82. Method method=null;
  83. try {
  84. method = targetClass.getMethod(methodName, claszs);
  85. } catch (SecurityException e) {
  86. } catch (NoSuchMethodException e) {
  87. }
  88. //若方法为空(描述无法获得则des=null)
  89. if(method!=null){
  90. System.out.println(method.getAnnotation(SystemLogAnnotation.class)
  91. .description());
  92. des = method.getAnnotation(SystemLogAnnotation.class).description();
  93. }
  94. }
  95. // 获得Session
  96. HttpSession session = ServletActionContext.getRequest().getSession();
  97. // 取到当前的操作用户
  98. User appUser = (User) session.getAttribute("USER");
  99. if (appUser != null) {
  100. System.out.println("用户已经存在Session中");
  101. // 操作日志对象
  102. sysLog.setUid(appUser.getUserId());
  103. sysLog.setUsername(appUser.getFullName());
  104. }
  105. HttpServletRequest request = ServletActionContext.getRequest();
  106. String ip = request.getRemoteAddr();
  107. sysLog.setOperateTime(DateUtil.getCurrentTime());
  108. sysLog.setOperateDes(methodName +"->"+ des);
  109. sysLog.setIp(ip);
  110. systemLogService.save(sysLog);
  111. System.out.println("----------保存操作日志");
  112. }
  113. }

读书人网 >编程

热点推荐