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的更多相关文章

  1. spring boot 国际化MessageSource

    转自:https://blog.csdn.net/flowingflying/article/details/76358970 spring中ResourceBundleMessageSource的配 ...

  2. 1.Spring Cloud初相识--------简单项目搭建

    开发工具:STS 代码下载链接:GitHub管理项目 前言: Springcloud 算是当前比较火的技术,一套微服务架构的技术. 我个人对微服务的理解为: 服务可以代表service,微服务就是小的 ...

  3. Spring boot 官网学习笔记 - logging

    commons-logging和slf4j是java中的日志门面,即它们提供了一套通用的接口,具体的实现可以由开发者自由选择.log4j和logback则是具体的日志实现方案. 比较常用的搭配是com ...

  4. 20191112 Spring Boot官方文档学习(4.4)

    4.4.日志 Spring Boot使用Commons Logging进行所有内部日志记录,但是使底层日志实现打开状态.为Java Util Logging,Log4J2和Logback提供了默认配置 ...

  5. Spring cloud gateway自定义filter以及负载均衡

    自定义全局filter package com.example.demo; import java.nio.charset.StandardCharsets; import org.apache.co ...

  6. Spring Boot从入门到精通(九)整合Spring Data JPA应用框架

    JPA是什么? JPA全称Java Persistence API,是Sun官方提出的Java持久化规范.是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中. ...

  7. Spring Boot 日志各种使用姿势,是时候捋清楚了!

    @ 目录 1. Java 日志概览 1.1 总体概览 1.2 日志级别 1.3 综合对比 1.4 最佳实践 2. Spring Boot 日志实现 2.1 Spring Boot 日志配置 2.2 L ...

  8. SpringBoot 使用yml配置 mybatis+pagehelper+druid+freemarker实例

    SpringBoot 使用yml配置 mybatis+pagehelper+druid+freemarker实例 这是一个简单的SpringBoot整合实例 这里是项目的结构目录 首先是pom.xml ...

  9. SpringBoot官方文档学习(三)配置文件、日志、国际化和JSON

    一.Profiles Spring配置文件提供了一种方法来隔离应用程序配置的各个部分,并使其仅在某些环境中可用.任何@Component.@Configuration或@ConfigurationPr ...

随机推荐

  1. matplotlib删除地图投影上的等值线及风场

    [前言]最近在编写一个气象应用程序,用来显示某一时刻某一地区的气温等值线和风场,程序主要用到了第三方库matplotlib及Basemap.在编写的过程中发现,如果不进行擦除操作直接绘制新的等值线或风 ...

  2. R2学习记录

    Setting up the Libevent library1.重写log行为 #include <event2/event.h> #include <stdio.h> st ...

  3. Java应用中使用ShutdownHook友好地清理现场

    在线上Java程序中经常遇到进程程挂掉,一些状态没有正确的保存下来,这时候就需要在JVM关掉的时候执行一些清理现场的代码.Java中得ShutdownHook提供了比较好的方案. JDK在1.3之后提 ...

  4. 洛谷3732:[HAOI2017]供给侧改革——题解

    https://www.luogu.org/problemnew/show/P3732 Anihc国提高社会生产力水平.落实好以人民为中心的发展思想.决定进行供给侧结构性改革. 为了提高供给品质.你调 ...

  5. 解决SurfaceView调用setZOrderOnTop(true)遮挡其他控件

    解决SurfaceView调用setZOrderOnTop(true)遮挡其他控件的问题 http://marller.blog.51cto.com/8699646/1762028 FAQ: Surf ...

  6. 解题:POI 2009 Ticket Inspector

    题面 看起来很水,然而不会DP的蒟蒻并不会做,PoPoqqq orz 设$f[i][j]$表示当前在第$i$个点和第$i+1$个点之间查票,已经查了$j$次的最大收益.然后就是那种很常见的枚举前一个结 ...

  7. Java虚拟机性能监控与调优

    1 基于JDK命令行工具的监控 1.1 JVM的参数类型 1.1.1 标准参数 在JVM的各个版本基本上保持不变,很稳定的. -help -server -client -version -showv ...

  8. laravel 数据库迁移转 sql 语句

    可以使用下面的命令 php artisan migrate --pretend --no-ansi 当然,你需要有可以 migrate 的东西. 数据库迁移导出到文件(使用命令) <?php n ...

  9. ACF/PACF,残差白噪声的检验问题

    关于自相关.偏自相关: 一.自协方差和自相关系数       p阶自回归AR(p)       自协方差 r(t,s)=E[X(t)-EX(t)][X(s)-EX(s)]       自相关系数ACF ...

  10. 读论文Machine Learning for Improved Diagnosis and Prognosis in Healthcare

    Deep Learning的基本思想 假设我们有一个系统S,它有n层(S1,…Sn),它的输入是I,输出是O,形象地表示为: I =>S1=>S2=>…..=>Sn => ...