1. 创建接口注解日志类

    1. package com.fh.service.logAop;
    2.  
    3. /**
    4. * Created by caozengling on 2018/7/21.
    5. */
    6.  
    7. import java.lang.annotation.*;
    8.  
    9. /**
    10. * 日志切面注解
    11. */
    12.  
    13. @Target({ ElementType.METHOD, ElementType.TYPE })
    14. @Retention(RetentionPolicy.RUNTIME)
    15. @Documented
    16. public @interface MethodLog {
    17.  
    18. String remark() default "";
    19. String operType() default "0";
    20. // String desc() default "";
    21. }
  2. 切面实现
    1. package com.fh.service.logAop;
    2.  
    3. /**
    4. * Created by caozengling on 2018/7/21.
    5. */
    6.  
    7. import com.fh.dao.DaoSupport;
    8. import org.aspectj.lang.ProceedingJoinPoint;
    9. import org.aspectj.lang.annotation.Around;
    10. import org.aspectj.lang.annotation.Aspect;
    11. import org.aspectj.lang.annotation.Pointcut;
    12. import org.springframework.stereotype.Component;
    13. import org.springframework.web.context.request.RequestContextHolder;
    14. import org.springframework.web.context.request.ServletRequestAttributes;
    15.  
    16. import javax.annotation.Resource;
    17. import javax.servlet.http.HttpServletRequest;
    18. import java.awt.geom.Area;
    19. import java.lang.reflect.Method;
    20. import java.text.SimpleDateFormat;
    21. import java.util.Calendar;
    22.  
    23. /**
    24. * 日志切面实现
    25. */
    26.  
    27. @Component
    28. @Aspect
    29. public class LogService {
    30.  
    31. @Resource(name = "daoSupport")
    32. private DaoSupport dao;
    33.  
    34. public LogService() {
    35. System.out.println("Aop");
    36. }
    37.  
    38. /**
    39. * 切点
    40. */
    41. @Pointcut("@annotation(com.fh.service.logAop.MethodLog)")
    42. public void methodCachePointcut() { }
    43.  
    44. /**
    45. * 切面
    46. *
    47. * @param point
    48. * @return
    49. * @throws Throwable
    50. */
    51. @Around("methodCachePointcut()")
    52. public Object around(ProceedingJoinPoint point) throws Throwable {
    53.  
    54. HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
    55. .getRequestAttributes()).getRequest();
    56. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
    57. Calendar ca = Calendar.getInstance();
    58. String operDate = df.format(ca.getTime());
    59. String loginName;
    60. String name;
    61. String methodRemark = getMthodRemark(point);
    62. String methodName = point.getSignature().getName();
    63. String packages = point.getThis().getClass().getName();
    64. if (packages.indexOf("$$EnhancerByCGLIB$$") > -1) { // 如果是CGLIB动态生成的类
    65. try {
    66. packages = packages.substring(0, packages.indexOf("$$"));
    67. } catch (Exception ex) {
    68. ex.printStackTrace();
    69. }
    70. }
    71. String operatingcontent = "";
    72. Object[] method_param = null;
    73.  
    74. Object object;
    75. try {
    76. method_param = point.getArgs(); //获取方法参数
    77. // String param=(String) point.proceed(point.getArgs());
    78. object = point.proceed();
    79. } catch (Exception e) {
    80. // 异常处理记录日志..log.error(e);
    81. throw e;
    82. }
    83.  
    84. Area area = (Area) method_param[0];
    85.  
    86. // System.out.println("日志实体:"+sysLog.getLoginName()+sysLog.getMethodRemark()+sysLog.getOperatingcontent());
    87. return object;
    88.  
    89. }
    90.  
    91. /**
    92. * 方法异常时调用
    93. *
    94. * @param ex
    95. */
    96. public void afterThrowing(Exception ex) {
    97. System.out.println("afterThrowing");
    98. System.out.println(ex);
    99. }
    100.  
    101. /**
    102. * 获取方法中的中文备注
    103. *
    104. * @param joinPoint
    105. * @return
    106. * @throws Exception
    107. */
    108. public static String getMthodRemark(ProceedingJoinPoint joinPoint) throws Exception {
    109.  
    110. String targetName = joinPoint.getTarget().getClass().getName();
    111. String methodName = joinPoint.getSignature().getName();
    112. Object[] arguments = joinPoint.getArgs();
    113.  
    114. Class targetClass = Class.forName(targetName);
    115. Method[] method = targetClass.getMethods();
    116. String methode = "";
    117. for (Method m : method) {
    118. if (m.getName().equals(methodName)) {
    119. Class[] tmpCs = m.getParameterTypes();
    120. if (tmpCs.length == arguments.length) {
    121. MethodLog methodCache = m.getAnnotation(MethodLog.class);
    122. if (methodCache != null) {
    123. methode = methodCache.remark();
    124. }
    125. break;
    126. }
    127. }
    128. }
    129. return methode;
    130. }
    131. }
  3. 方法切入,这里只是举个例子,具体逻辑切入点请自行添加。
  4. 依赖:
    1. springboot
    1. <!--spring切面aop依赖-->
    2. <dependency>
    3. <groupId>org.springframework.boot</groupId>
    4. <artifactId>spring-boot-starter-aop</artifactId>
    5. </dependency>
    6.  
    7. 在application.properties文件里加这样一条配置
    8. spring.aop.auto=true
    9.  
    10. spring mvp :
      在ApplicationContext-mvc.xml 中添加以下配置:
    1. <aop:aspectj-autoproxy proxy-target-class="true"/>
    1.  

Spring AOP 切面实现操作日志的更多相关文章

  1. springboot—spring aop 实现系统操作日志记录存储到数据库

    原文:https://www.jianshu.com/p/d0bbdf1974bd 采用方案: 使用spring 的 aop 技术切到自定义注解上,针对不同注解标志进行参数解析,记录日志 缺点是要针对 ...

  2. Spring Boot 2.X(八):Spring AOP 实现简单的日志切面

    AOP 1.什么是 AOP ? AOP 的全称为 Aspect Oriented Programming,译为面向切面编程,是通过预编译方式和运行期动态代理实现核心业务逻辑之外的横切行为的统一维护的一 ...

  3. Spring AOP 切面编程记录日志和接口执行时间

    最近客户现在提出系统访问非常慢,需要优化提升访问速度,在排查了nginx.tomcat内存和服务器负载之后,判断是数据库查询速度慢,进一步排查发现是因为部分视图和表查询特别慢导致了整个系统的响应时间特 ...

  4. 利用Spring AOP切面对用户访问进行监控

    开发系统时往往需要考虑记录用户访问系统查询了那些数据.进行了什么操作,尤其是访问重要的数据和执行重要的操作的时候将数记录下来尤显的有意义.有了这些用户行为数据,事后可以以用户为条件对用户在系统的访问和 ...

  5. Spring AOP切面的时候参数的传递

    Spring AOP切面的时候参数的传递 Xml: <?xml version="1.0" encoding="UTF-8"?> <beans ...

  6. spring AOP(切面) 表达式介绍

    在 spring AOP(切面) 例子基础上对表达式进行介绍 1.添加接口删除方法 2.接口实现类 UserDaoServer 添加实现接口删除方法 3.测试类调用delUser方法 4. 输出结果截 ...

  7. 使用Spring AOP切面解决数据库读写分离

    http://blog.jobbole.com/103496/ 为了减轻数据库的压力,一般会使用数据库主从(master/slave)的方式,但是这种方式会给应用程序带来一定的麻烦,比如说,应用程序如 ...

  8. 【spring】aop切面通知,日志处理

    1.spring的切面编程 概念原理可以看这里:http://blog.csdn.net/moreevan/article/details/11977115 2.所需要的jar包 maven引入jar ...

  9. Spring AOP 实现写事件日志功能

    什么是AOP?AOP使用场景?AOP相关概念?Spring AOP组件?如何使用Spring AOP?等等这些问题请参考博文:Spring AOP 实现原理 下面重点介绍如何写事件日志功能,把日志保存 ...

随机推荐

  1. node 把前台传来的base64码转成图片存放

    最近做个人网站头像修改用到了,在做头像修改,先做了一个图片切割,只需要上传你选中部分, 如图 这种需求 应该还是会遇到的, http://pan.baidu.com/s/1boVkn1t 这是裁剪图片 ...

  2. myeclipse 代码提示

    from http://fuyiyuan2011.iteye.com/blog/1258264 在软件开发过程中,有了代码提示能使开发能够更加快捷与便利.但在Eclipse ,MyEclipse等ja ...

  3. VULKAN学习笔记-inter教学四篇

    --交换链相关函数:实例层vkCreateWin32SurfaceKHRvkDestroySurfaceKHRvkGetPhysicalDeviceSurfaceSurportKHRvkGetPhys ...

  4. conflicting types for ‘方法名’ 的错误

    将main()的实现写在drawShapes(),drawCircle(),drawRectangle()...之前. 结果编译的时候出现了  conflicting types for " ...

  5. No matter how hard it is or no matter how bad it gets, I am going to make it!

    No matter how hard it is or no matter how bad it gets, I am going to make it! He always had a yearni ...

  6. 【336】Tutorial of Endnote

    Now I start to use Endnote to manage my literatures and I need to learn how to use it. Below is some ...

  7. 如何打开Windows Server 2008 R2的域安全策略

    今天安装了Windows Server 2008 R2系统,并且建了域环境,在添加新用户的时候,发现用简单的密码时域安全策略提示密码复杂度不够,于是我就想在域安全策略里面把密码复杂度降低一点,但是很快 ...

  8. Centos7 vnc

    这是一个关于怎样在你的 CentOS 7 上安装配置 VNC 服务的教程.当然这个教程也适合 RHEL 7 .在这个教程里,我们将学习什么是 VNC 以及怎样在 CentOS 7 上安装配置 VNC ...

  9. dubbo通信协议

    对dubbo的协议的学习,可以知道目前主流RPC通信大概是什么情况,本文参考dubbo官方文档 http://dubbo.io/User+Guide-zh.htm dubbo共支持如下几种通信协议: ...

  10. FastDFSClient工具类 文件上传下载

    package cn.itcast.fastdfs.cliennt; import org.csource.common.NameValuePair; import org.csource.fastd ...