spring使用AOP做实现操作记录的功能
2019-01-21 11:03
483 查看
spring使用AOP做实现操作记录的功能
首先定义一个注解:
@Target({ ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited public @interface logAnnotation { /** 要执行的操作类型比如:add操作 **/ public String operationType() default ""; /** 要执行的具体操作比如:添加用户 **/ public String operationName() default ""; }
写一个AOP类:
// 该注解标示该类为切面类 @Aspect // 注入依赖 @Component public class OperationLogAspect { private HttpServletRequest request = null; @Resource ILogService logService; /** * * @Description: 方法调用后触发 记录结束时间 * @author fei.lei * @date 2016年11月23日 下午5:10 * @param joinPoint */ public void after(JoinPoint joinPoint) { request = getHttpServletRequest(); HttpSession session = request.getSession(); String account = (String) session.getAttribute("account"); //获取被代理的对象 String targetName = joinPoint.getTarget().getClass().getName(); //获取封装了署名信息的对象,在该对象中可以获取到目标方法名,所属类的Class等信息 String methodName = joinPoint.getSignature().getName(); //获取传入目标方法的参数对象 Object[] arguments = joinPoint.getArgs(); Class targetClass = null; try { targetClass = Class.forName(targetName); } catch (ClassNotFoundException e) { e.printStackTrace(); } Method[] methods = targetClass.getMethods(); String operationName = ""; for (Method method : methods) { if (method.getName().equals(methodName)) { Class[] clazzs = method.getParameterTypes(); if (clazzs!=null&&clazzs.length == arguments.length&&method.getAnnotation(logAnnotation.class)!=null) { operationName = method.getAnnotation(logAnnotation.class).operationName(); break; } } } Object obj =request.getParameter("fileName"); Log log = new Log(); log.setOperation(operationName); if(account != null && operationName != ""){ log.setUserid(Integer.valueOf(account)); Date nowDate = new Date(); long dateGetTime = nowDate.getTime(); Timestamp timestamp = new Timestamp(dateGetTime); log.setOperationtime(timestamp); logService.insertLog(log); } } /** * @Description: 获取request * @author fei.lei * @date 2016年11月23日 下午5:10 * @param * @return HttpServletRequest */ public HttpServletRequest getHttpServletRequest(){ RequestAttributes ra = RequestContextHolder.getRequestAttributes(); ServletRequestAttributes sra = (ServletRequestAttributes)ra; HttpServletRequest request = sra.getRequest(); return request; } }
在spring.xml中进行配置
<bean id="logAspect" class="com.honger.annotation.OperationLogAspect"></bean> <aop:config> <!--调用日志类--> <aop:aspect id="LogAspect" ref="logAspect"> <!--配置在controller包下所有的类在调用之前都会被拦截--> <aop:pointcut id="log" expression="execution(* com.honger.controller.UserController.*(..))"/> <!-- 方法后触发 --> <aop:after pointcut-ref="log" method="after"/> </aop:aspect> </aop:config>
使用
最后在想要记录的操作上加上相应的注解就可以了 /** * 修改用户 * @param user 修改后的用户信息 * @return */ @RequestMapping("/update") @ResponseBody @logAnnotation(operationType="修改操作",operationName="修改用户") public ServerResult update(User user) { //修改用户 ServerResult<Integer> serverResult = userService.updateUser(user); UserRole userRole = new UserRole(); userRole.setUid(user.getUid()); userRole.setRid(Integer.parseInt(user.getRole())); //根据uid修改用户对应的rid userRoleService.update(userRole); return serverResult; }
作者:haveFuner
来源:CSDN
原文:https://blog.csdn.net/qq_31118837/article/details/85986494
版权声明:本文为博主原创文章,转载请附上博文链接!
相关文章推荐
- 使用Aop面向切面技术实现记录详细操作日志功能
- spring aop 实现用户操作日志记录功能
- 从头认识Spring-3.8 简单的AOP日志实现(注解版)-扩展增加检查订单功能,以便记录并检测输入的参数
- Spring AOP 实现系统操作日志记录
- SpringAOP注解方式记录操作日志(操作模块,操作功能,调用方法,主键信息等)支持多笔操作时记录
- .NetCore中使用AspectCore、ExceptionLess 实现AOP操作日志记录
- 使用jdk和cglib实现spring的aop功能
- spring.net结合普通三层(实现IOC 及AOP中的异常记录功能)
- 15_传智播客Spring2.5视频教程_使用JDK中的Proxy技术实现AOP功能
- Spring AOP 实现日志记录功能
- 使用Spring AOP注解方式实现表单重复提交验证功能
- Spring AOP进行日志记录,管理 (使用Spring的拦截器功能获取对action中每个方法的调用情况,在方法调用前
- Spring AOP进行日志记录,管理 (使用Spring的拦截器功能获取对action中每个方法的调用情况,在方法调用前和调用后记录相关日志。)
- spring.net结合普通三层(实现IOC 及AOP中的异常记录功能)
- 使用jdk和cglib实现spring的aop功能
- ITCAST视频-Spring学习笔记(使用JDK中的Proxy技术实现AOP功能)
- Spring AOP进行日志记录,管理 (使用Spring的拦截器功能获取对action中每个方法的调用情况)
- 微软企业库5.0 学习之路——第九步、使用PolicyInjection模块进行AOP—PART4——建立自定义Call Handler实现用户操作日志记录
- Spring AOP 实现系统操作日志记录
- springMVC +Mybatis +spring aop 实现用户系统操作日志记录