春如利用aop操作日志功?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
<强> 1。在pom中添加所需依赖
强>
创建一个springboot工程,添加所需要的依赖,持久化用的是mybatis
& lt; dependency> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-starter-web & lt;/dependency> & lt; !——springboot aop依赖——比; & lt; dependency> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-starter-aop & lt;/dependency> & lt; !——mybatis祝辞 & lt; dependency> & lt; groupId> org.mybatis.spring.boot & lt; artifactId> mybatis-spring-boot-starter & lt; version> 2.1.3 & lt;/dependency> & lt; !——mysql连接——比; & lt; dependency> & lt; groupId> mysql & lt; artifactId> mysql-connector-java & lt; version> 8.0.19 & lt; scope> runtime & lt;/dependency> & lt; !——lombok祝辞 & lt; dependency> & lt; groupId> org.projectlombok & lt; artifactId> lombok & lt; optional> true & lt;/dependency>
2。创建日志实体类
进口lombok.Data; 进口java.io.Serializable; @ data 公共类AdminLog实现Serializable { 私有静态最终长serialVersionUID=-291495801959706565 l; 私人整数id;//日志记录id 私人整数标识;//操作人id 私人字符串的用户名;//操作人的名字 私人字符串loginip;//登录ip 私人int类型; 私人字符串url; 私人字符串操作; 私人字符串createtime; 私人字符串的话; }
3。自定义日志注解
进口java.lang.annotation。*;/* * *自定义日志注解 */@Target (ElementType.METHOD)//注解防止位置 @Retention (RetentionPolicy.RUNTIME)//运行时可见 @Documented//生成文档 公共@ interface MyLog { 字符串操作()默认“,“; int类型(); }
4。创建aop切面处理类
进口cn。* * * .springaopdemo.anno.MyLog; 进口cn。* * * .springaopdemo.dao.MyLogMapper; 进口cn。* * * .springaopdemo.pojo.Admin; 进口cn。* * * .springaopdemo.pojo.AdminLog; 进口org.apache.logging.log4j.LogManager; 进口org.apache.logging.log4j.Logger; 进口org.aspectj.lang.JoinPoint; 进口org.aspectj.lang.annotation.Aspect; 进口org.aspectj.lang.annotation.Before; 进口org.aspectj.lang.annotation.Pointcut; 进口org.aspectj.lang.reflect.MethodSignature; 进口org.springframework.beans.factory.annotation.Autowired; 进口org.springframework.stereotype.Component; 进口org.springframework.web.context.request.RequestContextHolder; 进口org.springframework.web.context.request.ServletRequestAttributes; 进口javax.servlet.http.HttpServletRequest; 进口java.lang.reflect.Method;/* * *切面处理类 */@Aspect @ component 公开课SysLogAspect {/* * *使用log4j2把一些信息打印在控制台上面 */私有静态最终记录器日志=LogManager.getLogger (SysLogAspect.class); @ autowired 私人MyLogMapper MyLogMapper;//定义切点@Pointcut//在注解的位置切入代码 @Pointcut (“@annotation (cn。* * * .springaopdemo.anno.MyLog)“) 公共空间logPointCut () { }//切面配置为前置通知 @Before (“logPointCut ()“) 公共空saveOperation(连接点的连接点){ log.info(“- - - - - - - - - - - - - - - - - -接口日志记录- - - - - - - - - - - - -产生绯闻;);//创建一个日志对象 AdminLog AdminLog=new AdminLog ();//获取切面织处入点的方法 MethodSignature签名=(MethodSignature) joinPoint.getSignature ();//获取切入点所在的方法 方法方法=signature.getMethod ();//获取操作日志的属性值 MyLog MyLog=method.getAnnotation (MyLog.class); 如果(myLog !=null) {//操作事件 字符串操作=myLog.operation (); adminLog.setOperation(操作);//日志类型 int类型=myLog.type (); adminLog.setType(类型); log.info(“操作=?+手术+“,类型=?+类型); }//获取url HttpServletRequest请求=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes ()) .getRequest (); .toString字符串requestURL=request.getRequestURI () (); adminLog.setUrl (requestURL);//获取客户端ip 字符串ip=request.getRemoteAddr (); adminLog.setLoginip (ip);//获取操作人账号,姓名(需要提前将用户信息保存到会话) 管理员管理=(管理)request.getSession () .getAttribute (“admin"); 如果(admin !=null) { 整数id=admin.getId (); 字符串名称=admin.getName (); adminLog.setUserId (id); adminLog.setUserName(名称); } log.info (“url=?+ requestURL +“, ip=?+ ip);//调用服务保存操作实体类到数据库//可以在这设置id,因为是测试,这里就使用的是数据库的自增id myLogMapper.insertLog (adminLog); } }春如利用aop操作日志功