时间过的真快,转眼就一年了,没想到随手写的笔记会被这么多人浏览,不想误人子弟,于是整理了一个优化版,在这里感谢智斌哥提供的建议和帮助,话不多说,进入正题

所需jar包 :spring4.3相关联以及aspectjweaver-1.8.5.jar,jdk  1.7,1.8亲测可用,源码下载链接放在最后,关键代码如下:

1.Action

package com.opr.controller;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;import com.opr.service.UserService;@Controller
@RequestMapping("user")
public class UserController {@Autowired UserService userService;/**** 首页* @param request* @param response* @return ModelAndView*/@RequestMapping("index")public ModelAndView index(HttpServletRequest request,HttpServletResponse response) {return new ModelAndView("index");}/**** 登录* @param request* @param response* @return ModelAndView*/@RequestMapping("userLogin")public ModelAndView userLogin(HttpServletRequest request,HttpServletResponse response,String userName,String password) {ModelAndView mv = new ModelAndView();try {boolean result = userService.userLogin(userName,password);if(result) {mv.setViewName("success");}else {mv.setViewName("error");}} catch (Exception e) {// TODO: handle exception
            e.printStackTrace();}return mv;}}

2.Service

package com.opr.service.impl;import org.springframework.stereotype.Service;import com.opr.annotation.OperLog;
import com.opr.service.UserService;@Service("userService")
public class UserServiceImpl implements UserService {//设置默认值,模拟登录private final String userName = "admin";private final String password = "123456";@Override@OperLog(operType="用户登录",userIndex = 0 )//0为下标,代表传入的第一个参数,这里取userName为示例。实际场景可以在session中取操作人!public boolean userLogin(String userName,String password) {boolean flag = false;if(this.userName.equals(userName) && this.password.equals(password)) {flag = true;}return flag;}}

3.自定义注解

package com.opr.annotation;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;//这里不明白的童鞋可以抽空看看自定义注解
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface OperLog {//操作类型String operType() default "";//操作人String user() default "";//操作人下标int userIndex() default -1;}

4.拦截器

package com.opr.interceptor;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;import com.opr.annotation.OperLog;@Aspect
@Component
public class OperLogInterceptor {//这里写的为环绕触发 ,可自行根据业务场景选择@Before @After//触发条件为:com.opr包下面所有类且注解为OperLog的@Around("within(com.opr..*) && @annotation(operLog)")public Object doAroundMethod(ProceedingJoinPoint pjd,OperLog operLog) throws Throwable {long startTime=System.currentTimeMillis();//开始时间
        Object[] params = pjd.getArgs();//获取请求参数System.out.println("监听到传入参数为:");for(Object param:params) {System.out.println(param);}//###################上面代码为方法执行前#####################Object result  = pjd.proceed();//执行方法,获取返回参数//###################下面代码为方法执行后#####################System.out.println("返回参数为:" + result);String user = operLog.userIndex()==-1?operLog.user():(String)params[operLog.userIndex()];//操作人String operType = operLog.operType();//操作类型System.out.println("操作人: " + user +" 操作类型: " + operType);long endTime=System.currentTimeMillis();//结束时间float excTime=(float)(endTime-startTime)/1000;System.out.println("执行时间:"+excTime+"s");System.out.println("#######################分隔符##########################");return result;} }

5.Spring

<?xml version="1.0" encoding="UTF-8"?>
<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"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/context                http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/aop                http://www.springframework.org/schema/aop/spring-aop-4.3.xsdhttp://www.springframework.org/schema/mvc                    http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"><!-- 设置扫描目录 --><context:component-scan base-package="com.opr" /><!-- 设置请求映射器 --><bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/><!-- 设置适配器处理器 --><bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/><!-- 设置视图处理器 --><bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/"/><property name="suffix" value=".jsp" /></bean><aop:aspectj-autoproxy proxy-target-class="true" /></beans>

6.运行项目后

7.成功

  1).前端效果:

  

  2).后台打印:

  

8.失败

  1).前端效果:

  

  2).后台打印:

   

源码下载地址:http://download.csdn.net/download/qq_16437937/10188600

吐槽一下:CSDN下载最低为2分,分不够的可以邮箱@我,或者在下面留下你的邮箱,我看到了就会发你

邮箱为:Leifeiwangyi@163.com

有什么问题可以留言咱们讨论讨论,谢谢大家

转载于:https://www.cnblogs.com/leifei/p/8194644.html

Spring aop 记录操作日志 Aspect 自定义注解相关推荐

  1. SpringBoot AOP 记录操作日志、异常日志

    使用SpringBoot AOP 记录操作日志.异常日志 我们在做项目时经常需要对一些重要功能操作记录日志,方便以后跟踪是谁在操作此功能.在操作某些功能时也有可能会发生异常,但是每次发生异常要定位原因 ...

  2. 如何使用SpringBoot AOP 记录操作日志、异常日志?

    点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 作者:咫尺的梦想_w cnblogs.com/wm-dv/ ...

  3. 用户登录(使用Spring AOP记录登录日志)

    1.Spring配置文件解析: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&qu ...

  4. spring boot记录操作日志

    1.实体类 import lombok.*; import net.ruifeng.app.cloud.domain.base.AbstractEntityDefault;import javax.p ...

  5. 使用SpringBoot AOP 记录操作日志、异常日志

    https://www.cnblogs.com/wm-dv/p/11735828.html

  6. slf4j注解log报错_SpringBoot自定义日志注解,用于数据库记录操作日志,你用过吗?...

    大家好,我是程序员7歌! 今天我将为大家讲解如何通过自定义注解记录接口访问日志.一般的开发中,有两种方式可以记录日志信息,第一种:把接口日志信息保存到日志文件中,第二种:把接口操作日志保存到数据库中, ...

  7. 用aspect在springboot中记录操作日志至数据库的详细过程

    代码来自若依管理系统的后台,我截取的其中用于记录操作日志的部分 1.切面 2.操作日志表 3.spring工具类 4.客户端工具类 异步工厂(产生任务用) 异步任务管理器 5.服务层 6.控制层 1. ...

  8. AOP实现操作日志记录

    一.设计: 操作日志记录 根据业务场景 一般是需要记录下数据修改更新的日志,查询类可以忽略. 所以需要对指定的某些方法进行记录.这块希望可以结合注解灵活操作,对于注解的方法进行日志记录 操作日志表设计 ...

  9. 【实践】万字干货:如何优雅地记录操作日志?(附代码)

    猜你喜欢 1.如何搭建一套个性化推荐系统? 2.从零开始搭建创业公司后台技术栈 3.某视频APP推荐详解(万字长文) 4.微博推荐算法实践与机器学习平台演进 5.腾讯PCG推荐系统应用实践 6.强化学 ...

最新文章

  1. [html] 你有使用过summary标签吗?说说它的用途
  2. C++实现:自定义数组类型实现相关运算符重载
  3. ILI9486 和 stm32F407 cortex-M4
  4. Json Datable Convert
  5. prism v2之旅(7)
  6. 第四章Python数值计算工具 ——Numpy
  7. 用户增长 - BG/NBD概率模型预测用户生命周期LTV(二)
  8. html5提供类似“JQuery”中操作类名的方法
  9. 菜鸟教程 程序员学习网站
  10. arcgis怎么压缩tif文件_PDF文件怎么压缩才能变小?这样压缩,真的很简单!
  11. 树莓派ssh远程登录连接默认账号密码
  12. Python 自动化工具开源及办公自动化 10 高频操作,代码可直接套用
  13. 【计算机毕业设计】小型OA系统设计与实现Springboot
  14. UVA-10246 - Asterix and Obelix(dijkstra)
  15. 华为服务器进入bios怎么重装系统,华为服务器进bios设置
  16. jpa原生query_SpringDataJpa使用原生sql的小坑
  17. 社保费客户端显示服务器连接异常,社保费客户端登录服务器异常
  18. oc 管理工具 黑苹果 下载_灵越7590黑苹果(win10下)
  19. A. K-divisible Sum
  20. 随笔随想-2022-06-07

热门文章

  1. 记录一个解决了一个下午加一个晚上的问题,关于springMVC上传文件的功能
  2. 《leetcode》single-number-ii
  3. Python爬虫进阶三之Scrapy框架安装配置
  4. spark之2:原理介绍
  5. 指标搭建篇:如何搭建指标体系?——以公众号实战为例
  6. java深度学习(一)Maven创建一个新的ND4J工程
  7. javaweb学习总结(十五):JSP基础语法
  8. Elasticsearch技术解析与实战(六)Elasticsearch并发
  9. linux挂载NTFS分区
  10. Python多线程原理与实现