首页 > 代码库 > AOP 实现请求参数打印

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 这样进入方法之前参数就会被打印出来了

AOP 实现请求参数打印