SpringBoot 使用@Aspect进行日志管理(基于反射代理模式+注解Log)
来源:互联网 发布:vue.js权威指南 微盘 编辑:程序博客网 时间:2024/05/22 00:35
在上一篇“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)、数据库添加日志数据
* 附件代码下载:https://git.oschina.net/CatalpaFlat/JouranlDemo2.git*
阅读全文
0 0
- SpringBoot 使用@Aspect进行日志管理(基于反射代理模式+注解Log)
- SpringBoot 使用@Aspect进行日志管理(基于反射代理模式+动态修改注解Log)
- SpringBoot 使用@Aspect进行日志管理(基于反射代理模式)
- SpringBoot @Aspect注解详情
- Spring4-aspect日志管理实例
- 基于apache的日志log管理
- springboot log日志打印
- SpringBoot学习:(二)使用logback进行日志记录
- Spring中Aspect注解使用
- 代理模式-----进行事务的注解
- SpringBoot学习:使用logback进行日志记录
- Aspect注解进行通知的配置
- 基于@Aspect的AOP注解配置
- 基于注解@Aspect的AOP实现
- Spring 基于 Aspect 注解的增强实现
- Spring_AOP_XML使用Aspect实现动态代理(常用)
- Spring_AOP_XML使用Aspect实现动态代理(常用) .
- 反射,注解,泛型,代理
- php中正则表达式详解
- request初步和获取表单数据
- 20171009
- 简单理解c语言——‘\0’ ,‘0’, “0” ,0之间的区别
- Ubuntu环境下的nwjs安装使用
- SpringBoot 使用@Aspect进行日志管理(基于反射代理模式+注解Log)
- 【超详细图文教程】用SpringBoot+Maven搭建SSM框架
- c++一些入门基础,也是初学者容易卡住的地方
- 屏幕最大话和最小化,隐藏任务栏
- Effective Java 读书笔记(八):异常
- 个人英雄主义和个人品牌
- Linux之数据流重导向
- 2017常见前端面试题
- 网易视频云分享:流媒体技术介绍(下篇)