Spring Aop 日志拦截器

1.spring.xml 配置文件

<bean id="logAdvice" class="com.util.LogAdvice"></bean>

<!-- 使用CGLib库动态代理技术织入 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>                                   //使用自动代理方式,service实现类需要无参构造方法
<aop:config>
<aop:pointcut id="servicePointCut" expression="execution(* com.web.*.service..*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointCut" />
<!-- 日志记录拦截器 -->
<aop:aspect ref="logAdvice">
<aop:pointcut id="logPointCut" expression="execution(* com.web.system.action..*(..))"/>      //拦截action操作的所有方法
<aop:before pointcut-ref="logPointCut" method="logBefore"/>
<aop:after-throwing pointcut-ref="logPointCut" method="logAfterThrow"/>
</aop:aspect>

</aop:config>


2.拦截器类


public class LogAdvice
{
    private static Log log = LogFactory.getLog(LogAdvice.class);

    public void logBefore(final JoinPoint joinPoint)
    {
        logMsg(joinPoint,"info");
    }


    /**
     * 日志入库操作
     * <功能详细描述>
     * @param joinPoint
     * @param operMsg
     * @see [类、类#方法、类#成员]
     */
    private void logMsg(final JoinPoint joinPoint,String operMsg)
    {
        String params  = "";
        String className = joinPoint.getTarget().getClass().getName();          //类型
        String methodName = joinPoint.getSignature().getName();                   //方法名
       
       BaseAction target = (BaseAction)joinPoint.getTarget();
       HttpServletRequest request =  target.getRequest();

       Enumeration enu =  request.getParameterNames();             //获取参数名
       //获取参数
       while(enu.hasMoreElements()){
           String paramName = (String)enu.nextElement();                  //参数名
           params +=paramName+":"+request.getParameter(paramName)+",";
           
       }
       String request_ip = request.getLocalAddr();                   //获取ip

    }


    /**
     * 捕获异常信息>
     * <功能详细描述>
     * @param joinPoint
     * @see [类、类#方法、类#成员]
     */
    public void logAfterThrow(final JoinPoint joinPoint) {
        logMsg(joinPoint,"error");
    }

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值