java--Aop--记录日志
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--记录日志的更多相关文章
- Java AOP (1) compile time weaving 【Java 切面编程 (1) 编译期织入】
According to wikipedia aspect-oriented programming (AOP) is a programming paradigm that aims to inc ...
- Java AOP (2) runtime weaving 【Java 切面编程 (2) 运行时织入】
接上一篇 Java AOP (1) compile time weaving [Java 切面编程 (1) 编译期织入] Dynamic proxy 动态代理 Befor talking abou ...
- java AOP使用注解@annotation方式实践
java AOP使用配置项来进行注入实践 AOP实际开发工作比较常用,在此使用注解方式加深对面向切面编程的理解 废话不多少,先看下面的实例代码 场景: 1.未满一岁的小孩,在执行方法之前打印:“ ...
- java AOP使用配置项来进行注入实践
java AOP使用注解@annotation方式实践 场景: 在目标方法前面和后面执行通知方法 目标类 @Component public class Play { public void watc ...
- Java AOP的底层实现原理
Java AOP的底层实现原理 一.什么是AOP 1.AOP:Aspect Oriented Programming(面向切面编程),OOP是面向对象编程,AOP是在OOP基础之上的一种更高级的设计思 ...
- 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 ...
- Java AOP - Aspectj
1. 序 Aspect Oriented Programming (AOP)是近来一个比较热门的话题. AspectJ是AOP的Java语言的实现,获得了Java程序员的广泛关注. 关于AspectJ ...
- 在JAVA中记录日志的十个小建议
JAVA日志管理既是一门科学,又是一门艺术.科学的部分是指了解写日志的工具以及其API,而选择日志的格式,消息的格式,日志记录的内容,哪种消息对应于哪一种日志级别,则完全是基于经验.从过去的实践证明, ...
- java aop 日志打印 正则设置
package tz.lion.Utils.aop; import com.alibaba.fastjson.JSON;import org.springframework.web.multipart ...
- Java AOP总结
AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...
随机推荐
- RewriteCond 和RewriteRule规则说明 (转)
Apache的Mod_rewrite学习 (RewriteCond重写规则的条件)收藏RewriteCond Syntax: RewriteCond TestString CondPattern [f ...
- 写给小白看的Mysql事务
1 为什么需要事务 在网上的很多资料里,其实没有很好的解释为什么我们需要事务.其实我们去学习一个东西之前,还是应该了解清楚这个东西为什么有用,硬生生的去记住事务的ACID特性.各种隔离级别个人认为没有 ...
- 如何使用 VS Code开发.NET Core应用程序
Visual Studio Code(VS Code)是Microsoft为Windows,Linux和Mac操作系统开发的免费,跨平台,轻量级的源代码编辑器,它是源代码编辑器,而Visual Stu ...
- PAT甲级 1155 Heap Paths (30分) 堆模拟
题意分析: 给出一个1000以内的整数N,以及N个整数,并且这N个数是按照完全二叉树的层序遍历输出的序列,输出所有的整条的先序遍历的序列(根 右 左),以及判断整棵树是否是符合堆排序的规则(判断是大顶 ...
- C语言指针-从底层原理到花式技巧,用图文和代码帮你讲解透彻
这是道哥的第014篇原创 目录 一.前言 二.变量与指针的本质 1. 内存地址 2. 32位与64位系统 3. 变量 4. 指针变量 5. 操作指针变量 5.1 指针变量自身的值 5.2 获取指针变量 ...
- 虚拟机linux共享文件夹
linux共享文件夹问题 1. 初配置 2. 挂载 018.7.8 为了实现win7与VMware中linux文件夹共享很花费了一些时间,但终成正果 1. 初配置 虚拟机设置->选项->共 ...
- 日常采坑:.NET Core SDK版本问题
1..NetCore SDK版本问题 .NetCore3.1 webapi 部署linux,遇到一个坑,开启的目录浏览功能失效,几番尝试发现是版本问题.本地sdk版本与linux安装的sdk版本不对应 ...
- 【Spring】Spring中的Bean - 4、Bean的生命周期
Bean的生命周期 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 了解Spring中Bean的生命周期有何意义? 了解Sp ...
- 【ASM】从asm中复制文件到本地,或者从本地到asm中方法
工作中,有时需要把文件从ASM中复制到文件系统中或者反过来,做一些维护操作,本文介绍了4种复制文件的的方法: ASMCMD中的cp命令(11g) dbms_file_transfer包 rman的co ...
- Api文档自动生成工具
java开发,根据代码自动生成api接口文档工具,支持RESTful风格,今天我们来学一下api-doc的生成 作者:互联网编程. 欢迎投稿,一起交流技术 https://www.jianshu.co ...