AOP实现用户操作日志记录
2017-10-16 14:55
513 查看
一、自定义注解
在使用aop切面拦截的时候需要统一的日志描述,这个日志描述,我们使用自定义注解来为每个方法添加日志自定义描述内容。创建一个自定义注解类
@Target({ElementType.PARAMETER, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface UserLogAnnotation { String module() default ""; String methods() default ""; }
SpringMvc配置aop
SpringMvc.xm增加以下代码
xmlns:aop="http://www.springframework.org/schema/aop" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd <aop:aspectj-autoproxy proxy-target-class="true" /> <!-- aop的执行类 --> <bean id="userLogAopAction" class="com.topwalk.bdp.log.web.UserLogAopAction"/>
AOP拦截执行类
@Aspect public class UserLogAopAction extends WebController { @Autowired private UserLogService userLogService; @Pointcut("execution(* com.topwalk.icgap.*.web..*.*(..))") private void controllerAspect() {}// 定义一个切入点 @Around("controllerAspect()") public Object around(ProceedingJoinPoint pjp) throws Throwable { // 创建日志实体对象 UserLog log = new UserLog(); // 获取当前登录用户 User userInfo = HttpUtil.getSessionUser(); // 设置用户信息 log.setAccount(userInfo.getAccount()); log.setUserId(userInfo.getId()); log.setUserName(userInfo.getName()); log.setOrgCode(userInfo.getOrgCode()); log.setOrgName(userInfo.getOrgName()); // 当前IP地址 String theIp = HttpUtil.getIpAddr(); log.setIp(theIp); // 获取当前拦截的实体类 /*Object target = pjp.getTarget();*/ // 拦截的方法参数类型 Signature sig = pjp.getSignature(); MethodSignature methodSignature = (MethodSignature) sig; Object object = null; Method method = null; try { /* method = target.getClass().getMethod(methodName, parameterTypes); */ method = methodSignature.getMethod(); } catch (SecurityException e1) { e1.printStackTrace(); } if (null != method) { // 判断是否包含自定义的注解 if (method.isAnnotationPresent(UserLogAnnotation.class)) { UserLogAnnotation userLogAnnotation = method .getAnnotation(UserLogAnnotation.class); String module = userLogAnnotation.module(); String methods = userLogAnnotation.methods(); try { //拼接描述信息 String description = "[帐号:"+log.getAccount()+"],"+"操作"+module+"模块,"+"执行"+methods; log.setDescription(description); log.setModule(Module.OTHER); log.setFunction(Function.OTHER); super.info(log); object = pjp.proceed(); } catch (Exception e) { e.printStackTrace(); } } else {// 没有包含注解 object = pjp.proceed(); } } else { // 不需要拦截直接执行 object = pjp.proceed(); } return object; } }
相关文章推荐
- spring aop 实现用户操作日志记录功能
- 微软企业库5.0 学习之路——第九步、使用PolicyInjection模块进行AOP—PART4——建立自定义Call Handler实现用户操作日志记录
- springMVC +Mybatis +spring aop 实现用户系统操作日志记录
- [EntLib]微软企业库5.0 学习之路——第九步、使用PolicyInjection模块进行AOP—PART4——建立自定义Call Handler实现用户操作日志记录
- 使用Aop面向切面技术实现记录详细操作日志功能
- spring aop 实现操作日志记录
- spring的AOP实现记录操作日志
- Spring AOP 实现系统操作日志记录
- AOP实战---对用户/角色/规则操作日志记录
- springAOP记录用户操作日志
- 通过SpringAOP获取request中所有参数,记录用户操作日志
- spring aop 实现用户操作日志记录功能(转)
- Spring自定义注解+Aop记录用户操作日志
- 使用Spring AOP使用注解记录用户操作日志
- Spring AOP 实现系统操作日志记录
- 《web工程aop实现前台操作日志记录》初稿
- 基于注解的aop实现用户操作日志管理
- 利用spring AOP记录用户操作日志的方法示例
- MongoDB记录操作日志的Base类实现