zoukankan      html  css  js  c++  java
  • AOP 实现请求参数打印

    1.编写打印方法

    import java.util.Enumeration;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.aspectj.lang.JoinPoint;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    
    public class LogAspect {
    
        private static Logger LOGGER = LoggerFactory.getLogger(LogAspect.class);
    
        public void doBefore(JoinPoint joinPoint) {
    
            // 接收到请求,记录请求内容
            LOGGER.info("WebLogAspect.doBefore()");
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
                    .getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
    
            Enumeration<String> enu = request.getParameterNames();
            LOGGER.debug("CLASS_METHOD : {}.{}", joinPoint.getSignature().getDeclaringTypeName() , joinPoint.getSignature().getName());
            while (enu.hasMoreElements()) {
                String paraName = (String) enu.nextElement();
                LOGGER.debug("{}:{}",paraName,request.getParameter(paraName));
            }
        }
    
    }

    2.配置 AOP

        <aop:config>  
            <aop:aspect id="logAspect" ref="logAspectBean">  
                <aop:pointcut id="allMethod"   
                    expression="execution(* com.qi.jr.fp.test..*.*(..))"/>   
                <aop:before method="doBefore" pointcut-ref="allMethod" />  
            </aop:aspect>  
        </aop:config>  
        
        <bean id="logAspectBean" class="com.qi.aop.LogAspect"></bean>

    ok 这样进入方法之前参数就会被打印出来了

  • 相关阅读:
    Linux从入门到进阶全集——【第十四集:Shell编程】
    cmake 编译 c++ dll 的一个例子
    %1 不是有效的Win32应用程序
    C++ 生成 dll 和调用 dll 的方法实例(转)
    Clion cmake 一个简单的 C++ 程序
    一月4
    一月4日
    1月4日
    一月4日
    一月4日
  • 原文地址:https://www.cnblogs.com/yun965861480/p/6846229.html
Copyright © 2011-2022 走看看