SpringAop之日志(读配置文件方式)
发布时间:2020-12-15 07:14:25 所属栏目:Java 来源:网络整理
导读:读配置文件的目的在于减少代码上的冗余,这个冗余通常指加注解之类的。 比方说,我们原来的代码是这样: @GetMapping( " /list " )@Log(title = 查询用户列表 " ,businessType = BusinessType.QUERY) public AjaxResult list() { return AjaxResult.success(u
读配置文件的目的在于减少代码上的冗余,这个冗余通常指加注解之类的。 比方说,我们原来的代码是这样: @GetMapping("/list") @Log(title = 查询用户列表",businessType = BusinessType.QUERY) public AjaxResult list() { return AjaxResult.success(userService.queryUserListInfo()); } 相当于每个我都要加上@Log,我才能在aop中将其插入日志表(识别功能),现在我觉得这样太麻烦了。所以我可以这样做,也就是将@Log抽取一个配置文件,这个配置文件,团队定个规矩,上线前统一更改即可。 于是我的AOP代码就变成这样: @Aspect @Component public class LogAspect { static LogAspect logAspect; @PostConstruct void init() { logAspect = this; } @Pointcut(execution(public * com.eqics.blog.controller..*.*(..))) Pointcut() { System.out.println(切点); } //@Around:环绕通知 @Around(Pointcut()) @Transactional(isolation = Isolation.DEFAULT) Object Around(ProceedingJoinPoint pjp) throws Throwable { Map<String,Object> data = new HashMap<>(); 获取目标类名称 String clazzName = pjp.getTarget().getClass().getName(); 获取目标类方法名称 String methodName = pjp.getSignature().getName(); 请求的地址 String ip = IpUtils.getIpAddr(ServletUtils.getRequest()); String apiUrl = ServletUtils.getRequest().getRequestURI(); 关键核心代码 InputStream path = getClass().getResourceAsStream(/api.properties); BufferedReader reader = new BufferedReader(new InputStreamReader(path)); System.reader:" + reader); Properties pro = Properties(); pro.load(reader); System.pro: pro.getProperty(apiUrl)); data.put(apiUrl,apiUrl); IP地址 data.put(ip记录类名称 data.put(clazzName记录对应方法名称 data.put(methodName记录请求参数 data.put(params开始调用时间 计时并调用目标函数 long start = System.currentTimeMillis(); Object result = pjp.proceed(); Long time = System.currentTimeMillis() - start; 记录返回参数 data.put(result设置消耗总时间 data.put(consumeTimetry { System.日志输出: data); } catch (Exception e) { e.printStackTrace(); } result; } } api.properties配置文件如下: # 用户管理 /blog_user/list=get user manage /blog_user/list_test=用户管理测试 ? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |