【问题标题】:Spring application with embedded servlet container run error带有嵌入式 servlet 容器的 Spring 应用程序运行错误
【发布时间】:2017-01-20 08:02:00
【问题描述】:

我正在学习 Spring,但遇到了问题。我创建了在嵌入式 servlet 容器中运行的独立应用程序(Web 服务)。我将 Intellij IDEA 与 gradle 一起使用。当我从 IDEA 运行应用程序时 - 一切正常。但是当我创建了 jar 文件并通过控制台运行时(“java -jar my.jar”),我遇到了错误(如下)。 我正在加载官方示例(https://github.com/spring-guides/gs-rest-service),但它也不起作用(通过运行 jar)。 我看到了这个链接,但它没有帮助我:Spring Boot: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean

请帮忙

错误日志:

18:47:26.079 [main] ERROR o.s.boot.SpringApplication - Application startup faile
d org.springframework.context.ApplicationContextException: Unable to start embedde
d container; nested exception is org.springframework.context.ApplicationContextE
xception: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedS
ervletContainerFactory bean.
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationConte
xt.onRefresh(EmbeddedWebApplicationContext.java:135) ~[ClientsIm.jar:na]
        at org.springframework.context.support.AbstractApplicationContext.refres
h(AbstractApplicationContext.java:476) ~[ClientsIm.jar:na]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationConte
xt.refresh(EmbeddedWebApplicationContext.java:120) ~[ClientsIm.jar:na]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.
java:691) [ClientsIm.jar:na]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java
:320) [ClientsIm.jar:na]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java
:952) [ClientsIm.jar:na]
        at com.clientsim.server.Application.main(Application.java:13) [ClientsIm
.jar:na]
Caused by: org.springframework.context.ApplicationContextException: Unable to st
art EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory
 bean.

我的 build.gradle:

group 'ClientsIm'
version '1.0-SNAPSHOT'

buildscript {
    repositories {
        maven { url "http://repo.spring.io/libs-release" }
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'

sourceCompatibility = 1.5

repositories {
    maven { url "http://repo.spring.io/libs-release" }
    mavenLocal()
    mavenCentral()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

【问题讨论】:

  • 使用 Gradle 构建 jar 时正在运行什么命令?

标签: java spring web-services spring-boot


【解决方案1】:

有什么告诉我你没有使用 gradle 创建你的 jar 吗?也许您是从 intellij 导出的?

(1) 使用 gradle 构建:

您应该使用 gradle 构建您的 jar(使用 spring-boot-gradle-plugin 创建 jar)。在项目的根目录下打开一个终端并输入:

$ gradle build
  ...

$ cd build/libs
$ java -jar yourjar.jar

(2) 从 Intellij 导出 spring-boot jar:

【讨论】:

  • 非常感谢!我已经使用 gradle build 和 jar 工作正常。
猜你喜欢
  • 2018-06-13
  • 1970-01-01
  • 2017-10-21
  • 2015-07-31
  • 2017-05-28
  • 2017-10-27
  • 2013-01-11
  • 2012-09-06
  • 2018-03-18
相关资源
最近更新 更多