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

转载于:https://www.cnblogs.com/yun965861480/p/6846229.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值