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. win10/win7 笔记本 开启虚拟无线 批处理

    Microsoft Virtual WiFi Miniport Adapter 一.看网络网卡 有多出的这一项“Microsoft Virtual WiFi Miniport Adapter”,那么说 ...

  2. Spring boot @PropertySource, @ImportResource, @Bean

    @PropertySource:加载指定的配置文件 /** * 将配置文件中配置的每一个属性的值,映射到这个组件中 * @ConfigurationProperties:告诉SpringBoot将本类 ...

  3. Spring Colud 学习

    转自:    http://blog.csdn.net/forezp/article/details/70148833#t0

  4. mysql打开log-bin报错

    在mysqld下设置log-bin后重启出现错误, [ERROR] You have enabled the binary log, but you haven't provided the mand ...

  5. Django模型的filed type总结

    1)AutoField:如果没有指明主键,就会产生一个自增的主键 2)BigIntegerField:64位的整形数值,从 -2^63 (-9223372036854775808) 到 2^63-1( ...

  6. python中if __name__ == '__main__': 解析

    当你打开一个.py文件时,经常会在代码的最下面看到if __name__ ==  '__main__':,现在就来介 绍一下它的作用. 模块是对象,并且所有的模块都有一个内置属性 __name__.一 ...

  7. sql server判断是否为null

    sql server 替换null:isnull(arg,value) 如:select isnull(price,0.0) from orders ,如果price为null的话,用0.0替换 与n ...

  8. Gviz

    1) Introduction 为了理解基因组数据,通常旨在在基因组浏览器中绘制这样的数据,以及各种基因组注释特征,例如基因或转录物模型,CpG岛,重复区域等.这些功能可以从ENSEMBL或UCSC等 ...

  9. bedtools简介及应用

    1)背景处理基因组数据中,比较基因组不同区域,例如寻找overlap等,是一种基本的且常见的问题.虽然UCSC 中‘Table Browser’或者Galaxy可以用来处理,但是当这些工具面对大的数据 ...

  10. Python globals() 函数

    Python globals() 函数  Python 内置函数 描述 globals() 函数会以字典类型返回当前位置的全部全局变量. 语法 globals() 函数语法: globals() 参数 ...