zoukankan      html  css  js  c++  java
  • Spring MVC 中使用AOP 进行统一日志管理--XML配置实现

    1.介绍

    上一篇博客写了使用AOP进行统一日志管理的注解版实现,今天写一下使用XML配置实现版本,与上篇不同的是上次我们记录的Controller层日志,这次我们记录的是Service层的日志。使用的工程还是原来的那个,具体的Spring mvc 工程搭建暂不介绍。上篇记录controller层日志的时候是将切面类组件叫给spring MVC 进行管理,因为 controller 也是交给spring MVC进行管理的,但是记录service 层日志的时候应该就应该再spring 容器中进行了,因为services 层是在spring 容器中进行管理的。

    2.实现

    (1)首先新建一个记录日志的类,这是一个普通的类。其中有几个方法,分别对应执行前, 执行后,返回前,报错,环绕等几个需要打印日志的场景

    package com.lzl.sss.aop;
    
    import java.io.UnsupportedEncodingException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Enumeration;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    
    //定义切面类
    public class ServiceLogAspect {
        private Logger logger = LoggerFactory.getLogger(ServiceLogAspect.class);
        
        //定义通知,方法执行前
        public void doBefore(JoinPoint poin) throws UnsupportedEncodingException{
            logger.info("【Service】方法执行前,当前时间:"+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            // 记录下请求内容
            logger.info("【Service】请求URL : " + request.getRequestURL().toString());
            logger.info("【Service】请求方法 : " + request.getMethod());
            logger.info("【Service】IP地址 : " + request.getRemoteAddr());
            Enumeration<String> enu = request.getParameterNames();
            while (enu.hasMoreElements()) {
                String name = (String) enu.nextElement();
                logger.info("【Service】参数:{},值:{}", name,new String(request.getParameter(name).getBytes("ISO-8859-1"),"utf-8"));
            }
    
    
        }
        
        //定义通知,方法执行后
        public void after(JoinPoint poin){
            logger.info("【Service】方法执行后,当前时间:"+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        }
        
        //定义通知,方法返回前
        public void AfterReturning(JoinPoint poin){
            logger.info("【Service】方法返回前,当前时间:"+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        }
        
        //定义通知,抛出异常
        public void AfterThrowing(Throwable error){
            logger.info("【Service】方法报错,当前时间:"+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        }
        
        //定义通知环绕型
        public Object Around (ProceedingJoinPoint pjp) throws Throwable{
            logger.info("【Service】环绕前:"+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            Object obj= pjp.proceed();
            logger.info("【Service】环绕后:"+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            return obj;
        }
    }

    (2)配置。在spring 配置文件中将日志组件注入spring ,并配置切点表达式。注意在配置文件的头文件新增AOP相关的部分

    <beans xmlns="http://www.springframework.org/schema/beans"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:context="http://www.springframework.org/schema/context"  
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="    
               http://www.springframework.org/schema/beans    
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
               http://www.springframework.org/schema/aop    
               http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
               http://www.springframework.org/schema/context    
               http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
    <bean id = "ServiceLogAspect" class = "com.lzl.sss.aop.ServiceLogAspect"></bean>
       <aop:config>
            <aop:aspect id="serviceLogAspect" ref="ServiceLogAspect">
                <aop:pointcut expression="execution(* com.lzl.sss.service.*.*(..))" id="businessService"/>
                <aop:before method="doBefore" pointcut-ref="businessService"/>
                <aop:after method="after" pointcut-ref="businessService"/>
                <aop:around method="Around" pointcut-ref="businessService"/>
                <aop:after-returning method="AfterReturning" pointcut-ref="businessService"/>
                <aop:after-throwing method="AfterThrowing" pointcut-ref="businessService" throwing="error"/>
            </aop:aspect> 
        </aop:config>

    3.实现效果

  • 相关阅读:
    datatable转mode方法
    UVA10652 Board Wrapping (二维凸包+多边形面积)
    洛谷P3175 [HAOI2015]按位或 (min-max容斥 + 高维前缀和)
    icpc2020沈阳 M (fwt+高维前缀和)
    icpc2019 香港 H. Hold the Line (离线+线段树+单调队列)
    icpc2019 香港 C. Constructing Ranches (点分治)
    点分治模板
    slope trick 待填
    codeforces 713 C. Sonya and Problem Wihtout a Legend (dp(n^2)/slope trick (nlogn))
    icpc2020澳门 I. Nim Cheater (轻重链剖分优化dp空间复杂度)
  • 原文地址:https://www.cnblogs.com/li-zhi-long/p/9394966.html
Copyright © 2011-2022 走看看