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 ...
随机推荐
- ReentrantLock详解 以及与synchronized的区别
ReentrantLock lock = new ReentrantLock(); //参数默认false,不公平锁 ReentrantLock lock = new ReentrantLock(tr ...
- Gauss Prime UVA - 1415
题意:给出a和b判定是否为高斯素数 解析: 普通的高斯整数i = sqrt(-1) 高斯整数是素数当且仅当: a.b中有一个是零,另一个是形为或其相反数的素数: 或a.b均不为零,而为素数. 这题 提 ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence(线段树)
D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input st ...
- 【原创】centos6创建sftp账号,并设置权限和目录
网上找了个教程,折腾好长时间都不行,最后往死里整,终于弄好了,记录一下. 系统环境:Centos6.9 64bit 完美解决: Permission denied (publickey,gssapi- ...
- STL 算法中函数对象和谓词
STL 算法中函数对象和谓词 函数对象和谓词定义 函数对象: 重载函数调用操作符的类,其对象常称为函数对象(function object),即它们是行为类似函数的对象.一个类对象,表现出一个函数的特 ...
- hihocoder1711 评论框排版[并查集+set]
#include <cstdio> #include <iostream> #include <set> using namespace std; ; struct ...
- 【BZOJ1914】数三角形(组合数,极角排序)
[BZOJ1914]数三角形(组合数,极角排序) 题面 BZOJ权限题 良心洛谷 题解 这种姿势很吼啊,表示计算几何啥的一窍不通来着. 题目就是这样,正难则反,所以我们不考虑过原点的三角形, 反过来, ...
- BZOJ3601 一个人的数论 【数论 + 高斯消元】
题目链接 BZOJ3601 题解 挺神的 首先有 \[ \begin{aligned} f(n) &= \sum\limits_{x = 1}^{n} x^{d} [(x,n) = 1] \\ ...
- 【bzoj3209】 花神的数论题
http://www.lydsy.com/JudgeOnline/problem.php?id=3209 (题目链接) 题意 ${sum(i)}$表示${i}$的二进制表示中${1}$的个数.求${\ ...
- 【uoj129】 NOI2015—寿司晚宴
http://uoj.ac/problem/129 (题目链接) 题意 给出2~n这n-1个数,求选2个集合,使得从两集合中任意各选取1个数出来它们都互质.求方案数. Solution PoPoQQQ ...