美文网首页
spring-boot 是如何查找到 MainApplicati

spring-boot 是如何查找到 MainApplicati

作者: riag | 来源:发表于2018-12-27 16:47 被阅读0次

代码是在 SpringApplication.java 里的 deduceMainApplicationClass 函数来获取到 MainApplication class 的,
是通过遍历 StackTrace 来查找到的,代码如下

    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;
    }

相关文章

网友评论

      本文标题:spring-boot 是如何查找到 MainApplicati

      本文链接:https://www.haomeiwen.com/subject/uxcdlqtx.html