Java 为什么我的 Spring Boot App 总是在启动后立即关闭?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22380119/
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
Why does my Spring Boot App always shutdown immediately after starting?
提问by johnsam
This is my first Spring Boot code. Unfortunately, it always shuts down. I was expecting it to run continuously so that my web client can get some data from the browser.
这是我的第一个 Spring Boot 代码。不幸的是,它总是关闭。我希望它能够持续运行,以便我的 Web 客户端可以从浏览器获取一些数据。
package hello;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@Controller
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
[@localhost initial]$ java -jar build/libs/gs-spring-boot-0.1.0.jar
. ____ _ __ _ _
/\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.0.0.RC4)
2014-03-13 09:20:24.805 INFO 14650 --- [ main] hello.SampleController : Starting SampleController on localhost.localdomain with PID 14650 (/home/xxx/dev/gs-spring-boot/initial/build/libs/gs-spring-boot-0.1.0.jar started by xxx)
2014-03-13 09:20:25.002 INFO 14650 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@b9eec: startup date [Thu Mar 13 09:20:24 EDT 2014]; root of context hierarchy
2014-03-13 09:20:28.833 INFO 14650 --- [ main] o.s.b.a.e.jmx.EndpointMBeanExporter : Registering beans for JMX exposure on startup
2014-03-13 09:20:30.148 INFO 14650 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0
2014-03-13 09:20:30.154 INFO 14650 --- [ main] o.s.b.a.e.jmx.EndpointMBeanExporter : Located managed bean 'requestMappingEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=requestMappingEndpoint]
2014-03-13 09:20:30.316 INFO 14650 --- [ main] o.s.b.a.e.jmx.EndpointMBeanExporter : Located managed bean 'environmentEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=environmentEndpoint]
2014-03-13 09:20:30.335 INFO 14650 --- [ main] o.s.b.a.e.jmx.EndpointMBeanExporter : Located managed bean 'healthEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=healthEndpoint]
2014-03-13 09:20:30.351 INFO 14650 --- [ main] o.s.b.a.e.jmx.EndpointMBeanExporter : Located managed bean 'beansEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=beansEndpoint]
2014-03-13 09:20:30.376 INFO 14650 --- [ main] o.s.b.a.e.jmx.EndpointMBeanExporter : Located managed bean 'infoEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=infoEndpoint]
2014-03-13 09:20:30.400 INFO 14650 --- [ main] o.s.b.a.e.jmx.EndpointMBeanExporter : Located managed bean 'metricsEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=metricsEndpoint]
2014-03-13 09:20:30.413 INFO 14650 --- [ main] o.s.b.a.e.jmx.EndpointMBeanExporter : Located managed bean 'traceEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=traceEndpoint]
2014-03-13 09:20:30.428 INFO 14650 --- [ main] o.s.b.a.e.jmx.EndpointMBeanExporter : Located managed bean 'dumpEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=dumpEndpoint]
2014-03-13 09:20:30.450 INFO 14650 --- [ main] o.s.b.a.e.jmx.EndpointMBeanExporter : Located managed bean 'autoConfigurationAuditEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=autoConfigurationAuditEndpoint]
2014-03-13 09:20:30.465 INFO 14650 --- [ main] o.s.b.a.e.jmx.EndpointMBeanExporter : Located managed bean 'shutdownEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=shutdownEndpoint]
2014-03-13 09:20:30.548 INFO 14650 --- [ main] o.s.b.a.e.jmx.EndpointMBeanExporter : Located managed bean 'configurationPropertiesReportEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=configurationPropertiesReportEndpoint]
2014-03-13 09:20:30.589 INFO 14650 --- [ main] hello.SampleController : Started SampleController in 7.396 seconds (JVM running for 9.569)
2014-03-13 09:20:30.608 INFO 14650 --- [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@b9eec: startup date [Thu Mar 13 09:20:24 EDT 2014]; root of context hierarchy
2014-03-13 09:20:30.610 INFO 14650 --- [ Thread-2] o.s.c.support.DefaultLifecycleProcessor : Stopping beans in phase 0
2014-03-13 09:20:30.624 INFO 14650 --- [ Thread-2] o.s.b.a.e.jmx.EndpointMBeanExporter : Unregistering JMX-exposed beans on shutdown
Please advise.
请指教。
Thanks
谢谢
P.S. build.gradle is the fault.
PS build.gradle 是错误。
dependencies {
// tag::jetty[]
compile("org.springframework.boot:spring-boot-starter-web") {
**exclude module: "spring-boot-starter-tomcat"**
}
Once I took off the above line in bold, everything works. My application context now is correct. Thanks Dave
一旦我以粗体删除了上面的行,一切正常。我的应用程序上下文现在是正确的。谢谢戴夫
. ____ _ __ _ _
/\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.0.0.RC4)
2014-03-13 13:58:08.965 INFO 7307 --- [ main] hello.Application : Starting
Application on with PID 7307 (/ladev/home/xxx/dev/gs-spring-boot/initial/build/libs/gs-spring-boo
t-0.1.0.jar started by xxx)
2014-03-13 13:58:09.021 INFO 7307 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshi
ng org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@45490eb5: startup
date [Thu Mar 13 13:58:09 MDT 2014]; root of context hierarchy
2014-03-13 13:58:09.653 INFO 7307 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overridi
ng bean definition for bean 'beanNameViewResolver': replacing [Root bean: class [null]; scope=; abstract=fal
se; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanNam
e=org.springframework.boot.actuate.autoconfigure.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration;
factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class
path resource [org/springframework/boot/actuate/autoconfigure/ErrorMvcAutoConfiguration$WhitelabelErrorView
Configuration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3;
dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconf
igure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; in
itMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/au
toconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
采纳答案by Dave Syer
Resolution: the app is not a webapp because it doesn't have an embedded container (e.g. Tomcat) on the classpath. Adding one fixed it. If you are using Maven, then add this in pom.xml
:
解决方案:该应用程序不是 Web 应用程序,因为它在类路径上没有嵌入式容器(例如 Tomcat)。添加一个修复它。如果您使用的是Maven,则将其添加到pom.xml
:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
For Gradle(build.gradle
) it looks like
对于Gradle( build.gradle
) 它看起来像
dependencies {
compile 'org.springframework.boot:spring-boot-starter-web'
}
回答by Ajay Menon
I think the right answer was at Why does Spring Boot web app close immediately after starting?about the starter-tomcat not being set and if set and running through the IDE, the provided scope should be commented off. Scope doesn't create an issue while running through command. I wonder why.
我认为正确的答案是为什么 Spring Boot web 应用程序启动后立即关闭?关于未设置 starter-tomcat,如果设置并通过 IDE 运行,则应注释掉提供的范围。Scope 在通过命令运行时不会产生问题。我想知道为什么。
Anyways just added my additional thoughts.
无论如何只是添加了我的其他想法。
回答by N. berouain
I had the same problem but when I removed
我有同样的问题,但是当我删除时
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
it started working again.
它又开始工作了。
回答by Farrukh Najmi
In my case the problem was introduced when I fixed a static analysis error that the return value of a method was not used.
在我的情况下,当我修复未使用方法的返回值的静态分析错误时引入了问题。
Old working code in my Application.java was:
我的 Application.java 中的旧工作代码是:
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
New code that introduced the problem was:
引入问题的新代码是:
public static void main(String[] args) {
try (ConfigurableApplicationContext context =
SpringApplication.run(Application.class, args)) {
LOG.trace("context: " + context);
}
}
Obviously, the try with resource block will close the context after starting the application which will result in the application exiting with status 0. This was a case where the resource leak error reported by snarqube static analysis should be ignored.
显然,try with resource 块会在启动应用程序后关闭上下文,这将导致应用程序以状态 0 退出。 这是一种应该忽略 snarqube 静态分析报告的资源泄漏错误的情况。
回答by chatsap
Here is how you can fix it:
以下是修复它的方法:
Check if you don't have dependency on spring-boot-starter-web in your pom.xml file. To get you pom.xml file right, use this link start.spring.io
If you have above dependency, but still facing the issue, it is highly possible that your embedded tomcat jars are present. To confirm this, run maven build in debug mode -
检查您的 pom.xml 文件中是否不依赖 spring-boot-starter-web。为了让你 pom.xml 文件正确,使用这个链接start.spring.io
如果您具有上述依赖性,但仍然面临问题,则很可能存在您的嵌入式 tomcat jar。要确认这一点,请在调试模式下运行 maven build -
mvn spring-boot:run --debug
mvn spring-boot:run --debug
and look for messages like -
并寻找类似的消息 -
[WARNING] error reading /Users/sparrowmac1/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/8.5.20/tomcat-embed-core-8.5.20.jar; invalid LOC header (bad signature) [WARNING] error reading /Users/sparrowmac1/.m2/repository/com/fasterxml/Hymanson/core/Hymanson-core/2.8.10/Hymanson-core-2.8.10.jar; invalid LOC header (bad signature)
[WARNING] error reading /Users/sparrowmac1/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/8.5.20/tomcat-embed-core-8.5.20.jar; invalid LOC header (bad signature) [WARNING] error reading /Users/sparrowmac1/.m2/repository/com/fasterxml/Hymanson/core/Hymanson-core/2.8.10/Hymanson-core-2.8.10.jar; invalid LOC header (bad signature)
If such messages are present, purge your local maven repo and try again -
如果存在此类消息,请清除您的本地 Maven 存储库并重试 -
mvn dependency:purge-local-repository
mvn dependency:purge-local-repository
回答by Guchelkaben
Maybe it does not fit to your code but i found out if you have a code snippet like this:
也许它不适合您的代码,但我发现您是否有这样的代码片段:
@SpringBootApplication
public class SpringBootApacheKafkaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootApacheKafkaApplication.class,args).close();
}
}
then just remove the close() method. That's fixed my problem! Maybe I can help someone with that
然后只需删除 close() 方法。这解决了我的问题!也许我可以帮助某人
回答by RAUL
this work with spring boot 2.0.0
这项工作与弹簧靴 2.0.0
replace
代替
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
with
和
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>9.0.6</version>
</dependency>
回答by Abhijit Pritam Dutta
In my case I fixed this issue like below:-
就我而言,我解决了这个问题,如下所示:-
First I removed (apache)
C:\Users\myuserId\.m2\repository\org\apache
I added below dependencies in my
pom.xml
file<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
I have changed the default socket by adding below lines in resource file
..\yourprojectfolder\src\main\resourcesand\application.properties
(I manually created this file)server.port=8099 [email protected]@
for that I have added below block in my
pom.xml
under<build>
section.<build> . . <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> . . </build>
首先我删除了(apache)
C:\Users\myuserId\.m2\repository\org\apache
我在我的
pom.xml
文件中添加了以下依赖项<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
我通过在资源文件中添加以下行来更改默认套接字
..\yourprojectfolder\src\main\resourcesand\application.properties
(我手动创建了这个文件)server.port=8099 [email protected]@
对于我在下面加块
pom.xml
下<build>
部分。<build> . . <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> . . </build>
My final pom.xml
file look like
我的最终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.bhaiti</groupId>
<artifactId>spring-boot-rest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-rest</name>
<description>Welcome project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.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</groupId>
<artifactId>spring-web</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>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
回答by Mohammed Rafeeq
Just another possibility,
只是另一种可能,
I replaced
我换了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
with
和
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
and it started without any issues
它开始没有任何问题
回答by deFreitas
If you don't want to make your spring a web application then just add @EnableAsync
or @EnableScheduling
to your Starter
如果您不想让 Spring 成为 Web 应用程序,那么只需将@EnableAsync
或添加@EnableScheduling
到您的 Starter
@EnableAsync
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}