trace spring
package xx.com.aspect; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;
import java.util.Date;
import java.util.Vector; @Aspect
@Configuration
public class performace { @Autowired
HttpServletRequest request; static final ThreadLocal<TraceInfo> localRequest=new ThreadLocal<>();
static final ThreadLocal<Boolean> localIsChild=new ThreadLocal<>(); public static TraceInfo getLocalRequest() {
return localRequest.get();
}
public static void setParentRequest(TraceInfo traceInfo) {
localRequest.set(traceInfo);
localIsChild.set(true);
} // @Around("execution(* xx.com..*(..))")
// @Around("!within(is(FinalType)) && execution(* *..*(..))")
public Object aspectAround(ProceedingJoinPoint point) throws Throwable { String methodName = point.getSignature().getName();
String type=point.getSignature().getDeclaringType().getName()+":"+methodName;
TraceInfo traceInfo=new TraceInfo(type,new Date());
traceInfo.setChilds(new Vector<>());
if(localIsChild.get()!=null&&localIsChild.get()){
traceInfo.setChildThread(true);
}
boolean isMain=false;
if(localRequest.get()==null) {
isMain=true;
localRequest.set(traceInfo);
}
Object ret=point.proceed();
traceInfo.setEndDate(new Date()); if(isMain) {
try {
request.getAttribute("a");
} catch (Exception e) {
localRequest.remove();
System.out.println("*** 非请求执行:" + traceInfo.getType() + " : " + String.valueOf(traceInfo.getEndDate().getTime() - traceInfo.getStartDate().getTime()));
return ret;
}
} if(!isMain){
localRequest.get().getChilds().add(traceInfo);
} for (Annotation an : point.getTarget().getClass().getAnnotations()) {
if (an instanceof RestController) {
//end controller
trace(localRequest.get(), 0);
localRequest.remove();
break;
}
} // traces=new Vector<>();
// request.setAttribute("traces", traces);
return ret; } private void trace(TraceInfo traceInfo,int deep){ char[] space=new char[deep];
for(int i=0;i<deep;i++){
if(i==deep-1){
if(traceInfo.isChildThread()){
space[i] = '→';
}else {
space[i] = '┞';
}
break;
}
space[i]=' ';
}
System.out.println(
new String(space)+
traceInfo.getType()+
" : "+
String.valueOf(traceInfo.getEndDate().getTime()-traceInfo.getStartDate().getTime()));
if(traceInfo.getChilds()==null){
return;
}
for(TraceInfo child:traceInfo.getChilds()){
trace(child,deep+1);
} } public static class TraceInfo{ private Date startDate;
private Date endDate;
private String type;
private Vector<TraceInfo> childs;
private boolean childThread=false; public TraceInfo(String type,Date time){
this.startDate=time;
this.type=type;
} public Vector<TraceInfo> getChilds() {
return childs;
} public void setChilds(Vector<TraceInfo> childs) {
this.childs = childs;
} public Date getEndDate() {
return endDate;
} public void setEndDate(Date endDate) {
this.endDate = endDate;
} public Date getStartDate() {
return startDate;
} public void setStartDate(Date startDate) {
this.startDate = startDate;
} public String getType() {
return type;
} public void setType(String type) {
this.type = type;
} public boolean isChildThread() {
return childThread;
} public void setChildThread(boolean childThread) {
this.childThread = childThread;
}
} }
trace spring的更多相关文章
- spring boot 国际化MessageSource
转自:https://blog.csdn.net/flowingflying/article/details/76358970 spring中ResourceBundleMessageSource的配 ...
- 1.Spring Cloud初相识--------简单项目搭建
开发工具:STS 代码下载链接:GitHub管理项目 前言: Springcloud 算是当前比较火的技术,一套微服务架构的技术. 我个人对微服务的理解为: 服务可以代表service,微服务就是小的 ...
- Spring boot 官网学习笔记 - logging
commons-logging和slf4j是java中的日志门面,即它们提供了一套通用的接口,具体的实现可以由开发者自由选择.log4j和logback则是具体的日志实现方案. 比较常用的搭配是com ...
- 20191112 Spring Boot官方文档学习(4.4)
4.4.日志 Spring Boot使用Commons Logging进行所有内部日志记录,但是使底层日志实现打开状态.为Java Util Logging,Log4J2和Logback提供了默认配置 ...
- Spring cloud gateway自定义filter以及负载均衡
自定义全局filter package com.example.demo; import java.nio.charset.StandardCharsets; import org.apache.co ...
- Spring Boot从入门到精通(九)整合Spring Data JPA应用框架
JPA是什么? JPA全称Java Persistence API,是Sun官方提出的Java持久化规范.是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中. ...
- Spring Boot 日志各种使用姿势,是时候捋清楚了!
@ 目录 1. Java 日志概览 1.1 总体概览 1.2 日志级别 1.3 综合对比 1.4 最佳实践 2. Spring Boot 日志实现 2.1 Spring Boot 日志配置 2.2 L ...
- SpringBoot 使用yml配置 mybatis+pagehelper+druid+freemarker实例
SpringBoot 使用yml配置 mybatis+pagehelper+druid+freemarker实例 这是一个简单的SpringBoot整合实例 这里是项目的结构目录 首先是pom.xml ...
- SpringBoot官方文档学习(三)配置文件、日志、国际化和JSON
一.Profiles Spring配置文件提供了一种方法来隔离应用程序配置的各个部分,并使其仅在某些环境中可用.任何@Component.@Configuration或@ConfigurationPr ...
随机推荐
- 一些说明&其他奇奇怪怪的东西
NOIP考完了,这篇博客彻底咕了.
- 开源入侵检测系统SELKS系统搭建
一.系统环境配置 系统环境:centos7x64 ip地址:172.16.91.130 1.设置静态IP地址 [root@localhost backlion]#vi /etc/sys ...
- [USACO10OPEN]牛跳房子Cow Hopscotch
题目描述 奶牛们正在回味童年,玩一个类似跳格子的游戏,在这个游戏里,奶 牛们在草地上画了一行N个格子,(3 <=N <= 250,000),编号为1..N. 就像任何一个好游戏一样,这样的 ...
- 【OpenCV】角点检测:Harris角点及Shi-Tomasi角点检测
角点 特征检测与匹配是Computer Vision 应用总重要的一部分,这需要寻找图像之间的特征建立对应关系.点,也就是图像中的特殊位置,是很常用的一类特征,点的局部特征也可以叫做“关键特征点”(k ...
- chrome神插件之:SwitchyOmega的安装设置
转至:http://yiweifen.com/v-1-118586.html 前言 往常找某草的网站基本上是这样的:先FQ,然后谷歌找1024,出现很多某草的更新地址,大多都是广告,大约需要花十几分钟 ...
- R语言:克里金插值
基于空间自相关,R语言克里金插值 library(gstat) Warning message: In scan(file = file, what = what, sep = sep, quote ...
- P3866 [TJOI2009]战争游戏
P3866 [TJOI2009]战争游戏 题目背景 小R正在玩一个战争游戏.游戏地图是一个M行N列的矩阵,每个格子可能是障碍物,也可能是空地,在游戏开始时有若干支敌军分散在不同的空地格子中.每支敌军都 ...
- day21 数据库(DataBase)
1.数据库由多张表组成,一张表就是一个实体. 2.表的列就是属性的值,行就是一个个具体的对象的属性值. primary key主键:1.非空.2.不能修改(定好不变).3.业务无关. 作用:在表中具体 ...
- noi题库(noi.openjudge.cn) 3.9数据结构之C++STL T1——T2
T1 1806:词典 描述 你旅游到了一个国外的城市.那里的人们说的外国语言你不能理解.不过幸运的是,你有一本词典可以帮助你. 输入首先输入一个词典,词典中包含不超过100000个词条,每个词条占据一 ...
- Spring Enable*高级应用及原理
Enable* 之前的文章用到了一些Enable*开头的注解,比如EnableAsync.EnableScheduling.EnableAspectJAutoProxy.EnableCaching等, ...