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


     

转载于:https://www.cnblogs.com/caozengling/p/9347308.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值