Spring MVC 中使用AOP 进行统一日志管理--XML配置实现
2024-09-04 06:05:54
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.实现效果
最新文章
- 对于前端JS、Html、CSS的大小、位置是否影响网站的相应时间
- (转)github设置添加SSH
- asm/aam links
- 计算NSString含有多少个相同字符串
- devexpress中gridcontrol头部添加垂直线(右边框)
- 如何制作iso文件
- Dynamics CRM 2016 使用Plug-in Trace Log调试插件
- Ceph之数据分布:CRUSH算法与一致性Hash
- 返璞归真 asp.net mvc (1) - 添加、查询、更新和删除的 Demo
- OC之消息调用过程
- RedisPool操作Redis,工具类实例
- MSSql Server 批量插入数据优化
- 012 Spark在IDEA中打jar包,并在集群上运行(包括local模式,standalone模式,yarn模式的集群运行)
- Guava学习笔记(一):Maven
- MySQL数据库----流程控制
- 【xshell】xshell设置快捷键 设置Ctrl+C Ctrl+V快捷键为复制粘贴
- GetProcAddress 使用注意事项
- ExtJs Ext.data.Model 学习笔记
- CC2530zigbee技术-简介协议栈
- 学习笔记之glog的使用