Java Spring Boot 应用程序在启动时终止
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40205600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot App Terminating at Startup
提问by Umar
I am trying out a simple spring boot application it always shuts down automatically
我正在尝试一个简单的弹簧启动应用程序,它总是自动关闭
:: Spring Boot :: (v1.4.1.RELEASE)
2016-10-23 13:05:21.681 INFO 16532 --- [ main] com.example.RestBootApplication : No active profile set, falling back to default profiles: default
2016-10-23 13:05:21.766 INFO 16532 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6e20b53a: startup date [Sun Oct 23 13:05:21 EDT 2016]; root of context hierarchy
2016-10-23 13:05:23.682 INFO 16532 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-10-23 13:05:23.704 INFO 16532 --- [ main] com.example.RestBootApplication : Started RestBootApplication in 2.632 seconds (JVM running for 5.168)
2016-10-23 13:05:23.705 INFO 16532 --- [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@6e20b53a: startup date [Sun Oct 23 13:05:21 EDT 2016]; root of context hierarchy
2016-10-23 13:05:23.708 INFO 16532 --- [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
pom.xml
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>rest-boot</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Main Class
主类
@SpringBootApplication
public class RestBootApplication {
public static void main(String[] args) {
SpringApplication.run(RestBootApplication.class, args);
}
}
Controller
控制器
@Controller
public class HelloController {
@RequestMapping("/hello")
String helloWorld(){
return "helloWorld";
}
}
Trying to run in spring tool suite. it always stops after starting. I even added "spring-boot-starter-web" after looking at some stackoverflow questions, but still facing the issue.
试图在 spring 工具套件中运行。它总是在启动后停止。在查看了一些 stackoverflow 问题后,我什至添加了“spring-boot-starter-web”,但仍然面临这个问题。
Please can you someone point out the issue.
请你有人指出这个问题。
回答by Achille_vanhoutte
My suggestion is to remove this dependency
我的建议是删除这种依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
You may also try to add this tomcat dependency:
您也可以尝试添加此 tomcat 依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
回答by Michael Lihs
I just took your code and started a Spring-Boot application from scratch. On my machine, your code starts up as expected when I run
我只是拿了你的代码并从头开始启动了一个 Spring-Boot 应用程序。在我的机器上,您的代码在我运行时按预期启动
mvn spring-boot:run
To make sure, we have the very same code, I uploaded my solution to Github, see https://github.com/michaellihs/stackoverflow-40205600
为了确保我们有相同的代码,我将我的解决方案上传到 Github,请参阅https://github.com/michaellihs/stackoverflow-40205600
Just an idea why it doesn't work for you: do have another Tomcat instance running that is listening on port 8080 - this will stop your application immediately (but normally shows a different log / error message).
只是想知道为什么它对您不起作用:确实有另一个正在运行的 Tomcat 实例正在侦听端口 8080 - 这将立即停止您的应用程序(但通常会显示不同的日志/错误消息)。
回答by Praveen Kumar K S
Solution to your Question
解决您的问题
- I reviewed your POM.xml, it looks fine and doesn't require any changes to it.
spring-boot-starter-web
is an opinionated dependency, it automatically pulls your other dependencies. Please check whether it has pulled your embedded Tomcat in your Maven Dependencies as shown below.
- 我查看了您的 POM.xml,它看起来不错,不需要对其进行任何更改。
spring-boot-starter-web
是一个自以为是的依赖项,它会自动拉取您的其他依赖项。请检查它是否已将您的嵌入式 Tomcat 拉入您的 Maven 依赖项中,如下所示。
- Go to application.properties file or application.yml to change the server port
server.port=9081
and run the maven goal asmvn clean install spring-boot:run -e
If these options doesn't workout please try this approach postedby me.
- 转到 application.properties 文件或 application.yml 更改服务器端口
server.port=9081
并运行 maven 目标,mvn clean install spring-boot:run -e
如果这些选项不起作用,请尝试我发布的这种方法。
回答by varsha tj
I deleted the maven dependencies folder and it worked for me
我删除了 Maven 依赖项文件夹,它对我有用
回答by hlayachi
I had the same problem and there is how I resolved it :
我遇到了同样的问题,我是如何解决它的:
- I deleted maven's local repository
- run again maven install
- run the app as spring boot app
- 我删除了 maven 的本地存储库
- 再次运行 maven install
- 将应用程序作为 Spring Boot 应用程序运行
====> Hallelujah, and it worked just by magic !
====>哈利路亚,它只是靠魔法!
回答by AK47
In my case as well , I had to clear the complete .m2 directory and run clean install to make it to work. Not sure what is conflicting.
就我而言,我也必须清除完整的 .m2 目录并运行全新安装才能使其正常工作。不知道有什么冲突。
回答by Darkman
This was happening to me and it turned out to be a corrupted maven apache repository.
这发生在我身上,结果证明是一个损坏的 maven apache 存储库。
To fix it I removed the apache repo - on my Mac it was located at /Users/myname/.m2/repository/org/apache.
为了修复它,我删除了 apache 存储库 - 在我的 Mac 上它位于 /Users/myname/.m2/repository/org/apache。
On a pc it should be c:\users\myname.m2\repository\org\apache.
在 PC 上,它应该是 c:\users\myname.m2\repository\org\apache。
I then ran Maven - Update Project and then I ran my class and it was fixed.
然后我运行了 Maven - Update Project,然后我运行了我的课程并且它被修复了。
回答by Maria
First of all check if the port 8080 is available or is being used by some other process.
首先检查端口 8080 是否可用或正在被其他进程使用。
If this port is not available, try adding server.port=someAvailablePortNumber
in the application.properties
file located in "resources" folder.
如果该端口不可用,尝试添加server.port=someAvailablePortNumber
了在application.properties
位于“资源”文件夹中的文件。
I was also facing same problem. Tried a lot of changes suggested in pom.xml file and also tried multiple suggestion related to maven (e.g: deleting folder, updating project etc) but nothing worked for me. In my case the port 8080 wasn't available so application wasn't able to start tomcat using default port(i.e.: 8080) causing it to shutdown immediately.
我也面临同样的问题。尝试了 pom.xml 文件中建议的很多更改,还尝试了与 maven 相关的多个建议(例如:删除文件夹、更新项目等),但对我来说没有任何效果。在我的情况下,端口 8080 不可用,因此应用程序无法使用默认端口(即:8080)启动 tomcat,导致它立即关闭。
Changing the port number helped to start tomcat and it started working. Hope it helps :)
更改端口号有助于启动 tomcat 并开始工作。希望能帮助到你 :)
回答by Biscuit Coder
For me the embed tomcat was corrupted. I have done a mvn build and found below line as a WARNING.
对我来说,嵌入的 tomcat 已损坏。我已经完成了 mvn 构建,并在下面找到了警告。
[WARNING] error reading /home/syam/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/8.5.23/tomcat-embed-core-8.5.23.jar; invalid LOC header (bad signature)
[警告] 错误读取 /home/syam/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/8.5.23/tomcat-embed-core-8.5.23.jar; 无效的 LOC 标头(错误的签名)
So deleted the tomcat embed directory and did a maven clean, things started working.
因此删除了 tomcat 嵌入目录并进行了 Maven 清理,事情开始工作了。
回答by Lakshmi K
i changed the port from 8080 to 8083 in application.properties which is under resources folder. server.port=8083 and it worked.
我在资源文件夹下的 application.properties 中将端口从 8080 更改为 8083。server.port=8083 并且它起作用了。