package com.pt.modules.log;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pt.modules.cfca.util.OutLogTextUtil;
import com.pt.modules.contract.rmtcontractlog.dto.RmtContractLogDTO;
import com.pt.modules.contract.rmtcontractlog.service.RmtContractLogService;
import com.pt.modules.contract.utils.Customannotations.OperationDescription; @Aspect
@Scope("prototype")
public class LoanLogAspect { private Logger logger = LoggerFactory.getLogger(LoanLogAspect.class); // 缓存有@OperationDescription方法参数名称
private static Map<String, String[]> parameterNameCaches = new ConcurrentHashMap<String, String[]>();
/**
* 接口请求记录实例
*/
@Autowired
@Qualifier("com.pt.modules.contract.rmtcontractlog.service.RmtContractLogService")
private RmtContractLogService rmtContractLogService; @Around("execution(* com.pt.modules.*.rest.*Rest*.*(..)) && @annotation(annotation)")
public Object advice(ProceedingJoinPoint joinPoint, OperationDescription annotation) throws Throwable{
String descption = annotation.description();
String entityType = annotation.entityType();
String reqContent = JSON.toJSONString(joinPoint.getArgs());
logger.info("\n\n"+OutLogTextUtil.outLogText("--接口名称----"+entityType+"-----操作动作:----"+descption+"---拦截器-接收到请求报文------------"+ reqContent));
Object result = joinPoint.proceed();
JSONObject response = JSON.parseObject(JSON.toJSONString(result));
response = response.getJSONObject("body");
String retcode = response.getString("retCode");
String errorDesc = response.getString("errorDesc");
RmtContractLogDTO dto = new RmtContractLogDTO();
dto.setErrMessage(errorDesc);
if(retcode!=""&&retcode!=null&&"302".equals(retcode)){
dto.setState("0");//失败状态
}else{
dto.setState("1");//成功状态
dto.setErrMessage(null);
}
this.saveRmtContractLog(dto, reqContent, "外部系统调用", entityType, descption,response.toString());
logger.info("\n\n"+OutLogTextUtil.outLogText("--接口名称----"+entityType+"-----操作动作:----"+descption+"---拦截器-返回接口报文------------"+ response.toString()));
return result;
} public void saveRmtContractLog(RmtContractLogDTO dto,String requestJson,String Systemsource ,
String interfaceName,String reqmapping,String responseJson) throws Exception{
dto.setSystemsource(Systemsource);
dto.setInterfaceName(interfaceName);
dto.setRequestJson(requestJson);
dto.setReqmapping(reqmapping);
dto.setResponseJson(responseJson);
rmtContractLogService.insertRmtContractLog(dto);
} }
package com.pt.modules.contract.utils.Customannotations;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface OperationDescription {
/**
* 方法描述
* @return
*/
public String description() default "no description"; /**
* 操作
* @return
*/
public String entityType() default "";
}
    <aop:aspectj-autoproxy proxy-target-class="true" />
<bean id="loanLogAspect" class="com.pt.modules.log.LoanLogAspect" />

java--Aop--记录日志的更多相关文章

  1. Java AOP (1) compile time weaving 【Java 切面编程 (1) 编译期织入】

    According to wikipedia  aspect-oriented programming (AOP) is a programming paradigm that aims to inc ...

  2. Java AOP (2) runtime weaving 【Java 切面编程 (2) 运行时织入】

    接上一篇 Java AOP (1) compile time weaving [Java 切面编程 (1) 编译期织入] Dynamic proxy   动态代理 Befor talking abou ...

  3. java AOP使用注解@annotation方式实践

       java AOP使用配置项来进行注入实践 AOP实际开发工作比较常用,在此使用注解方式加深对面向切面编程的理解 废话不多少,先看下面的实例代码 场景: 1.未满一岁的小孩,在执行方法之前打印:“ ...

  4. java AOP使用配置项来进行注入实践

    java AOP使用注解@annotation方式实践 场景: 在目标方法前面和后面执行通知方法 目标类 @Component public class Play { public void watc ...

  5. Java AOP的底层实现原理

    Java AOP的底层实现原理 一.什么是AOP 1.AOP:Aspect Oriented Programming(面向切面编程),OOP是面向对象编程,AOP是在OOP基础之上的一种更高级的设计思 ...

  6. Java AOP nested exception is java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice || Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0' 两个异常解决办法

    贴出applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...

  7. Java AOP - Aspectj

    1. 序 Aspect Oriented Programming (AOP)是近来一个比较热门的话题. AspectJ是AOP的Java语言的实现,获得了Java程序员的广泛关注. 关于AspectJ ...

  8. 在JAVA中记录日志的十个小建议

    JAVA日志管理既是一门科学,又是一门艺术.科学的部分是指了解写日志的工具以及其API,而选择日志的格式,消息的格式,日志记录的内容,哪种消息对应于哪一种日志级别,则完全是基于经验.从过去的实践证明, ...

  9. java aop 日志打印 正则设置

    package tz.lion.Utils.aop; import com.alibaba.fastjson.JSON;import org.springframework.web.multipart ...

  10. Java AOP总结

    AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...

随机推荐

  1. JavaScript--总结三(数组和函数)

    数组 数组的概念: 将多个元素(通常是同一类型)按照一定顺序排列放到一个集合中,这个集合称之为数组---简(一组有序的数据) 数组的作用:可以一次性存储多个数据 数组的定义: 1.通过构造函数创建数组 ...

  2. log4j2文件结构

    标签结构 Configuration properties Appenders Console PatternLayout File RollingRandomAccessFile Filters T ...

  3. Ubuntu上好用的截图工具——flameshot

    前言   堪称完美的截图工具--flameshot,windows上人们习惯性的使用QQ自带的截图工具Ctrl+Alt+A或者WeChat自带的截图工具Alt+A,若您是一位使用聊天工具截图多年的&q ...

  4. 大数相加Java

    题目 以字符串的形式读入两个数字,编写一个函数计算它们的和,以字符串形式返回. 分析 两个字符串,定义两个指针,分别从这两个字符串的结尾开始遍历,因为可能字符串1比字符串2长度要长,因此只要两者其中有 ...

  5. http-请求和响应报文的构成

    请求的构成: 1)请求方法URI协议/版本 2)请求头(Request Header) 3)请求正文 1)请求方法URI协议/版本 Request URL: http://localhost:8080 ...

  6. Python pip install 默认路径修改。

    pip动不动就下载数百M的文件.这些文件默认在C:盘,那么为了节省空间需要修改这些路径: 打开cmd命令窗口.输入: python -m site C:\Users\hewei>python - ...

  7. 【MYSQL】MySQL5.6.37二进制安装

    最近有个项目要用到mysql 于是在mysql的论坛中找到了一个5.6.37版本的 下面介绍怎么安装和使用mysql 下载地址: https://dev.mysql.com/downloads/mys ...

  8. postgresql插件安装

    postgresql安装包自带插件安装: 1.编译安装插件 # root用户 # postgresql安装过程省略 # 进入解压包的contrib目录 cd postgresql-10.6/contr ...

  9. 浅谈JavaScript代码性能优化

    可以通过https://jsbench.me/测试网站完成性能测试. 一.慎用全局变量 1.全局变量定义在全局执行上下文,是所有作用域链的顶端,在局部作用域中没找到的变量都会到全局变量中去查找,所以说 ...

  10. luoguP2016 战略游戏

    题目描述 Bob喜欢玩电脑游戏,特别是战略游戏.但是他经常无法找到快速玩过游戏的办法.现在他有个问题.他要建立一个古城堡,城堡中的路形成一棵树.他要在这棵树的结点上放置最少数目的士兵,使得这些士兵能了 ...