gpt4 book ai didi

带有 sbt : Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory 的 Spring 启动

转载 作者:行者123 更新时间:2023-12-05 07:46:36 66 4
gpt4 key购买 nike

我已经使用 sbt assembly 打包了我的 spring boot 应用程序,并在尝试运行我收到的 jar 时

Application startup failed
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is
org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.

该项目在 Intellij 中运行良好。我的 build.sbt 在下面

lazy val news = (project in file("wherever")).
enablePlugins(DockerPlugin).
settings(commonSettings: _*).
settings(
name := "name",
mainClass in assembly := Some("mainEntry"),
test in assembly := {},
assemblyJarName := "jarName",
libraryDependencies ++= dependency1,
libraryDependencies ++= dependency2,
libraryDependencies += ScalaTest,
scalaSource in Compile := baseDirectory.value / "src/main/scala",
dockerfile in docker := {
val artifact: File = assembly.value
val artifactTargetPath = "/"
new Dockerfile {
from("openjdk:8-jre-alpine")
add(artifact, artifactTargetPath)
add(new File("./config/"),"/config")
cmd("java", "-jar", " -Denv=$env","jarName")
env("env","stage")
}
},
imageNames in docker := Seq(
ImageName("imageName")
)
)

我已经做了一些挖掘,它看起来像 Spring Boot requires the jar to contain nested jars (而不是像使用 sbt 程序集创建的 Uber jar)。所以,这给了我两个选择。使用 sbt 将我的 jar 打包为嵌套 jar,或者将 spring 配置为使用普通类加载器并从 Uber jar 加载。

我查看了嵌套的 jar sbt 插件,但我似乎找不到任何维护的东西(甚至在 Maven Central 中)。 Thisthis都已经过时了,而且不在 Maven Central 中。有人对此有任何建议吗?

我也研究过配置 spring boot 以使用 uber jar,压倒性的 react 只是“使用 maven 插件”,这在这里不适用。

最佳答案

您需要在应用程序上提供嵌入式 servlet 容器工厂。它应该类似于以下内容:

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration
import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory
import org.springframework.boot.context.web.SpringBootServletInitializer
import org.springframework.context.annotation.{Bean, ComponentScan, Configuration, PropertySource}

/**
* Spring boot application configuration and servlet initializer.
*/
@Configuration
@ComponentScan(value = Array("com.foobusiness.foopackage"))
@PropertySource(Array("classpath:application.properties"))
@SpringBootApplication(exclude = Array(classOf[ErrorMvcAutoConfiguration]))
class Application extends SpringBootServletInitializer {
@Bean
def servletContainer: JettyEmbeddedServletContainerFactory = new JettyEmbeddedServletContainerFactory()
}

object Application {
def main(args: Array[String]): Unit = {
SpringApplication run classOf[Application]
}
}

上面显然使用了 Jetty,这意味着您还需要将 jetty-runner 作为库编译时依赖项包含在内。你的sbt构建文件在编译、运行或者打包的时候也需要使用上面的类:

mainClass in(Compile, run, packageBin) := Some("com.foobusiness.foopackage.Application"),

可以使用 sbt run 从命令行运行应用程序

关于带有 sbt : Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory 的 Spring 启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40622142/

66 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com