1. 创建接口注解日志类

    package com.fh.service.logAop;
    
    /**
    * Created by caozengling on 2018/7/21.
    */ import java.lang.annotation.*; /**
    * 日志切面注解
    */ @Target({ ElementType.METHOD, ElementType.TYPE })
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface MethodLog { String remark() default "";
    String operType() default "0";
    // String desc() default "";
    }
  2. 切面实现
    package com.fh.service.logAop;
    
    /**
    * Created by caozengling on 2018/7/21.
    */ import com.fh.dao.DaoSupport;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.stereotype.Component;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes; import javax.annotation.Resource;
    import javax.servlet.http.HttpServletRequest;
    import java.awt.geom.Area;
    import java.lang.reflect.Method;
    import java.text.SimpleDateFormat;
    import java.util.Calendar; /**
    * 日志切面实现
    */ @Component
    @Aspect
    public class LogService { @Resource(name = "daoSupport")
    private DaoSupport dao; public LogService() {
    System.out.println("Aop");
    } /**
    * 切点
    */
    @Pointcut("@annotation(com.fh.service.logAop.MethodLog)")
    public void methodCachePointcut() { } /**
    * 切面
    *
    * @param point
    * @return
    * @throws Throwable
    */
    @Around("methodCachePointcut()")
    public Object around(ProceedingJoinPoint point) throws Throwable { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
    .getRequestAttributes()).getRequest();
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
    Calendar ca = Calendar.getInstance();
    String operDate = df.format(ca.getTime());
    String loginName;
    String name;
    String methodRemark = getMthodRemark(point);
    String methodName = point.getSignature().getName();
    String packages = point.getThis().getClass().getName();
    if (packages.indexOf("$$EnhancerByCGLIB$$") > -1) { // 如果是CGLIB动态生成的类
    try {
    packages = packages.substring(0, packages.indexOf("$$"));
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    }
    String operatingcontent = "";
    Object[] method_param = null; Object object;
    try {
    method_param = point.getArgs(); //获取方法参数
    // String param=(String) point.proceed(point.getArgs());
    object = point.proceed();
    } catch (Exception e) {
    // 异常处理记录日志..log.error(e);
    throw e;
    } Area area = (Area) method_param[0]; // System.out.println("日志实体:"+sysLog.getLoginName()+sysLog.getMethodRemark()+sysLog.getOperatingcontent());
    return object; } /**
    * 方法异常时调用
    *
    * @param ex
    */
    public void afterThrowing(Exception ex) {
    System.out.println("afterThrowing");
    System.out.println(ex);
    } /**
    * 获取方法中的中文备注
    *
    * @param joinPoint
    * @return
    * @throws Exception
    */
    public static String getMthodRemark(ProceedingJoinPoint joinPoint) throws Exception { String targetName = joinPoint.getTarget().getClass().getName();
    String methodName = joinPoint.getSignature().getName();
    Object[] arguments = joinPoint.getArgs(); Class targetClass = Class.forName(targetName);
    Method[] method = targetClass.getMethods();
    String methode = "";
    for (Method m : method) {
    if (m.getName().equals(methodName)) {
    Class[] tmpCs = m.getParameterTypes();
    if (tmpCs.length == arguments.length) {
    MethodLog methodCache = m.getAnnotation(MethodLog.class);
    if (methodCache != null) {
    methode = methodCache.remark();
    }
    break;
    }
    }
    }
    return methode;
    }
    }
  3. 方法切入,这里只是举个例子,具体逻辑切入点请自行添加。
  4. 依赖:
  5. springboot:
    <!--spring切面aop依赖-->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
    </dependency> 在application.properties文件里加这样一条配置
    spring.aop.auto=true spring mvp :
    在ApplicationContext-mvc.xml 中添加以下配置:
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    
    

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. 列表(ul ol dl)

    Title 1 2 3 1 2 3 a 1 2 b 1 2 <!DOCTYPE html> <html lang="en"> <head> &l ...

  2. hashlib 加密模块使用说明

    import hashlib  #hashilib 模块 m = hashlib.md5() m.update('hello 天王盖地虎'.encode(encoding = 'utf-8)) m.h ...

  3. 16 MySQL--正确使用索引

    count 统计 count(*)和count(字段名) 基本结果是一样的 但是一种情况例外,就是当某字段名下边的数据有null值的时候,不计入这个count中,*则全部列入count中 一 .索引未 ...

  4. 低版本eclipse离线集成svn步骤,亲测有效!!!

    1.下载svn离线版的插件: 百度云盘链接:http://pan.baidu.com/s/1eSnMoHO 密码:6oef 2.解压出来的额目录如下: 3.将features和plugins里面的ja ...

  5. VBox 安装 macOS 10.12

    安装步骤⑴ 下载及解压 macOS 10.12 Sierra Final by TechReviews.rar ⑵ 下载及双击安装 VirtualBox-5.1.6-110634-Win.exe ,默 ...

  6. 主流JS库一览

    主流JS库一览 标签: prototypedojomootoolsprototypejsjqueryjavascript 2009-10-14 22:52 19936人阅读 评论(2) 收藏 举报   ...

  7. win64+anaconda+xgboost(转)

    Windows下安装python版的XGBoost(Anaconda)          XGBoost是近年来很受追捧的机器学习算法,由华盛顿大学的陈天奇提出,在国内外的很多大赛中取得很不错的名次, ...

  8. PCA原理(转)

    PCA(Principal Component Analysis)是一种常用的数据分析方法.PCA通过线性变换将原始数据变换为一组各维度线性无关的表示,可用于提取数据的主要特征分量,常用于高维数据的降 ...

  9. linux一些基本常识(四)

    tail -f时时监控 一开启内存最小位u原则,尽量优化代码 grep -v "" /etc/passwd 这样行不行 怎么清除last nice调整进程运行级别 pkill是匹配 ...

  10. configparser模块 logging模块

    configparser模块 固定格式的配置文件 有一个对应的模块去帮你做这个文件的字符串处理 config = configparser.Configparser() config.read(“ex ...