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

Spring MVC 中使用AOP 进行统一日志管理--XML配置实现的更多相关文章
- Spring MVC 中使用AOP 进行统一日志管理--注解实现
1.AOP简介 AOP称为面向切面编程 AOP的基本概念 (1)Aspect(切面):通常是一个类,里面可以定义切入点和通知 (2)JointPoint(连接点):程序执行过程中明确的点,一般是方法的 ...
- Spring Boot中使用AOP记录请求日志
这周看别人写的springboot后端代码中有使用AOP记录请求日志,以前没接触过,因此学习下. 一.AOP简介 AOP为Aspect Oriented Programming的缩写,意为:面向切面编 ...
- Spring MVC 中使用AOP 进行事务管理--XML配置实现
1.今天写一篇使用AOP进行事务管理的示例,关于事务首先需要了解以下几点 (1)事务的特性 原子性(Atomicity):事务是一个原子操作,由一系列动作组成.事务的原子性确保动作要么全部完成,要么完 ...
- Spring MVC 中使用AOP 进行事务管理--注解实现
注解实现实现事务管理很简单,和配置式差不多,配置文件的头文件也要加相应的支持.配置数据源,并开启事务管理支持即可. <bean id="transactionManager" ...
- Spring Boot中使用AOP统一处理Web请求日志
AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是Spring框架中的一个重要内容,它通 ...
- 46. Spring Boot中使用AOP统一处理Web请求日志
在之前一系列的文章中都是提供了全部的代码,在之后的文章中就提供核心的代码进行讲解.有什么问题大家可以给我留言或者加我QQ,进行咨询. AOP为Aspect Oriented Programming的缩 ...
- spring MVC中的异常统一处理
1.spring MVC中定义了一个标准的异常处理类SimpleMappingExceptionResolver 该类实现了接口HandlerExceptionResolver 2.看下SimpleM ...
- 【Java分享客栈】超简洁SpringBoot使用AOP统一日志管理-纯干货干到便秘
前言 请问今天您便秘了吗?程序员坐久了真的会便秘哦,如果偶然点进了这篇小干货,就麻烦您喝杯水然后去趟厕所一边用左手托起对准嘘嘘,一边用右手滑动手机看完本篇吧. 实现 本篇AOP统一日志管理写法来源于国 ...
- Spring MVC中各个filter的用法
转载:http://blog.csdn.net/qyp1314/article/details/42023725 Spring MVC中各个filter的用法 2014-12-19 09:08 105 ...
随机推荐
- Redis——SpringBoot项目使用Lettuce和Jedis接入Redis集群
Jedis连接Redis: 非线程安全 如果是多线程环境下共用一个Jedis连接池,会产生线程安全问题,可以通过创建多个Jedis实例来解决,但是创建许多socket会影响性能,因此好一点的方法是使用 ...
- border-box与content-box的区别
㈠box-sizing 属性 ⑴box-sizing 属性允许您以特定的方式定义匹配某个区域的特定元素. ⑵语法:box-sizing: content-box|border-box|inherit; ...
- Luogu2000 拯救世界
题目链接:戳我 生成函数的入门题吧. 我们可以把条件限制转化为生成函数,然后用第i项的系数来表示一共使用n块石头的方案个数. (你问我为什么?你可以自己演算一下,或者去看大佬的博客-->这里面讲 ...
- python3.6+selnium3+IE11问题及解决方法
环境:python3.6+selnium3+IE11+win7 一.输入框输入字符很慢,大概5秒输入一个字符 解决方法:把IEDriverServer.exe替换成32位的 二.用例异常后不继续执行剩 ...
- Redis的一点笔记
Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 优势: 性能极高 – Redis能读的速度是110000次/s,写的速度是81000次/s . 丰富 ...
- Django日志的配置
做开发离不开日志,以下是我在工作中写Django项目常用的logging配置. BASE_LOG_DIR = os.path.join(BASE_DIR, "log") LOG ...
- C++入门经典-例2.15-逗号表达式的应用
1:代码如下: // 2.15.cpp : 定义控制台应用程序的入口点. #include "stdafx.h" #include<iostream> using na ...
- JavaWeb之上传与下载
文件上传概述: 1,文件上传对页面的要求: 必须使用表单,而不能是超链接 表单的method必须是post 表单的enctype必须是multipart/form-data 在表单中添加file表单字 ...
- 按模版导出Excel
实现效果: excel模版: ExcelHandle.java package com.common.utils; import java.io.File; import java.io.FileIn ...
- Firefox63以后 禁止自动更新方式
参考:https://bbs.kafan.cn/thread-2135160-1-1.html 63版以后在prefs.js文件末尾加代码来禁止自动更新的方式失效 新方式: 使用DisableAppU ...