private Class<?> deduceMainApplicationClass() {
    try {
        StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
        for (StackTraceElement stackTraceElement : stackTrace) {
            if ("main".equals(stackTraceElement.getMethodName())) {
                return Class.forName(stackTraceElement.getClassName());
            }
        }
    }
    catch (ClassNotFoundException ex) {
        // Swallow and continue
    }
    return null;
}

这个是SpringApplication中获取main class的方法。一直觉得这种方式有点“挫”,_,没想到Spring也这么用,不知道还有没有其他更好的办法。

相关文章:

  • 2021-05-18
  • 2021-09-15
  • 2021-12-07
  • 2021-06-12
  • 2021-04-12
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-14
  • 2021-12-01
  • 2021-07-18
  • 2022-02-04
  • 2022-12-23
  • 2021-05-17
相关资源
相似解决方案