Spring aop 记录操作日志 Aspect 自定义注解
时间过的真快,转眼就一年了,没想到随手写的笔记会被这么多人浏览,不想误人子弟,于是整理了一个优化版,在这里感谢智斌哥提供的建议和帮助,话不多说,进入正题
所需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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://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
有什么问题可以留言咱们讨论讨论,谢谢大家
Spring aop 记录操作日志 Aspect 自定义注解的更多相关文章
- Spring aop 记录操作日志 Aspect
前几天做系统日志记录的功能,一个操作调一次记录方法,每次还得去收集参数等等,太尼玛烦了.在程序员的世界里,当你的一个功能重复出现多次,就应该想想肯定有更简单的实现方法.于是果断搜索各种资料,终于搞定了 ...
- 使用SpringBoot AOP 记录操作日志、异常日志
平时我们在做项目时经常需要对一些重要功能操作记录日志,方便以后跟踪是谁在操作此功能:我们在操作某些功能时也有可能会发生异常,但是每次发生异常要定位原因我们都要到服务器去查询日志才能找到,而且也不能对发 ...
- [编码实践]SpringBoot实战:利用Spring AOP实现操作日志审计管理
设计原则和思路: 元注解方式结合AOP,灵活记录操作日志 能够记录详细错误日志为运营以及审计提供支持 日志记录尽可能减少性能影响 操作描述参数支持动态获取,其他参数自动记录. 1.定义日志记录元注解, ...
- spring-boot-route(十七)使用aop记录操作日志
在上一章内容中--使用logback管理日志,我们详细讲述了如何将日志生成文件进行存储.但是在实际开发中,使用文件存储日志用来快速查询问题并不是最方便的,一个优秀系统除了日志文件还需要将操作日志进行持 ...
- springmvc集成aop记录操作日志
首先说明一下,这篇文章只做了记录日志相关事宜 具体springmvc如何集成配置aop对cotroller进行拦截,请看作者的另一篇文章 http://www.cnblogs.com/guokai87 ...
- 使用spring aop 记录接口日志
spring配置文件中增加启用aop的配置 <!-- 增加aop 自动代理配置 --> <aop:aspectj-autoproxy /> 切面类配置 package com. ...
- 用AOP记录操作日志,并写进数据库。
先用AOP注解 1 package com.vlandc.oss.apigate.log.aspect; import java.util.Map; import java.util.Optional ...
- spring reactor记录操作日志
1.注册日志的类: @Configuration@EnableReactorpublic class ReactorConfig { /** * * 〈注册审计日志 Reactor〉 */ @Bean ...
- Spring Boot中使用AOP记录请求日志
这周看别人写的springboot后端代码中有使用AOP记录请求日志,以前没接触过,因此学习下. 一.AOP简介 AOP为Aspect Oriented Programming的缩写,意为:面向切面编 ...
随机推荐
- 《Python学习手册》(三)
string 字符串,和列表.元组,都适用序列操作. 关于python转义字符 迭代 for x in S: print(x) [c * 2 for c in S] Comparasion: > ...
- centos7 -lvm卷组
老忘,记一下 基本的逻辑卷管理概念: PV(Physical Volume)- 物理卷 物理卷在逻辑卷管理中处于最底层,它可以是实际物理硬盘上的分区,也可以是整个物理硬盘,也可以是raid设备. ...
- eclipse部署的web项目没有添加到Tomcat的webapps目录下解决方法
eclipse没有像myeclipse那样,添加web项目时会自动部署到Tomcat的webapps目录下. 而是部署到了eclipse的.metadata\.plugins\org.eclipse. ...
- tomcat官方下载连接——安装版&绿色版
Tomcat绿色版Windows64位9.0.10 http://mirror.bit.edu.cn/apache/tomcat/tomcat-9/v9.0.10/bin/apache-tomcat- ...
- 关于Node.js的__dirname,__filename,process.cwd(),./文件路径的一些坑
探索 计算机不会欺骗人,一切按照规则执行,说找不到这个文件,那肯定就是真的找不到,至于为什么找不到,那就是因为我们理解有偏差,我最初理解的'./'是当前执行js文件所在的文件夹的绝对路径,然后Node ...
- aliyun阿里云Maven仓库地址——加速你的maven构建 - 转载
maven仓库用过的人都知道,国内有多么的悲催.还好有比较好用的镜像可以使用,尽快记录下来.速度提升100倍. http://maven.aliyun.com/nexus/#view-reposito ...
- Pandas基本功能
到目前为止,我们了解了三种Pandas数据结构以及如何创建它们.接下来将主要关注数据帧(DataFrame)对象,因为它在实时数据处理中非常重要,并且还讨论其他数据结构. 系列基本功能 编号 属性或方 ...
- cssrem 比例适配理解
cssrem只是帮你自动计算,省去了你在切图时,从设计稿拿到的px再根据比例转换成rem的中间过程. 这个40我无法猜测, 以前设计稿给的都是按640px(iphone5s的宽)来的,我们就按照这个比 ...
- Java动态代理的总结
最近和一个好友在聊起Mybatis时,他问用Mybatis我们只是配置好mapper,然后写dao层接口就实现了dao层方法.然后我说我觉得用动态代理可以实现.然后他又说感觉动态代理和外观模式没什么区 ...
- 四十一 Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)基本的索引和文档CRUD操作、增、删、改、查
elasticsearch(搜索引擎)基本的索引和文档CRUD操作 也就是基本的索引和文档.增.删.改.查.操作 注意:以下操作都是在kibana里操作的 elasticsearch(搜索引擎)都是基 ...