首页 > 解决方案 > 我的 Spring Boot 应用程序在加载后立即退出且没有错误

问题描述

我正在将 maven spring boot 应用程序转换为基于 bazel 的应用程序。我终于让它正确编译了,但是一旦我运行它,它就退出了。服务器没有启动,但它会打印 sprint boot 启动消息。

我认为这与spring无法找到servlet有关,但我对java很陌生,所以我不知道去哪里找。

我无法从 JVM 获得任何关于它为什么退出的可用信息。有没有办法可以增加 spring 的日志记录详细程度?

这是我的 Application.java

package com.example.abc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AbcClient {

    public static void main(String[] args) {
      SpringApplication.run(AbcClient.class, args);
    }
}

我的工作空间文件

maven_jar(
    name = "org_springframework_spring_core",
    artifact = "org.springframework:spring-core:jar:5.1.1.RELEASE"
)

maven_jar(
    name = "org_springframework_spring_beans",
    artifact = "org.springframework:spring-beans:jar:5.1.1.RELEASE"
)

maven_jar(
    name = "org_springframework_spring_context",
    artifact = "org.springframework:spring-context:jar:5.1.1.RELEASE"
)

maven_jar(
    name = "org_springframework_spring_aop",
    artifact = "org.springframework:spring-aop:jar:5.1.1.RELEASE"
)

maven_jar(
    name = "org_springframework_spring_expression",
    artifact = "org.springframework:spring-expression:jar:5.1.1.RELEASE"
)

maven_jar(
    name = "org_springframework_boot_spring_boot",
    artifact = "org.springframework.boot:spring-boot:jar:2.0.6.RELEASE"
)

maven_jar(
    name = "org_springframework_boot_spring_boot_autoconfigure",
    artifact = "org.springframework.boot:spring-boot-autoconfigure:jar:2.0.6.RELEASE"
)

maven_jar(
    name = "org_springframework_boot_spring_boot_starter_aop",
    artifact = "org.springframework.boot:spring-boot-starter-aop:jar:2.0.6.RELEASE"
)

maven_jar(
    name = "org_springframework_boot_spring_boot_starter_web",
    artifact = "org.springframework.boot:spring-boot-starter-web:jar:2.0.6.RELEASE"
)

maven_jar(
    name = "org_springframework_boot_spring_boot_starter_test",
    artifact = "org.springframework.boot:spring-boot-starter-test:jar:2.0.6.RELEASE"
)

maven_jar(
    name = "org_apache_tomcat_embed_tomcat_embed_core",
    artifact = "org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.12"
)

maven_jar(
    name = "org_apache_tomcat_embed_tomcat_embed_jasper",
    artifact = "org.apache.tomcat.embed:tomcat-embed-jasper:jar:9.0.12"
)

maven_jar(
    name = "org_springframework_boot",
    artifact = "org.springframework.boot:spring-boot-starter-tomcat:jar:2.0.6.RELEASE"
)

maven_jar(
    name = "javax_servlet_jstl",
    artifact = "javax.servlet:jstl:jar:1.2"
)

maven_jar(
    name = "javax_servlet_javax_servlet_api",
    artifact = "javax.servlet:javax.servlet-api:jar:4.0.1"
)

maven_jar(
    name = "commons_logging_commons_logging",
    artifact = "commons-logging:commons-logging:jar:1.2"
)

maven_jar(
    name = "javax_servlet_jsp_javax_servlet_jsp_api",
    artifact = "javax.servlet.jsp:javax.servlet.jsp-api:jar:2.3.3"
)

和我的构建文件

java_binary(
    name = "AbcClient",
    srcs = glob(["src/main/java/com/example/abc/*.java"]),
    deps = [
        "@org_springframework_spring_core//jar",
        "@org_springframework_spring_beans//jar",
        "@org_springframework_spring_aop//jar",
        "@org_springframework_spring_expression//jar",
        "@org_springframework_boot_spring_boot//jar",
        "@org_springframework_boot_spring_boot_autoconfigure//jar",
        "@org_springframework_spring_context//jar",
        "@org_springframework_boot_spring_boot_starter_aop//jar",
        "@org_springframework_boot_spring_boot_starter_web//jar",
        "@org_apache_tomcat_embed_tomcat_embed_core//jar",
        "@org_apache_tomcat_embed_tomcat_embed_jasper//jar",
        "@javax_servlet_jstl//jar",
        "@javax_servlet_javax_servlet_api//jar",
        "@javax_servlet_jsp_javax_servlet_jsp_api//jar",
        "@commons_logging_commons_logging//jar",
    ],
    resources = glob([
        "src/main/java/resources/*",
        "src/main/java/webapp/resources/**"
    ])
)

标签: javaspringspring-bootbazel

解决方案


spring-boot-starter-web依赖就足够了,默认情况下包括Tomcat。运行应用程序时,您可能会丢失依赖项,例如查看它SpringBootServletInitializer是否存在并正在运行。

查看bazel-springboot-rule项目和springboot.bzl Packager,它们使用 Bazel 将 Spring Boot 应用程序打包为可运行的 JAR(与 Maven 和 Gradle 类似)。或多或少:

load("//tools/springboot:springboot.bzl",
    "springboot",
    "add_boot_web_starter"
)

add_boot_web_starter(app_deps)

springboot(
    name = "spring-boot-sample",
    boot_app_class = "com.main.Application",
    deps = app_deps
)

推荐阅读