在spring-mvc中使用aop进行业务日志的记录
来源:互联网 发布:公安局网络通缉人员 编辑:程序博客网 时间:2024/06/01 08:49
思路:自定义一个注解类,在需要记录系统业务日志(如登录、修改菜单、删除数据)的时候,在调用方法前使用该注解。定义一个切面,当系统拦截到使用自定义注解的方法的时候,执行aop切面。aop切面拦截到调用的方法后获取注解的参数,将注解的参数记录到日志文件。
流程:1、在maven中添加aop依赖,支持注解的依赖;
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version></dependency><dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.0</version></dependency><dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.7</version></dependency>
在springmvc配置文件中启动aspecaspecJ支持,自动扫描aop
<!-- 启动AspectJ支持 只对扫描过的bean有效--><aop:aspectj-autoproxy proxy-target-class="true" />
2、创建注解类BussinessLog,注解的语法参考
@Inherited@Retention(RetentionPolicy.RUNTIME)//生命周期运行时保存@Target({ElementType.METHOD})//注解只用于方法public @interface BussinessLog { String module() default "";//模块名 String methoed() default "";//操作方法}
3、定义类处理日志记录的逻辑,aop切面 pointcut expression表达式参考 @Around的执行顺序参考
/** * @author luotao * @Description 记录日志的aop,先使用pointcut expression定义一个切面 * @Date Created on 2017/11/21. */@Aspect@Componentpublic class LogAction { private Log log = LogFactory.getLog(LogAction.class); @Autowired private LogService logService; /** * 定义切面,使用注解BussinessLog注解的方法都会被拦截 */ @Pointcut("@annotation(com.shaun.commons.log.BussinessLog)") public void cutService(){ } /** * 配置环绕通知 * 首先获取拦截的方法对象。通过方法对象获取拦截的标注对象,获取标注参数,记录日志 * @param point * @return */ @Around(value = "cutService()") public Object around(ProceedingJoinPoint point)throws Throwable{ Object object = null; //获取拦截的方法 Signature sig = point.getSignature(); if(!(sig instanceof MethodSignature)){ throw new IllegalArgumentException("该注解只能使用在方法上!"); } MethodSignature msig = (MethodSignature) sig; //接下来通过拦截的方法名获取使用标注的方法 Method method = null; try{ method = point.getTarget().getClass().getMethod(msig.getName(),msig.getParameterTypes()); }catch (NoSuchMethodException e){ log.error(e); } //如果拦截的方法不为空,说明有方法使用了BussinessLog注解 if (method!=null){ //再次判断方法是否使用BussinessLog注解 if(method.isAnnotationPresent(BussinessLog.class)){ //获取方法使用的注解实例 BussinessLog annotation = method.getAnnotation(BussinessLog.class); LogEntity logEntity = new LogEntity(); logEntity.setModule(annotation.module()); logEntity.setMethod(annotation.methoed()); HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); UserAccount userAccount = (UserAccount) request.getSession().getAttribute("userAccountInfo"); logEntity.setUserId(userAccount.getUserId()); logEntity.setIp(CusAccessObjectUtil.getIpAddress(request)); try { object = point.proceed(); logEntity.setCommit("执行成功!"); logService.saveLog(logEntity); }catch (Throwable e){ logEntity.setCommit("发生异常!"); logService.saveLog(logEntity); } } else { object = point.proceed(); } }else { object = point.proceed(); } return object; }}
4、注解的使用,访问requestmapping时,使用注解,进行日志的记录
@BussinessLog(module = "系统登录",methoed = "首页")@RequestMapping("indexAction.do")public String IndexAction(){ return "index";}
参考:
https://www.cnblogs.com/peida/archive/2013/04/24/3036689.html
http://blog.csdn.net/kkdelta/article/details/7441829
http://blog.csdn.net/rainbow702/article/details/52185827
阅读全文
0 0
- 在spring-mvc中使用aop进行业务日志的记录
- 利用Spring的AOP进行日志记录
- Spring AOP进行日志记录,管理 (使用Spring的拦截器功能获取对action中每个方法的调用情况,在方法调用前)
- Spring AOP进行日志记录,管理 (使用Spring的拦截器功能获取对action中每个方法的调用情况,在方法调用前
- Spring AOP进行日志记录,管理 (使用Spring的拦截器功能获取对action中每个方法的调用情况,在方法调用前
- Spring AOP进行日志记录,管理 (使用Spring的拦截器功能获取对action中每个方法的调用情况,在方法调用前
- 使用spring aop + 注解完成对业务操作的日志记录
- spring中使用aop记录日志
- Spring AOP 实现业务日志记录
- Spring AOP 实现业务日志记录
- Spring AOP 实现业务日志记录
- 在ASP.NET MVC中使用Log4Net进行多种HttpCode日志的记录
- 使用Spring AOP 的@AspectJ记录日志
- Spring AOP进行日志记录,管理 (使用Spring的拦截器功能获取对action中每个方法的调用情况,在方法调用前和调用后记录相关日志。)
- Spring AOP进行日志记录,管理 (使用Spring的拦截器功能获取对action中每个方法的调用情况,在方法调用前和调用后记录相关日志。)
- Spring AOP进行日志记录,管理 (使用Spring的拦截器功能获取对action中每个方法的调用情况,在方法调用前和调用后记录相关日志。)
- Spring AOP进行日志记录,管理
- Spring AOP进行日志记录,管理
- 二叉树的建立与遍历
- TensorFlow入门(八)tensorboard 的一个简单示例
- vue的父子构造器
- 将HTML页面部分内容导出为PDF
- UE4蓝图节点翻译---Get Components by Class
- 在spring-mvc中使用aop进行业务日志的记录
- Maven 问题解决
- Json对象和Json字符串的区别,以及Springmvc接受json字符串类型。
- Class.forName()用法详解
- LintCode 83. 落单的数II
- 【Scikit-Learn 中文文档】朴素贝叶斯
- Java中对List集合的常用操作
- 35 个 Java 代码性能优化总结
- Java字符流