前几天做项目时,在做系统日志这一块,都是在每个方法里手写代码来添加,觉得很繁琐,考虑到spring有aop的功能,便寻思着用AOP来做这个日志功能。
首先需要传入日志记录的具体操作名称,我们可以用java的注解功能来带入参数,代码如下:
/** * 类的方法描述注解 * @author LuoYu */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited public @interface Log { /** 要执行的操作类型比如:add操作 **/ public String operationType() default ""; /** 要执行的具体操作比如:【添加仓库】 **/ public String operationName() default ""; }
注解类编写好之后,就要考虑spring我切面的问题目了,首先我们要创建一个切点,也就是需要插入的代码块,代码如下:
/** * 通过Spring AOP来添加系统日志 * @author LuoYu */ public class LogAspect extends BaseAction{ private static final long serialVersionUID = -5063868902693772455L; private Log logger = LogFactory.getLog(LogAspect.class); @SuppressWarnings( { "rawtypes", "unchecked" } ) public void doSystemLog(JoinPoint point) throws Throwable { Object[] param = point.getArgs(); Method method = null; String methodName = point.getSignature().getName(); if (!(methodName.startsWith("set") || methodName.startsWith("get")||methodName.startsWith("query"))){ Class targetClass = point.getTarget().getClass(); method = targetClass.getMethod(methodName, param[0].getClass()); if (method != null) { boolean hasAnnotation = method.isAnnotationPresent(com.tlj.pcxt.common.logaop.Log.class); if (hasAnnotation) { com.tlj.pcxt.common.logaop.Log annotation = method.getAnnotation(com.tlj.pcxt.common.logaop.Log.class); String methodDescp = annotation.operationType()+annotation.operationName(); if (logger.isDebugEnabled()) { logger.debug("Action method:" + method.getName() + " Description:" + methodDescp); } User appUser=(User) this.getHttpServletRequest().getSession().getAttribute("user"); if(appUser!=null){ try{ com.tlj.pcxt.entity.admin.Log logInfo=new com.tlj.pcxt.entity.admin.Log(); logInfo.setIp(this.getHttpServletRequest().getRemoteAddr()); logInfo.setSubmitUser(appUser); logInfo.setContent(annotation.operationType()+","+appUser.getUserName()+ "执行【"+annotation.operationName()+"】操作,影响数据的ID集合为["+getID(param[0])+"]"); this.logService.save(logInfo); }catch(Exception ex){ logger.error(ex.getMessage()); } } } } } } /** * 通过java反射来从传入的参数object里取出我们需要记录的id,name等属性, * 此处我取出的是id *@author 罗宇 *@date 2013-4-11 *@param obj *@return *@return String */ public String getID(Object obj){ if(obj instanceof String){ return obj.toString(); } PropertyDescriptor pd = null; Method method = null; String v = ""; try{ pd = new PropertyDescriptor("id", obj.getClass()); method = pd.getReadMethod(); v = String.valueOf(method.invoke(obj)); }catch (Exception e) { e.printStackTrace(); } return v; } }
切入代码编写好之后,需要在applicatioContext.xml里配置切入规则,也就是说要在哪些方法执行的时候来切入上面编写的代码:配置如 下:
<aop:aspectj-autoproxy/> <bean id="logAspect" class="com.tlj.pcxt.common.logaop.LogAspect"/> <aop:config> <aop:aspect ref="logAspect"> <aop:pointcut id="logPointCut" expression=" (execution(* com.tlj.pcxt.service.*.*Impl.add*(..))) or (execution(* com.tlj.pcxt.service.*.*Impl.update*(..))) or (execution(* com.tlj.pcxt.service.*.*Impl.delete*(..))) "/> <aop:after pointcut-ref="logPointCut" method="doSystemLog"/> </aop:aspect> </aop:config>
在此我配置的时在方法执行之后插入代码块
<aop:after pointcut-ref="logPointCut" method="doSystemLog"/>
并且是在所有以add,update,delete开头的方法才执行,其余的方法将不再匹配。
调用方法如下,
@Log(operationType="add操作:",operationName="添加仓库房间") public void addWareHouseRoom(WareHouseRoom wareHouseRoom) throws ServiceException { try{ this.getWareHouseRoomDao().save(wareHouseRoom); }catch (Exception e) { throw new ServiceException(e); } }
是在方法头前添加上面自定义的@Log注解,传入相关日志信息
另外,在LogAspect的doSystemLog方法里的
Object[] param = point.getArgs();
就是取出所匹配方法传入的参数,我们记录日志所需要的相关参数就是从这个对象里取出来的,并且在该方法下面的代码会检查所匹配的方法是否有注解@log,如果没有,会直接跳出该方法,不做任何处理.
相关推荐
4、想看spring aop 注解实现记录系统日志并入库等 二、能学到什么 1、收获可用源码 2、能够清楚的知道如何用spring aop实现自定义注解以及注解的逻辑实现 (需要知道原理的请看spring aop源码,此处不做赘述) 3、...
springboot spring aop 拦截器 注解方式实现脱敏(涉及到:pom.xml -->application.properties --->启动类-->拦截器)
Spring Mvc AOP通过注解方式拦截controller等实现日志管理
Spring MVC AOP通过自定义注解方式拦截Controller等实现日志管理, springMVC里做添加AOP拦截,用于捕获异常。
NULL 博文链接:https://conkeyn.iteye.com/blog/2354644
利用自定义注解和spring aop和java反射机制生成用户能够读懂的日志记录。如:用户张三在2013年9月27日17:00执行了用户管理模块的用户删除功能参数为(编号:123456)各位可根据需要写入数据库或者保存到文件。
本文我们通过Spring AOP和Java的自定义注解来实现日志的插入功能,非常不错,具有一定的参考借鉴价值,需要的朋友一起看看吧
Spring AOP(面向切面编程)是Java高级编程中的重要组成部分,它允许程序员以声明的方式处理关注点(例如日志、事务管理等),而不是通过硬编码。本文深入分析了Spring AOP的实现机制,让读者能够更好地理解和应用这...
元注解方式结合AOP,灵活记录操作日志 能够记录详细错误日志为运维提供支持 日志记录尽可能减少性能影响
本代码中还包括了一个java自带注解的例子写的都非常易懂对于hellospring中第一位回复者的问题spring4.0与以前的有什么不同最大的不同包用的不一样了哈哈支持的新技术更多了具体新特性会在后面例子中一点点体现
基于Log4j+SpringAOP+Annotation的可注解日志切面组件 使用方式 @Log(logAfter = true, logBefore = true) public returntype methodName(params) 测试类com.liam.aop.aspect.test.AopLogAnnotationTest 测试输出 ...
springboot 脱敏自定义注解 SpringAOP
1.使用aop切面编程解耦 2.使用spring事件监听保存日志 3.日志内容详细包括请求ip、请求响应数据、异常信息等
AutoLogging 监控日志框架一个非常强大的监控日志输出框架,主要功能包含方法监控日志输出应用内调用链路追踪可以通过SpringAOP动态字节码运行时注入两种方式引入,只要一个注解即可开启所有功能,而且具有强大的可...
Spring Boot是一个流行的Java开发框架,它提供了一种方便的方式来构建高效的、可扩展的企业级应用程序。Aspect-Oriented Programming(AOP)是Spring Boot框架中的一个重要组成部分,它允许开发者通过将横切关注点...
权限控制:基于token方式,禁用session 对各种不同异常进行了全局统一处理 使用lombok简化java代码,让源码更简洁,可读性高 mybatis未进行二次封装,原滋原味,简单sql采用注解,复杂sql采用Mapper.xml配置 ...
Java Spring是一个开源的Java平台,它为基于Java的企业级应用提供了全面的基础架构支持。Spring框架的核心特性可以用于任何Java应用程序,但存在扩展,它们提供了对构建Web应用程序上下文的全面支持。Spring框架的...
支持AOP Spring提供了对AOP的支持,它允许将一些通用任务,如安全、事务、日志等进行集中式处理,从而提高了程序的复用性。 支持声明式事务处理 只需要通过配置就可以完成对事务的管理,而无需手动编程。 方便程序...
Spring.NET以Java版的Spring框架为基础,将Spring.Java的核心概念与思想移植到了.NET平台上。 第一章 序言 第二章 简介 2.1.概述 2.2.背景 2.3.模块 2.4.许可证信息 2.5.支持 第三章 背景 3.1.控制反转 第...
在 Spring实现 AOP面向切面编程, 是通过 @Aspect注解来实现切面的 使用场景 常见用于记录日志, 异常集中处理, 权限验证以及 Web参数有效验证等等 列子1 (演示基本过程 @Aspect @Component public class TestAspect ...