如何估算春季施工中所有施工后时间



我有一个Spring应用程序,我想了解所有postconstruct需要多长时间。

但是这个方法没有被调用,我错过了什么?

@Component
@Aspect
@Order(1)
public class InspectorTime {

@Around("@within(javax.annotation.PostConstruct)")
public void beforePostConstruct(ProceedingJoinPoint joinPoint) throws Throwable {
LOG.info("Before PostContract:{}", joinPoint.getSignature().getDeclaringType().toString());
StopWatch stopWatch = new StopWatch();
stopWatch.start();
joinPoint.proceed(joinPoint.getArgs());
stopWatch.stop();
LOG.info("After PostContract:{}", joinPoint.getSignature().getDeclaringType().toString() + ", total time" + stopWatch.toString());
}
}

我遵循了这些解决方案@Before @PostConstruct Spring AOP中断但他们也估计"自动线路";时机。我只需要估算后工程。

我也试过这个代码

@Around("@annotation(javax.annotation.PostConstruct)")
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
long start = System.currentTimeMillis();
Object proceed = joinPoint.proceed();
long executionTime = System.currentTimeMillis() - start;
logger.info("{}.{} executed in {} ms", joinPoint.getSignature().getDeclaringTypeName(),
joinPoint.getSignature().getName(), executionTime);
return proceed;
}

但是它使我的其他bean实例化失败:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.core.RedisTemplate]: Factory method 'redisTemplate' threw exception; nested exception is java.lang.NullPointerException

不要自己这样做,要么使用适当的分析器来获得结果,要么开始使用Spring提供开箱即用支持的[Java Flight Recorder]。

Spring将把启动指标记录为JFR事件(请参阅文档),您可以将这些记录到一个文件中,并使用Java任务控制来调查该文件。

Spring将记录容器和bean生命周期的不同部分的各种事件,请参阅文档中的列表。

要启用,您需要注册ApplicationStartup并将其传递给您正在使用的ApplicationContext。要记录到飞行记录仪,您需要创建FlightRecorderApplicationStartup

假设您正在使用Spring Boot,这很简单,在运行它之前将它传递给SpringApplication

@SpringBootApplication
public class MyApplication {
public static void main(String[] args) throws Exception {
var app = new SpringApplication(MyApplication.class);
app.setApplicationStartup(new FlightRecorderApplicationStartup());
app.run(args);
}
}

现在当你开始时,你需要启用飞行记录器和录音。

java -XX:+FlightRecorder -XX:StartFlightRecording=duration=30s,filename=myrecording.jfr -jar your-app.jar

这将记录30秒的事件,之后写入一个名为myrecording.jfr的文件,然后您可以在Java任务控制中打开该文件,并查看某些部分用于bean或所有bean等的时间。如果你的应用程序启动时间超过30秒,将duration参数增加到你需要的值。

如果这还不够,我强烈建议使用一个合适的分析器,而不是尝试用AOP来解决这个问题,因为当添加AOP时,它会影响启动应用程序和bean的时间(由于创建了额外的代理,或者您必须使用加载或编译时编织)。

最新更新