使用spring aop + 注解完成对业务操作的日志记录
2016-09-19 19:53
591 查看
话不多说直接上代码,不喜勿喷 @After("@annotation(com.你的路径.service.LogAntn)") public void afterAdvice(JoinPoint joinPoint) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException{ Object[] args = joinPoint.getArgs(); User user = null; Map<String, String> paramsMap = null; for (Object object : args) { if(object instanceof User){ user = (User)object; }else if(object instanceof Map){ paramsMap = (Map<String, String>)object; } } LogAntn logAntn = getAntn(joinPoint); String signature = joinPoint.getSignature().toString(); // 获取目标方法签名 String methodName = signature.substring(signature.lastIndexOf(".") + 1,signature.indexOf("(")); String clazzName = joinPoint.getTarget().getClass().getName(); Class<?> clazz = Class.forName(clazzName); Method[] methods = clazz.getDeclaredMethods(); for (Method method : methods) { if (method.isAnnotationPresent(LogAntn.class)&& method.getName().equals(methodName)) { opObj opObjType = logAntn.opObjType(); String objVal = opObjType.getValue();//操作对象 opType type = logAntn.type(); String opVal = type.getValue();//操作类型 //添加入库操作 } } logger.info("aop log ... this is after Advice......"); }
@Documented @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface LogAntn { enum opType{ // UPDATE,ADD,DELETE,REPORT,SEND,TURNEDDOWN UPDATE("UPDATE","修改/维护"), DEL("DEL","删除/撤销"), ADD("ADD","添加"); private String key; private String value; private opType(String key,String value) { this.key = key; this.value = value; } public String getKey(String key){ return key; } public String getValue() { return value; } }; opType type(); enum opObj{//操作的简单描述 REPORT("REPORT","上报审批"); }; opObj opObjType(); }
//获取注解 public static LogAntn getAntn(JoinPoint joinPoint){ Signature signature = joinPoint.getSignature(); MethodSignature methodSignature = (MethodSignature) signature; Method method = methodSignature.getMethod(); if (method != null) { return method.getAnnotation(LogAntn.class); } return null; }
相关文章推荐
- 使用Spring AOP使用注解记录用户操作日志
- 使用自定义注解+Spring AOP 实现日志记录
- 基于SSM利用SpringAOP切面及自定义注解 记录每次操作记录(操作日志 同理)
- Spring自定义注解+Aop记录用户操作日志
- SpringAOP注解方式记录操作日志(操作模块,操作功能,调用方法,主键信息等)支持多笔操作时记录
- 使用Spring AOP记录Controller层操作日志
- Spring AOP 实现业务日志记录 (注解)
- 在spring-mvc中使用aop进行业务日志的记录
- 使用Spring AOP和自定义注解记录日志
- Spring AOP 自定义注解记录操作日志
- Spring AOP 实现业务日志记录
- spring aop 实现用户操作日志记录功能
- Spring AOP 完成日志记录
- 自定义注解和aop结合使用---自定义日志记录的实现
- 使用Spring AOP中MethodInterceptor记录日志
- Spring AOP 完成日志记录
- 从头认识Spring-3.8 简单的AOP日志实现(注解版)-扩展增加检查订单功能,以便记录并检测输入的参数
- spring2.x使用aop实现声明式日志记录
- Spring AOP 完成日志记录
- 使用AOP与注解记录Java日志