2016-11-16 93 views
1

我已經打包使用SBT組裝我的春天啓動的應用程序,並嘗試運行jar當我收到與SBT春季啓動:無法啓動EmbeddedWebApplicationContext由於缺少EmbeddedServletContainerFactory

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組件創建)。所以,這給了我兩個選擇。將我的jar打包成sbt的嵌套jar或配置spring以使用正常的classloader並從Uber jar中加載。

我已經研究過嵌套jar sbt插件,我似乎無法找到任何維護的內容(甚至在Maven中心)。 Thisthis都過時了,而不是maven中心。有沒有人有這樣做的任何建議?

我也研究過配置spring引導來使用uber jar,而壓倒性的響應只是「使用maven插件」,這裏不適用。

回答

0

您需要在應用程序中提供嵌入式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-runner作爲庫編譯時的依賴。您的SBT構建文件也需要使用上面的類編譯時,跑步或包裝:

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

應用程序可以通過命令行與sbt run

運行