博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot 使用@Aspect进行日志管理(基于反射代理模式+注解Log)
阅读量:6915 次
发布时间:2019-06-27

本文共 3267 字,大约阅读时间需要 10 分钟。

在上一篇“SpringBoot 使用@Aspect进行日志管理(基于反射代理模式)”的基础上,添加注解进行日志管理

1、添加日志注解

import java.lang.annotation.*;/** * 日志注解 * Created by 陈梓平 on 2017/9/7. */@Target({ElementType.PARAMETER, ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface Log {    /** 要执行的操作类型比如:add **/    public String operationType() default "";    /** 要执行的模块名称如:Carouse **/    public String modularTypeName() default "";}复制代码

2、修改JournalServiceAspect类

import com.chen.enums.ResultEnum;import com.chen.exception.CustomException;import com.chen.staticInfos.StaticInfo;import com.chen.utils.JournalUtils;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.Signature;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 org.apache.commons.lang.StringUtils;import org.springframework.transaction.annotation.Transactional;/** * 日志切面 * Created by 陈梓平 on 2017/9/11. */@Component@Aspectpublic class JournalAspect {    /**日志输出*/    private static final Logger logger = LoggerFactory.getLogger(JournalAspect.class);    /**日志工具类*/    @Autowired    private JournalUtils aspectJournalUtils;    /**service层切面*/    private final String POINT_CUT = "execution(* com.chen.service..*(..))";    @Pointcut(POINT_CUT)    private void pointcut(){}    /**     * 后置最终通知(目标方法只要执行完了就会执行后置通知方法)     * 日志管理     * @param joinPoint     */    @After(value = "pointcut()")    @Transactional    public void doAfterAdvice(JoinPoint joinPoint) throws CustomException, ClassNotFoundException {        String targetName = joinPoint.getTarget().getClass().getName();        String methodName = joinPoint.getSignature().getName();        Object[] arguments = joinPoint.getArgs();        Class targetClass = Class.forName(targetName);        Method[] methods = targetClass.getMethods();        int modulerType = -1;        int opreationType = -1;        if (methods.length>0){            for (Method method : methods) {                if (method.getName().equals(methodName)) {                    Class[] clazzs = method.getParameterTypes();                    if (clazzs.length == arguments.length) {                        if (method.getAnnotation(JournalLog.class)!=null){                            modulerType = method.getAnnotation(JournalLog.class).modularTypeName();                            opreationType = method.getAnnotation(JournalLog.class).operationType();                            break;                        }                    }                }            }        }        //3.添加日志        if (modulerType!=-1&&opreationType!=-1)            //TODO 3.1 从请求获取用户id            aspectJournalUtils.addJournalInfo(modulerType,opreationType, 10086);    }}复制代码

3、修改JournalServiceImpl添加日志注解

/** * Created by 陈梓平 on 2017/9/11. */@Servicepublic class JournalServiceImpl implements JournalService {    @Override    @JournalLog(operationType = StaticInfo.OPERATIONTYPE_ADD,modularTypeName = StaticInfo.MODEULARTTYPE_FIRST)    public Result add() {        return ResultUtils.success(ResultEnum.OK);    }}复制代码

4、测试结果

1)、接口调用

测试结果
2)、数据库添加日志数据
数据库测试结果
附件代码下载:

转载地址:http://gxicl.baihongyu.com/

你可能感兴趣的文章
vmstat
查看>>
Word 2003操作技巧之改变默认字体及恢复方法
查看>>
redux-form(V7.4.2)笔记(三)之Flow简介
查看>>
Asp.Net 网站优化 数据库优化措施 使用主从库(上)
查看>>
华为存储行吗?!始终保持一种学习的心态
查看>>
wxPython StyledTextCtrl events
查看>>
Docker
查看>>
Linux命令行测试网速
查看>>
绍兴市×××局虚拟化容灾设备项目<100万
查看>>
Android JSON And Object Cast
查看>>
程序设计专业问与答
查看>>
《火星人开发纪实:敏捷开发一千零一夜》第四个月:用户故事的分类(下)...
查看>>
ocjp 171-180
查看>>
计算机网络数据安全管理措施
查看>>
javascript时间格式转换(今天,昨天,前天)
查看>>
linux总结之档案权限
查看>>
我的友情链接
查看>>
php-fpm.conf两个至关重要的参数
查看>>
hive任务提交的相关权限认证详析
查看>>
H3C园区网最佳设计模型
查看>>