Spring AOP 切面实现操作日志

  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"/>


     

posted @ 2018-07-21 17:21  caozengling  阅读(7027)  评论(0编辑  收藏  举报