SSM项目使用AOP技术进行日志记录

2024-09-08 12:52

本文主要是介绍SSM项目使用AOP技术进行日志记录,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本步骤只记录完成切面所需的必要代码

本人开发中遇到的问题:

切面一直切不进去,最后发现需要在springMVC的核心配置文件中中开启注解驱动才可以,只在spring的核心配置文件中开启是不会在web项目中生效的。

之后按照下面的代码进行配置,然后前端在访问controller层中的路径时即可观察到日志已经被正常记录到数据库,代码中有部分注释,看不懂的可以参照注释。接下来进入正题

1、导入maven坐标
 <!--切面--><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.7</version></dependency><!-- AspectJ Runtime --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>1.8.7</version> </dependency>
2、核心文件配置,在springMVC.xml文件中开启AOP切面注解驱动
<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd
"><!--    开启注解驱动--><context:annotation-config/><!--    开启组件扫描--><context:component-scan base-package="com.xszx"></context:component-scan><!--    开启切面注解驱动--><aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
3、配置web.xml文件,指定spring容器的配置文件
<!DOCTYPE web-app PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app><display-name>Archetype Created Web Application</display-name><!--配置一个全局的参数:指定spring容器的配置文件 ServletContext--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:beans.xml</param-value></context-param><!--解决中文乱码的filter一定要放在最前面  --><filter><filter-name>CharacterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><!-- 配置encoding,告诉我们指定的编码格式 --><init-param><param-name>encoding</param-name><param-value>utf-8</param-value></init-param><init-param><!--是否强制设置request的编码为encoding,默认false,不建议更改--><param-name>forceRequestEncoding</param-name><param-value>false</param-value></init-param><init-param><!--是否强制设置response的编码为encoding,建议设置为true,下面有关于这个参数的解释--><param-name>forceResponseEncoding</param-name><param-value>false</param-value></init-param></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!--配置监听器:创建spring容器对象--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!--定义一个request监听器--><listener><listener-class>org.springframework.web.context.request.RequestContextListener</listener-class></listener><servlet><servlet-name>SpringMVC</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!--指定配置文件的路径--><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:SpringMVC.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>SpringMVC</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
</web-app>
4、切面类
@Component
@Aspect
public class LogAop {@Resourceprivate LoginUserCountService loginUserCountService;@Autowiredprivate HttpServletRequest request;@Autowiredprivate ISysLogService sysLogService;private Date visitTime; //开始时间private Class clazz; //访问的类private Method method;//访问的方法//前置通知  主要是获取开始时间,执行的类是哪一个,执行的是哪一个方法@Before("execution(public * com.xszx.controller.*.*(..))")public void doBefore(JoinPoint jp) {try {visitTime = new Date(); // 当前时间就是开始访问的时间clazz = jp.getTarget().getClass(); // 具体要访问的类String methodName = jp.getSignature().getName(); // 获取访问的方法的名称Object[] args = jp.getArgs(); // 获取访问的方法的参数// 获取具体执行的方法的Method对象if (args == null || args.length == 0) {method = clazz.getMethod(methodName); // 只能获取无参数的方法} else {Class[] classArgs = new Class[args.length];for (int i = 0; i < args.length; i++) {classArgs[i] = args[i].getClass();}method = clazz.getMethod(methodName, classArgs);}} catch (NoSuchMethodException e) {//            e.printStackTrace();} catch (Exception e) {//            e.printStackTrace();}}//后置通知@After("execution(* com.xszx.controller.*.*(..))")public void doAfter(JoinPoint jp) {try {long time = new Date().getTime() - visitTime.getTime(); // 获取访问的时长String url = "";// 获取urlif (clazz != null && method != null && clazz != LogAop.class) {RequestMapping methodAnnotation = method.getAnnotation(RequestMapping.class);if (methodAnnotation != null) {String[] methodValue = methodAnnotation.value();url = methodValue[0];// 获取访问的ipString ip = request.getRemoteAddr();// 登录时将用户信息存入session,现在从session中获取当前操作的用户HttpSession httpSession = request.getSession();User user = (User)httpSession.getAttribute("loginUser");String userEmail = "";if (user == null){userEmail = "无登录用户";}else {userEmail = user.getEmail();}// 将日志相关信息封装到SysLog对象SysLog sysLog = new SysLog();sysLog.setExecutionTime(time); // 执行时长sysLog.setIp(ip);sysLog.setMethod("[类名] " + clazz.getName() + "[方法名] " + method.getName());sysLog.setUrl(url);sysLog.setUsername(userEmail);sysLog.setVisitTime(visitTime);// 调用Service完成操作sysLogService.save(sysLog);}}} catch (Exception e) {//            e.printStackTrace();}}
}
5、beans层
public class SysLog {private Integer id;private Date visitTime;private String visitTimeStr;private String username;private String ip;private String url;private Long executionTime;private String method;//记得写getter,setter方法,构造方法,toString方法
}
6、dao层
public interface ISysLogMapper {void save(SysLog sysLog) throws Exception;List<SysLog> findAll() throws Exception;
}
7、service层
@Service
public class SysLogServiceImpl implements ISysLogService {private SqlSessionTemplate sqlSessionTemplate;@Autowiredpublic void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {this.sqlSessionTemplate = sqlSessionTemplate;}@Overridepublic List<SysLog> findAll() throws Exception {ISysLogMapper iSysLogMapper = sqlSessionTemplate.getMapper(ISysLogMapper.class);return iSysLogMapper.findAll();}@Overridepublic void save(SysLog sysLog) throws Exception {ISysLogMapper iSysLogMapper = sqlSessionTemplate.getMapper(ISysLogMapper.class);iSysLogMapper.save(sysLog);}
}
8、mapper层
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xszx.dao.ISysLogMapper"><insert id="save">insert into syslog(visitTime,username,ip,url,executionTime,method)values(#{visitTime},#{username},#{ip},#{url},#{executionTime},#{method})</insert><select id="findAll" resultType="com.xszx.beans.SysLog">select * from sysLog</select>
</mapper>
9、数据库表结构

执行完后进行测试即可,当前扫描的路径是controller下的所有方法,所以随便进入一个即可打印出对于的日志记录。  

这篇关于SSM项目使用AOP技术进行日志记录的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1148164

相关文章

LiteFlow轻量级工作流引擎使用示例详解

《LiteFlow轻量级工作流引擎使用示例详解》:本文主要介绍LiteFlow是一个灵活、简洁且轻量的工作流引擎,适合用于中小型项目和微服务架构中的流程编排,本文给大家介绍LiteFlow轻量级工... 目录1. LiteFlow 主要特点2. 工作流定义方式3. LiteFlow 流程示例4. LiteF

使用Python开发一个现代化屏幕取色器

《使用Python开发一个现代化屏幕取色器》在UI设计、网页开发等场景中,颜色拾取是高频需求,:本文主要介绍如何使用Python开发一个现代化屏幕取色器,有需要的小伙伴可以参考一下... 目录一、项目概述二、核心功能解析2.1 实时颜色追踪2.2 智能颜色显示三、效果展示四、实现步骤详解4.1 环境配置4.

使用jenv工具管理多个JDK版本的方法步骤

《使用jenv工具管理多个JDK版本的方法步骤》jenv是一个开源的Java环境管理工具,旨在帮助开发者在同一台机器上轻松管理和切换多个Java版本,:本文主要介绍使用jenv工具管理多个JD... 目录一、jenv到底是干啥的?二、jenv的核心功能(一)管理多个Java版本(二)支持插件扩展(三)环境隔

SQL中JOIN操作的条件使用总结与实践

《SQL中JOIN操作的条件使用总结与实践》在SQL查询中,JOIN操作是多表关联的核心工具,本文将从原理,场景和最佳实践三个方面总结JOIN条件的使用规则,希望可以帮助开发者精准控制查询逻辑... 目录一、ON与WHERE的本质区别二、场景化条件使用规则三、最佳实践建议1.优先使用ON条件2.WHERE用

Java中Map.Entry()含义及方法使用代码

《Java中Map.Entry()含义及方法使用代码》:本文主要介绍Java中Map.Entry()含义及方法使用的相关资料,Map.Entry是Java中Map的静态内部接口,用于表示键值对,其... 目录前言 Map.Entry作用核心方法常见使用场景1. 遍历 Map 的所有键值对2. 直接修改 Ma

MySQL 衍生表(Derived Tables)的使用

《MySQL衍生表(DerivedTables)的使用》本文主要介绍了MySQL衍生表(DerivedTables)的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学... 目录一、衍生表简介1.1 衍生表基本用法1.2 自定义列名1.3 衍生表的局限在SQL的查询语句select

Mybatis Plus Join使用方法示例详解

《MybatisPlusJoin使用方法示例详解》:本文主要介绍MybatisPlusJoin使用方法示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录1、pom文件2、yaml配置文件3、分页插件4、示例代码:5、测试代码6、和PageHelper结合6

MySQL分区表的具体使用

《MySQL分区表的具体使用》MySQL分区表通过规则将数据分至不同物理存储,提升管理与查询效率,本文主要介绍了MySQL分区表的具体使用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、分区的类型1. Range partition(范围分区)2. List partition(列表分区)3. H

统一返回JsonResult踩坑的记录

《统一返回JsonResult踩坑的记录》:本文主要介绍统一返回JsonResult踩坑的记录,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录统一返回jsonResult踩坑定义了一个统一返回类在使用时,JsonResult没有get/set方法时响应总结统一返回

使用SpringBoot整合Sharding Sphere实现数据脱敏的示例

《使用SpringBoot整合ShardingSphere实现数据脱敏的示例》ApacheShardingSphere数据脱敏模块,通过SQL拦截与改写实现敏感信息加密存储,解决手动处理繁琐及系统改... 目录痛点一:痛点二:脱敏配置Quick Start——Spring 显示配置:1.引入依赖2.创建脱敏