SpringMVC中基于AOP的自定义注解记录日志

来源:互联网 发布:食物热量软件 编辑:程序博客网 时间:2024/05/21 11:21

最近发现以前用的非注解式的记录操作日志非常麻烦,于是寻思用注解+AOP的方式来改造service层的日志记录

首先自定义操作日志注解


@Target(ElementType.METHOD)//作用于方法上@Retention(RetentionPolicy.RUNTIME)public @interface SystemLog {  /** * 模块名称  如:用户管理 */String module() default "";/** * 操作类型   如:添加用户、删除用户 */String operationType() default "";   }  


然后定义切面类

import java.lang.reflect.Method;import java.util.Date;import java.util.HashMap;import java.util.Map;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Pointcut;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import com.hj.annotation.SystemLog;/*** service层日志切面类* @author hujian* @date 2017年5月26日* @version 1.0*/@Aspect    @Component public class SystemLogAspect {        @Autowired    private OperationLogService operationLogService;        //本地异常日志记录对象        private static final Logger LOGGER = LoggerFactory.getLogger(SystemLogAspect.class);    //声明AOP切入点,凡是使用了@SystemLog的方法均被拦截    @Pointcut("@annotation(com.hj.annotation.SystemLog)")       public void log() {        System.out.println("这是一个切入点...");    }        /**     * 记录service层日志     * @author hujian     * @date 2017年5月26日     * @version 1.0     * @param joinPoint     */    @After("log()")       public void afterExec(JoinPoint joinPoint) {          LOGGER.info("日志记录");          try {             // 获取注解信息            MethodSignature ms = (MethodSignature) joinPoint.getSignature();            Method method = ms.getMethod();            SystemLog log = method.getAnnotation(SystemLog.class);            if (log != null) {                //获取当前登录用户                Userinfo loginUser = UserContext.getLoginUser();                if (loginUser != null) {                    //添加操作日志                    operationLogService.addLog(loginUser, log.module(), log.operationType());                }            }         }  catch (Exception e) {                 LOGGER.error("异常信息:{}", e);           }           }      }
这里@Pointcut里面是自定义注解的全路径


然后在spring-service的xml中配置

<!-- 启动对@AspectJ注解的支持  -->   <aop:aspectj-autoproxy proxy-target-class="true" />  <!-- 扫描切面  --> <context:component-scan base-package="com.hj.aop" /><!-- @Service扫描 --><context:component-scan base-package="com.hj.service"/>

在service层使用操作日志注解

@SystemLog(module="用户管理",operationType="修改密码")public void editPwd(String pwd) {Userinfo loginUser = UserContext.getLoginUser();if (loginUser != null) {     //修改密码     loginUser.setPassword(MD5Utils.md5(pwd));     userinfoMapper.updateByPrimaryKey(loginUser);}}

至此,基于AOP的自定义注解记录操作日志就完成了。。。



如果,要在controller层也用该注解,可以在springmvc.xml中增加如下配置

<!-- 启动对@AspectJ注解的支持  -->   <aop:aspectj-autoproxy proxy-target-class="true" />  <!-- 扫描切面  --> <context:component-scan base-package="com.hj.aop" /><!-- @Controller扫描 --><context:component-scan base-package="com.hj.controller"/>



笔者测试过,只配置spring-service.xml或者只配置springmvc.xml,但是结论就是,配置了哪个,哪层的注解才能被扫描到。。。我也很纠结这个地方。


第一次写博客,不知道怎么写,写得不好的地方,请多多包涵。


原创粉丝点击