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.实现效果

转载于:https://www.cnblogs.com/li-zhi-long/p/9394966.html

Spring MVC 中使用AOP 进行统一日志管理--XML配置实现相关推荐

  1. Spring MVC 中使用AOP 进行事务管理--XML配置实现

    1.今天写一篇使用AOP进行事务管理的示例,关于事务首先需要了解以下几点 (1)事务的特性 原子性(Atomicity):事务是一个原子操作,由一系列动作组成.事务的原子性确保动作要么全部完成,要么完 ...

  2. 基于注解的Spring MVC(所需jar包,web.xml配置,Spring文件配置,@Controller,@RequestMapping,@RequestParam,model填参,EL取值)

    1.添加jar 2.web.xml配置: <?xml version="1.0" encoding="UTF-8"?> <web-app ve ...

  3. 基于XML配置的Spring MVC(所需jar包,web.xml配置,处理器配置,视图解析器配置)

    1.添加jar 2.web.xml配置 <?xml version="1.0" encoding="UTF-8"?> <web-app ver ...

  4. asp.net core mvc接口,请求响应统一日志管理

    如何为api所有的请求和响应做一个统一的日志记录 1.创建日志类 public class RequestResponseLog {public string Url { get; set; }pub ...

  5. Spring MVC中的视图解析ViewResolver

    http://blog.csdn.net/prince2270/article/details/5891085 在Spring MVC中,当Controller将请求处理结果放入到ModelAndVi ...

  6. Spring Boot中使用AOP统一处理Web请求日志

    AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是Spring框架中的一个重要内容,它通 ...

  7. spring mvc中的拦截器

    本文说下spring MVC中的拦截器 文章目录 拦截器介绍 拦截器注入适配器 自定义拦截器 controller测试 测试结果 本文小结 拦截器介绍 拦截器是在servlet执行之前执行的程序(这里 ...

  8. 十、springboot注解式AOP(@Aspect)统一日志管理

    springboot注解式AOP(@Aspect)统一日志管理 简介 AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功 ...

  9. Spring 2.5:Spring MVC中的新特性

    转载说明:infoQ就是牛人多,看人家去年就把Spring2.5注视驱动的MVC写出来了,还是这么详细,我真是自叹不如,今天偶尔看到这篇文章非常认真的拜读了2遍,简直是茅厕顿开啊....\(^o^)/ ...

最新文章

  1. 查看PID 进程是否存在的一个小技巧
  2. KV数据存储:持久化
  3. 用tableView实现的一种加载数据的布局
  4. ML之xgboost:基于xgboost(5f-CrVa)算法对HiggsBoson数据集(Kaggle竞赛)训练实现二分类预测(基于训练好的模型进行新数据预测)
  5. 吴钩:打开宋代的“隐藏玩法”
  6. 模型类型与加速潜力的关系
  7. Thinkpad SL-400 XP驱动下载与安装方法
  8. PostgreSQL与MySQL语法对比总结
  9. Linux应急响应入侵排查思路
  10. 【原创】常用元器件选型目录-cayden(待续)
  11. linux安装软件系列之yum安装
  12. 冥思苦想,木疙瘩也能崩出个豆:扯一下各大软件的用户体验
  13. windows开启ftp服务及FTP命令使用
  14. element plus 部分组件转英文问题
  15. 1056. Confusing Number
  16. ftp连接服务器失败:响应:220-FileZilla Server version 0.9.24 beta 响应:220-written by Tim Kosse (Tim.Kosse@gmx.d
  17. 概率论与数理统计学习笔记——第六讲——离散型随机变量(6.2贝努利概型和二项分布)
  18. FLUKE LinkIQ-100 CH/福禄克LIQ-100 CH可以出测试报告
  19. 学习,教育的1000+篇文章总结
  20. 码住 学网页制作需要的基本步骤都在这里

热门文章

  1. HTML→标签、div语义化、表单、input标签
  2. 第一章节 初识C#程序
  3. 微软推补丁服务器,微软再推稳定性补丁 为即将到来功能更新做准备
  4. python symbols函数,Python
  5. Dekker互斥算法解析
  6. 在有空字符串的有序字符串数组中查找(找给定字符串)
  7. RayMarching2:给球加上光照
  8. bzoj 3372: [Usaco2004 Feb]Moo University -- Financial Aid 财政补助(set+贪心)
  9. ZOJ 3983 2017CCPC秦皇岛 C:Crusaders Quest
  10. 2017CCPC哈尔滨赛区总结