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 ...
随机推荐
- 【刷题】BZOJ 1934 [Shoi2007]Vote 善意的投票
Description 幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉.对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神.虽然每个人都有自己的主见,但是为了照顾一下自己朋友的想法,他们也可 ...
- Ants on tree
Description 从前有一个策略游戏, 叫做 蚂蚁上树 游戏中有一棵 nn 个节点, 以 11 为根的有根树 初始始每个节点都为空, 游戏系统会进行两种操作 : 1 x , 表示往 xx 节点放 ...
- APT攻击基础科普
0x00 APT的历史起源背景 APT这个词汇最早起源于:2005年英国和美国的CERT组织发布了关于有针对性的社交工程电子邮件,放弃特洛伊木马以泄露敏感信息的第一个警告,尽管没有使用“APT”这个名 ...
- JS的类型和值
1.类型 ECMAScript语言中所有的值都有一个对应的语言类型.ECMAScript语言类型包括Undefined.Null.Boolean.String.Number和Object. 对语言引擎 ...
- Android热点回顾第六期
Android热点回顾第五期 http://www.importnew.com/9274.html Android热点回顾第四期http://www.importnew.com/8997.html A ...
- golang package log
package main import ( "log" "os" ) var logger *log.Logger func main() { file, er ...
- 修改const保护的值
先看代码: #include <stdio.h> void main() { const int num = 10; int *a = (int *)(void *)# / ...
- micro 架构组件介绍
Micro 是什么 Micro 是微服务生态系统中专注于提供产品.服务和解决方案的一款软件,使得在现代软件企业中起到创新驱动.它计划是可以将任何微服务相关的资源与即将使用此款软件的公司结合起来,利用此 ...
- Spring整合Quartz定时任务 在集群、分布式系统中的应用
概述 虽然单个Quartz实例能给予你很好的Job调度能力,但它不能满足典型的企业需求,如可伸缩性.高可靠性满足.假如你需要故障转移的能力并能运行日益增多的 Job,Quartz集群势必成为你应用的一 ...
- 2017 清北济南考前刷题Day 1 afternoon
期望得分:80+30+70=180 实际得分:10+30+70=110 T1 水题(water) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK出了道水 ...