zoukankan      html  css  js  c++  java
  • LogAspect

    import java.lang.reflect.Array;
    import java.lang.reflect.Method;
    import java.text.SimpleDateFormat;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;
    
    import org.apache.commons.lang.StringUtils;
    import org.apache.log4j.Logger;
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.aspectj.lang.reflect.MethodSignature;
    import org.springframework.stereotype.Component;
    import org.springframework.web.context.request.RequestAttributes;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    
    import com.yd.common.runtime.CIPRuntime;
    import com.yd.common.runtime.CIPRuntimeConstants;
    import com.yd.common.session.CIPHttpSession;
    import com.yd.common.session.CIPSessionManager;
    import com.yd.common.session.CIPUser;
    
     
    /**
     * 第三步:声明一个切面
     * 
     * 记录日记的切面
     * 
     * @author ZRD
     *
     */
    @Component
    @Aspect
    public class LogAspect {
     
         private final Logger logger = Logger.getLogger(getClass());
        /**
         * 切入点:表示在哪个类的哪个方法进行切入。配置有切入点表达式
         */
    
        /*@Pointcut("execution(* com.yd..*.*(..))")
        public void pointcutExpression() {
             
        }*/
         
        /**
         * 1 前置通知
         * @param joinPoint
         */
        // @Before("execution(* com.yd.wmsc.busi.service.local.LWMSC_busi_inboundServiceImpl.*(..))")
         //@Before("execution(* com.yd.wmsc.busi.service..*(..))")
         //@Before("execution(* com.yd.wmsc.*.service..*(..))")
         //@Before("execution(* com.yd..service..*(..))")
         //@Before("execution(* *..service..*(..))")//service包下的所有方法
         //@Before("execution(* *..*Service*..*(..))")//带有Service的类的所有方法
        public void beforeMethod(JoinPoint joinPoint) {
            System.out.println("前置通知执行了");
        }
        /**
         * 2 后置通知
         */
        
        public void afterMethod(JoinPoint joinPoint) {
            System.out.println("后置通知执行了,有异常也会执行");
        }
        /**
         * 3 返回通知
         * 
         * 在方法法正常结束受执行的代码
         * 返回通知是可以访问到方法的返回值的!
         * 
         * @param joinPoint
         * @param returnValue
         * 
         */
       /* @AfterReturning(value = "pointcutExpression()", returning = "returnValue")
        public void afterRunningMethod(JoinPoint joinPoint, Object returnValue) {
            System.out.println("返回通知执行,执行结果:" + returnValue);
        }*/
         
        /**
         * 4 异常通知
         * 
         * 在目标方法出现异常时会执行的代码.
         * 可以访问到异常对象; 且可以指定在出现特定异常时在执行通知代码
         * 
         * @param joinPoint
         * @param e
         */
        /*@AfterThrowing(value = "pointcutExpression()", throwing = "e")
        public void afterThrowingMethod(JoinPoint joinPoint, Exception e){
            System.out.println("异常通知, 出现异常 :" + e);
        }*/
         
        /**
         * 环绕通知需要携带 ProceedingJoinPoint 类型的参数. 
         * 环绕通知类似于动态代理的全过程: ProceedingJoinPoint 类型的参数可以决定是否执行目标方法.
         * 且环绕通知必须有返回值, 返回值即为目标方法的返回值
         */
         
        /*@Around("pointcutExpression()")
        public Object aroundMethod(ProceedingJoinPoint pjd){
             
            Object result = null;
            String methodName = pjd.getSignature().getName();
             
            try {
                //前置通知
                System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs()));
                //执行目标方法
                result = pjd.proceed();
                //返回通知
                System.out.println("The method " + methodName + " ends with " + result);
            } catch (Throwable e) {
                //异常通知
                System.out.println("The method " + methodName + " occurs exception:" + e);
                throw new RuntimeException(e);
            }
            //后置通知
            System.out.println("The method " + methodName + " ends");
             
            return result;
        }*/
         
        @Around("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
        public Object aroundMethod(ProceedingJoinPoint pjd){
            
            String requestPath = null; // 请求地址
            String userId = null;
            String userName = null; // 用户名
            //Map<?, ?> inputParamMap = null; // 传入参数
            //Map<String, Object> outputParamMap = null; // 存放输出结果
              
            Object result = null;
            Object args[] = pjd.getArgs();  
            MethodSignature signature = (MethodSignature) pjd.getSignature();  
            Method method = signature.getMethod();    
            long startTimeMillis = System.currentTimeMillis();  
            try {
                /**
                * 1.获取request信息
                * 2.根据request获取session
                * 3.从session中取出登录用户信息            */
               RequestAttributes ra = RequestContextHolder.getRequestAttributes();
              
               ServletRequestAttributes sra = (ServletRequestAttributes)ra;
               HttpServletRequest request = sra.getRequest();
               if (CIPRuntime.getOperateSubject()!=null ) {
                   userId =CIPRuntime.getOperateSubject().getSubject_id();
                   userName =CIPRuntime.getOperateSubject().getSubject_name();
               }
               
               // 从session中获取用户信息
              // userName = (String) session.getAttribute("userName");
               //String sessionId = runtimeInfo.get(CIPRuntimeConstants.SYSTEM_SESSION_ID);
               //CIPHttpSession systemSession = CIPSessionManager.getSession(request, response);
               //CIPUser systemUser = systemSession.getAttribute(CIPRuntimeConstants.LOGIN_USER, CIPUser.class);
               // 获取输入参数
               //inputParamMap = request.getParameterMap();
               // 获取请求地址
               requestPath = request.getRequestURI();
               
               // 执行完方法的返回值:调用proceed()方法,就会触发切入点方法执行
               //outputParamMap = new HashMap<String, Object>();
              
                //执行目标方法
                result = pjd.proceed();
                //outputParamMap.put("result", result);
            } catch (Throwable e) {
                //异常通知
                logger.error(e);
                throw new RuntimeException(e);
            }
            long endTimeMillis = System.currentTimeMillis(); 
            String optTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startTimeMillis);
            //后置通知
            
            logger.debug("
     userId:"+userId +" userName:"+userName +"  url:"+requestPath+"; op_time:" + optTime + " pro_time:" + (endTimeMillis - startTimeMillis) + "ms ;"
            +"
    "+ method.getDeclaringClass().getName()+"."+ method.getName()+":"+ Arrays.toString(args)+"
    "+"result:"+result);
            
            return result;
        }
    }
  • 相关阅读:
    概率密度函数 通俗理解
    宋浩《概率论与数理统计》笔记---2.2.2、连续型随机变量及其概率密度函数
    宋浩《概率论与数理统计》笔记---2.2.1、离散型随机变量及其概率分布
    宋浩《概率论与数理统计》笔记---2.1、随机变量的概念
    宋浩《概率论与数理统计》笔记---1.5.2、伯努利模型
    宋浩《概率论与数理统计》笔记---1.5.1、事件的独立性
    贝叶斯公式-汽车实例
    贝叶斯公式的理解
    宋浩《概率论与数理统计》笔记---1.4.2、贝叶斯公式
    nodemon通过配置文件监视
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/6520409.html
Copyright © 2011-2022 走看看